日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

math python 向上取整_Python成为专业人士笔记-各数学运算操作深度剖析

發(fā)布時(shí)間:2024/7/19 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 math python 向上取整_Python成为专业人士笔记-各数学运算操作深度剖析 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

“專(zhuān)業(yè)人士筆記”系列目錄:

創(chuàng)帆云:Python成為專(zhuān)業(yè)人士筆記--強(qiáng)烈建議收藏!每日持續(xù)更新!?zhuanlan.zhihu.com

Python可以執(zhí)行常見(jiàn)的數(shù)學(xué)運(yùn)算符,包括整數(shù)和浮點(diǎn)除法、乘法、取冪、加法和減法,而數(shù)學(xué)math模塊(包含在所有標(biāo)準(zhǔn)Python版本中)提供了擴(kuò)展功能,如三角函數(shù)、根操作、對(duì)數(shù)等。

Division 除法

當(dāng)兩個(gè)操作數(shù)都是整數(shù)時(shí),Python3執(zhí)行后返回的結(jié)果是float數(shù)據(jù)類(lèi)型,這點(diǎn)和Python2不同;Python不同版本對(duì)除法的處理會(huì)有不同,其是根據(jù)除號(hào)兩端變量的數(shù)據(jù)類(lèi)型決定如何處理的

比如, 我們先定義幾個(gè)變量:

a, b, c, d, e = 3, 2, 2.0, -3, 10

在Python 3中,

‘/’ 運(yùn)算符執(zhí)行“ true”原始除法,即除出來(lái)是結(jié)果是多少就顯示多少, 和參與的數(shù)據(jù)類(lèi)型無(wú)關(guān)。但注意結(jié)果的數(shù)據(jù)類(lèi)型都是小數(shù)float型(這點(diǎn)和pyton2不同);

‘//’運(yùn)算是除完后強(qiáng)制取整數(shù)部分(注意,不是四舍五入),其結(jié)果的數(shù)據(jù)類(lèi)型,取決于參與計(jì)算的數(shù)據(jù)類(lèi)型,兩個(gè)都為整數(shù)則結(jié)果為整數(shù);其中有一個(gè)是float小數(shù),則整個(gè)結(jié)果為小數(shù);兩個(gè)都為小數(shù),則結(jié)果也是小數(shù)

a / b # = 1.5e / b # = 5.0a // b # = 1a // c # = 1.0import operator# operator 模塊接收兩個(gè)算術(shù)參數(shù)operator.truediv(a, b)#輸出 1.5operator.floordiv(a, b)= 1#輸出 1operator.floordiv(a, c)#輸出 1.0

內(nèi)置數(shù)據(jù)類(lèi)型之間相除后的返回?cái)?shù)據(jù)類(lèi)型:

int and int ( Python 2 中返回 int整數(shù)結(jié)果,Python 3 中返回 float小數(shù)結(jié)果)int and float (返回 float)int and complex (返回 complex 復(fù)數(shù))float and float (返回 float)float and complex (返回 complex)complex and complex (返回 complex)

Addition 加法

a, b = 1, 2 用 "+" 操作符: a + b # = 3# 用 "in-place" "+=" 進(jìn)行變量的添加和重賦值:a += b # a = 3 (相當(dāng)于 a = a + b,對(duì)變量操作并重賦值了)import operatoroperator.add(a, b)# 輸出 5 注意:a變量在上面一行代碼變成3了a = operator.iadd(a, b)# a = 5 相當(dāng)于 a = a + b,對(duì)變量操作并重賦值了

內(nèi)置數(shù)據(jù)類(lèi)型之間相加后返回的數(shù)據(jù)類(lèi)型:

int and int (返回 int) int and float (返回 float) int and complex (返回 complex) float and float (返回 float) float and complex (返回 complex) complex and complex (返回 complex)

注意:‘+’運(yùn)算符也用于連接字符串、列表和元組

"first string " + "second string"#輸出: 'first string second string'[1, 2, 3] + [4, 5, 6]#輸出:[1, 2, 3, 4, 5, 6]

取冪運(yùn)算

a, b = 2, 3(a ** b)#= 8pow(a, b)#= 8import mathmath.pow(a, b)= 8.0 (float小數(shù),不支持complex復(fù)數(shù))import operatoroperator.pow(a, b)#= 8

內(nèi)置的pow和數(shù)學(xué)模塊math的pow之間的另一個(gè)區(qū)別是,內(nèi)置的pow可以接受三個(gè)參數(shù)

a, b, c = 2, 3, 2pow(2, 3, 2)# 輸出0,計(jì)算等效于 (2 ** 3)% 2,但根據(jù)Python文檔,直接這樣寫(xiě)更有效而不是調(diào)用pow函數(shù)

特殊函數(shù)

函數(shù)math.sqrt(x)計(jì)算x的平方根

import mathimport cmathc = 4math.sqrt(c)#輸出 2.0 ( float小數(shù),不支持complex復(fù)數(shù) )cmath.sqrt(c)#輸出 (2+0j) (提供complex復(fù)數(shù)支持)

