新聞中心
Python的math模塊提供了一系列數(shù)學(xué)運(yùn)算相關(guān)的函數(shù),這些函數(shù)對(duì)于科學(xué)計(jì)算、數(shù)據(jù)分析以及需要在Python程序中執(zhí)行復(fù)雜數(shù)學(xué)運(yùn)算的任何場(chǎng)景都非常有用,以下是一些常用的math模塊函數(shù)及其用法:

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了伊通免費(fèi)建站歡迎大家使用!
1、math.sqrt(x): 返回x的平方根。
2、math.fabs(x): 返回x的絕對(duì)值。
3、math.factorial(x): 返回x的階乘。
4、math.pow(x, y): 返回x的y次冪。
5、math.log(x[, base]): 返回x的自然對(duì)數(shù),base參數(shù)可選,默認(rèn)為e。
6、math.log10(x): 返回以10為底的x的對(duì)數(shù)。
7、math.exp(x): 返回e的x次冪。
8、math.sin(x): 返回x的正弦值(在弧度中)。
9、math.cos(x): 返回x的余弦值(在弧度中)。
10、math.tan(x): 返回x的正切值。
11、math.pi: 返回π的值。
12、math.e: 返回自然對(duì)數(shù)的底數(shù)e的值。
13、math.trunc(x): 返回x的整數(shù)部分。
14、math.ceil(x): 返回大于或等于x的最小整數(shù)。
15、math.floor(x): 返回小于或等于x的最大整數(shù)。
16、math.modf(x): 將x分解為小數(shù)和整數(shù)部分。
接下來(lái),我們將通過(guò)一些示例來(lái)展示如何使用math模塊中的函數(shù):
import math
計(jì)算平方根
square_root = math.sqrt(16)
print("Square root of 16 is", square_root)
計(jì)算絕對(duì)值
absolute_value = math.fabs(5.3)
print("Absolute value of 5.3 is", absolute_value)
計(jì)算階乘
factorial = math.factorial(5)
print("Factorial of 5 is", factorial)
冪運(yùn)算
power = math.pow(2, 3)
print("2 raised to the power of 3 is", power)
自然對(duì)數(shù)
natural_log = math.log(10)
print("Natural logarithm of 10 is", natural_log)
以10為底的對(duì)數(shù)
log10 = math.log10(100)
print("Logarithm base 10 of 100 is", log10)
e的冪
e_power = math.exp(1)
print("e raised to the power of 1 is", e_power)
三角函數(shù)
angle_in_radians = math.pi / 4
sine_value = math.sin(angle_in_radians)
cosine_value = math.cos(angle_in_radians)
print("Sine of pi/4 is", sine_value)
print("Cosine of pi/4 is", cosine_value)
圓周率π和自然對(duì)數(shù)底數(shù)e
pi = math.pi
e = math.e
print("Value of Pi is", pi)
print("Value of E is", e)
取整相關(guān)函數(shù)
integer_part = math.trunc(3.14159)
ceiling_value = math.ceil(3.14159)
floor_value = math.floor(3.14159)
print("Truncated value of 3.14159 is", integer_part)
print("Ceiling value of 3.14159 is", ceiling_value)
print("Floor value of 3.14159 is", floor_value)
上述代碼展示了如何利用math模塊進(jìn)行常見(jiàn)的數(shù)學(xué)運(yùn)算,在使用math模塊時(shí),需要先導(dǎo)入該模塊,然后才能調(diào)用其提供的函數(shù),三角函數(shù)如sin, cos, tan等都需要接受弧度作為參數(shù),而不是度數(shù),如果需要將度數(shù)轉(zhuǎn)換為弧度,可以使用math.radians(degrees)函數(shù),反之,將弧度轉(zhuǎn)換為度數(shù),則可以使用math.degrees(radians)函數(shù)。
標(biāo)題名稱(chēng):Pythonmath函數(shù)調(diào)用
網(wǎng)站鏈接:http://m.fisionsoft.com.cn/article/dhcieec.html


咨詢(xún)
建站咨詢(xún)
