math python 向上取整_Python成为专业人士笔记-各数学运算操作深度剖析
“專業人士筆記”系列目錄:
創帆云:Python成為專業人士筆記--強烈建議收藏!每日持續更新!?zhuanlan.zhihu.comPython可以執行常見的數學運算符,包括整數和浮點除法、乘法、取冪、加法和減法,而數學math模塊(包含在所有標準Python版本中)提供了擴展功能,如三角函數、根操作、對數等。
Division 除法
當兩個操作數都是整數時,Python3執行后返回的結果是float數據類型,這點和Python2不同;Python不同版本對除法的處理會有不同,其是根據除號兩端變量的數據類型決定如何處理的
比如, 我們先定義幾個變量:
a, b, c, d, e = 3, 2, 2.0, -3, 10在Python 3中,
‘/’ 運算符執行“ true”原始除法,即除出來是結果是多少就顯示多少, 和參與的數據類型無關。但注意結果的數據類型都是小數float型(這點和pyton2不同);
‘//’運算是除完后強制取整數部分(注意,不是四舍五入),其結果的數據類型,取決于參與計算的數據類型,兩個都為整數則結果為整數;其中有一個是float小數,則整個結果為小數;兩個都為小數,則結果也是小數
a / b # = 1.5e / b # = 5.0a // b # = 1a // c # = 1.0import operator# operator 模塊接收兩個算術參數operator.truediv(a, b)#輸出 1.5operator.floordiv(a, b)= 1#輸出 1operator.floordiv(a, c)#輸出 1.0內置數據類型之間相除后的返回數據類型:
int and int ( Python 2 中返回 int整數結果,Python 3 中返回 float小數結果)int and float (返回 float)int and complex (返回 complex 復數)float and float (返回 float)float and complex (返回 complex)complex and complex (返回 complex)Addition 加法
a, b = 1, 2 用 "+" 操作符: a + b # = 3# 用 "in-place" "+=" 進行變量的添加和重賦值:a += b # a = 3 (相當于 a = a + b,對變量操作并重賦值了)import operatoroperator.add(a, b)# 輸出 5 注意:a變量在上面一行代碼變成3了a = operator.iadd(a, b)# a = 5 相當于 a = a + b,對變量操作并重賦值了內置數據類型之間相加后返回的數據類型:
int and int (返回 int) int and float (返回 float) int and complex (返回 complex) float and float (返回 float) float and complex (返回 complex) complex and complex (返回 complex)注意:‘+’運算符也用于連接字符串、列表和元組
"first string " + "second string"#輸出: 'first string second string'[1, 2, 3] + [4, 5, 6]#輸出:[1, 2, 3, 4, 5, 6]取冪運算
a, b = 2, 3(a ** b)#= 8pow(a, b)#= 8import mathmath.pow(a, b)= 8.0 (float小數,不支持complex復數)import operatoroperator.pow(a, b)#= 8內置的pow和數學模塊math的pow之間的另一個區別是,內置的pow可以接受三個參數
a, b, c = 2, 3, 2pow(2, 3, 2)# 輸出0,計算等效于 (2 ** 3)% 2,但根據Python文檔,直接這樣寫更有效而不是調用pow函數特殊函數
函數math.sqrt(x)計算x的平方根
import mathimport cmathc = 4math.sqrt(c)#輸出 2.0 ( float小數,不支持complex復數 )cmath.sqrt(c)#輸出 (2+0j) (提供complex復數支持)若要計算其他根,如立方根,則將該數提到根的次數的倒數,這可以用任何指數函數或操作符來做。
import mathx = 8math.pow(x, 1/3) # 計算結果為2.0 x**(1/3) # 計算結果為 2.0函數math.exp(x)計算e ** x :
math.exp(0)#輸出1.0math.exp(1)#輸出2.718281828459045 (e)函數math.expm1(x)計算e ** x – 1。當x很小時,這比math.exp(x) – 1的精度要高得多
math.expm1(0)#0.0math.exp(1e-6) - 1#1.0000004999621837e-06math.expm1(1e-6)#1.0000005000001665e-06完整結果: # 1.000000500000166666708333341666…Trigonometric Functions 三角函數
a, b = 1, 2import mathmath.sin(a)#返回弧度“ a”的正弦 0.8414709848078965math.cosh(b)#返回弧度'b'的反雙曲余弦值 #輸出: 3.7621956910836314math.atan(math.pi)# 返回以弧度為單位的反正切 1.2626272556789115 math.hypot(a, b) # 返回歐幾里得范數,與math.sqrt(aa + bb)相同 2.23606797749979請注意math.hypot(x,y)是 點(x,y) 距原點(0,0)的向量的長度(或歐幾里得距離), 所以要計算兩個點(x1,y1)和(x2,y2)之間的歐幾里得距離,可以使用這個函數:math.hypot(x2-x1, y2-y1)
要從弧度->度和度->弧度分別使用math.degrees和math.radians進行轉換
math.degrees(a)#輸出: 57.29577951308232math.radians(57.29577951308232)#輸出: 1.0簡易操作符
在應用程序中,通常需要這樣的代碼 :
a = a + 1
或
a = a * 2
對于這些操作有一個有效的快捷方式
a += 1和a *= 2任何數學運算符都可以使用在’=’字符之前,以進行簡易運算:
-= 遞減變量 += 遞增變量 *= 遞乘變量 /= 遞除變量//= 遞除后取整變量%= 遞取模后變量**= 遞取 冪 變量同樣,對于按位運算符(^,| 等都是適用的
Subtraction 減法
a, b = 1, 2使用“-”操作符 :b - a # 輸出= 1import operator包含2個參數算術的函數 operator.sub(b, a)#輸出= 1內置數據類型之間相減后返回的數據類型:
int and int (返回 int)int and float ( 返回 float)int and complex ( 返回 complex)float and float ( 返回 float)float and complex ( 返回 complex)complex and complex ( 返回 complex)Multiplication 乘法
a, b = 2, 3a * b # 輸出 6import operatoroperator.mul(a, b)#輸出 6內置數據類型之間相乘后返回的數據類型:
int and int ( 返回 int)int and float ( 返回 float)int and complex ( 返回 complex)float and float ( 返回 float)float and complex ( 返回 complex)complex and complex ( 返回 complex)注意: *操作符也可用于字符串、列表和元組的重復連接 :
3 * 'ab'#輸出: 'ababab'3 * ('a', 'b')#元組被重復了 ('a', 'b', 'a', 'b', 'a', 'b')Logarithms 對數
默認情況下,math.log函數計算以e為底數的對數,你可以選擇指定一個底數作為第二個參數:
import mathimport cmathmath.log(5)#輸出 1.6094379124341003#基礎參數可選,默認是math.e math.log(5, math.e) # 輸出 1.6094379124341003cmath.log(5)#輸出 (1.6094379124341003+0j)math.log(1000, 10)#輸出:3.0 (常返回 float)cmath.log(1000, 10)#輸出:(3+0j)Log函數的特殊變體適用于不同的情況:
# 以e - 1為底的對數(值較小時精度更高) math.log1p(5)#輸出 1.791759469228055#對數底2math.log2(8)#輸出 3.0#對數底10math.log10(100)#輸出 2.0cmath.log10(100)#輸出 (2+0j)Modulus 模數
與許多其他語言一樣,Python使用%運算符來取模
3 % 4#輸出310 % 2#輸出 06 % 4#輸出 2或者使用 operator module:
import operator operator.mod(3 , 4)#輸出 3 operator.mod(10 , 2)#輸出 0operator.mod(6 , 4)#輸出 2當然, 你也可以用負數 :
-9 % 7#輸出 59 % -7#輸出 -5-9 % -7#輸出 -2如果需要找到整數除法和模數的結果,可以使用divmod函數:
quotient, remainder = divmod(9, 4)# quotient = 2, remainder = 1 因為 4 * 2 + 1 == 9今天的分享就到這里,禁止轉載,違者必究!
總結
以上是生活随笔為你收集整理的math python 向上取整_Python成为专业人士笔记-各数学运算操作深度剖析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: char怎么比较_为什么阿里巴巴Java
- 下一篇: java进度条_自学java你需要知道的