若要計(jì)算其他根,如立方根,則將該數(shù)提到根的次數(shù)的倒數(shù),這可以用任何指數(shù)函數(shù)或操作符來(lái)做。

import mathx = 8math.pow(x, 1/3) # 計(jì)算結(jié)果為2.0 x**(1/3) # 計(jì)算結(jié)果為 2.0

函數(shù)math.exp(x)計(jì)算e ** x :

math.exp(0)#輸出1.0math.exp(1)#輸出2.718281828459045 (e)

函數(shù)math.expm1(x)計(jì)算e ** x – 1。當(dāng)x很小時(shí),這比math.exp(x) – 1的精度要高得多

math.expm1(0)#0.0math.exp(1e-6) - 1#1.0000004999621837e-06math.expm1(1e-6)#1.0000005000001665e-06完整結(jié)果: # 1.000000500000166666708333341666…

Trigonometric Functions 三角函數(shù)

a, b = 1, 2import mathmath.sin(a)#返回弧度“ a”的正弦 0.8414709848078965math.cosh(b)#返回弧度'b'的反雙曲余弦值 #輸出: 3.7621956910836314math.atan(math.pi)# 返回以弧度為單位的反正切 1.2626272556789115 math.hypot(a, b) # 返回歐幾里得范數(shù),與math.sqrt(aa + bb)相同 2.23606797749979

請(qǐng)注意math.hypot(x,y)是 點(diǎn)(x,y) 距原點(diǎn)(0,0)的向量的長(zhǎng)度(或歐幾里得距離), 所以要計(jì)算兩個(gè)點(diǎn)(x1,y1)和(x2,y2)之間的歐幾里得距離,可以使用這個(gè)函數(shù):math.hypot(x2-x1, y2-y1)

要從弧度->度和度->弧度分別使用math.degrees和math.radians進(jìn)行轉(zhuǎn)換

math.degrees(a)#輸出: 57.29577951308232math.radians(57.29577951308232)#輸出: 1.0

簡(jiǎn)易操作符

在應(yīng)用程序中,通常需要這樣的代碼 :

a = a + 1

a = a * 2

對(duì)于這些操作有一個(gè)有效的快捷方式

a += 1和a *= 2

任何數(shù)學(xué)運(yùn)算符都可以使用在’=’字符之前,以進(jìn)行簡(jiǎn)易運(yùn)算:

-= 遞減變量 += 遞增變量 *= 遞乘變量 /= 遞除變量//= 遞除后取整變量%= 遞取模后變量**= 遞取 冪 變量

同樣,對(duì)于按位運(yùn)算符(^,| 等都是適用的

Subtraction 減法

a, b = 1, 2使用“-”操作符 :b - a # 輸出= 1import operator包含2個(gè)參數(shù)算術(shù)的函數(shù) operator.sub(b, a)#輸出= 1

內(nèi)置數(shù)據(jù)類(lèi)型之間相減后返回的數(shù)據(jù)類(lèi)型:

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

內(nèi)置數(shù)據(jù)類(lèi)型之間相乘后返回的數(shù)據(jù)類(lèi)型:

int and int ( 返回 int)int and float ( 返回 float)int and complex ( 返回 complex)float and float ( 返回 float)float and complex ( 返回 complex)complex and complex ( 返回 complex)

注意: *操作符也可用于字符串、列表和元組的重復(fù)連接 :

3 * 'ab'#輸出: 'ababab'3 * ('a', 'b')#元組被重復(fù)了 ('a', 'b', 'a', 'b', 'a', 'b')

Logarithms 對(duì)數(shù)

默認(rèn)情況下,math.log函數(shù)計(jì)算以e為底數(shù)的對(duì)數(shù),你可以選擇指定一個(gè)底數(shù)作為第二個(gè)參數(shù):

import mathimport cmathmath.log(5)#輸出 1.6094379124341003#基礎(chǔ)參數(shù)可選,默認(rèn)是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函數(shù)的特殊變體適用于不同的情況:

# 以e - 1為底的對(duì)數(shù)(值較小時(shí)精度更高) math.log1p(5)#輸出 1.791759469228055#對(duì)數(shù)底2math.log2(8)#輸出 3.0#對(duì)數(shù)底10math.log10(100)#輸出 2.0cmath.log10(100)#輸出 (2+0j)

Modulus 模數(shù)

與許多其他語(yǔ)言一樣,Python使用%運(yùn)算符來(lái)取模

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

當(dāng)然, 你也可以用負(fù)數(shù) :

-9 % 7#輸出 59 % -7#輸出 -5-9 % -7#輸出 -2

如果需要找到整數(shù)除法和模數(shù)的結(jié)果,可以使用divmod函數(shù):

quotient, remainder = divmod(9, 4)# quotient = 2, remainder = 1 因?yàn)?4 * 2 + 1 == 9

今天的分享就到這里,禁止轉(zhuǎn)載,違者必究!

總結(jié)

以上是生活随笔為你收集整理的math python 向上取整_Python成为专业人士笔记-各数学运算操作深度剖析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。