日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python數學模塊(math/decimal模塊)_python

作者:小地瓜重新去華容道工作 ? 更新時間: 2022-11-17 編程語言

一, math模塊

math庫是python提供的內置數學類函數庫,math庫不支持復數類型,僅支持整數和浮點數運算。

常數 說明 實例
math.pi 圓周率Π math.pi輸出結果:3.141592653589793
math.e 自然常數e math.e輸出結果:2.718281828459045
math.inf 正無窮大,
-math.inf是負無窮大
math.inf輸出?inf
math.nan 非浮點數標記,NaN math.nan輸出結果:nan

2. math庫常用函數

函數名 說明
math.ceil(f) 向上取整,返回值:整數值
math.floor(f) 向下取整,返回值:整數
round(f) 四舍五入,返回值:整數
math.fabs(f) 獲取絕對值操作,返回值:浮點數
abs(num) 獲取絕對值操作,返回值:根據傳入的參數而定
math.fmod(x,y) 返回x/y的余數,返回值:浮點數
math.pow(x,n) 返回x的n次方,返回值:浮點型
math.sqrt(num) 對num開平方,返回值:浮點數
fsum(seq) 返回序列中所有元素的和,返回值:浮點數
sum(seq) 將一個序列的數值進行相加求和,返回值:根據序列中數值的類型變化
math.modf(num) 將一個浮點數拆成小數和整數部分組成的元組,返回值:元組
math.trunc(f) 返回浮點數的整數部分,返回值:整數
math.copysign(n1,n1) 將第二個數的正負號賦值給第一個數,返回值:浮點數
math.factorial(x) 返回x的階乘,如果x不是整數或為負數將引發ValueError,返回值:整數
math.gcd(x,y) 返回整數x和y的最大公約數,返回值:整數

3.math庫使用示例

# -*- coding: utf-8 -*-
import math

# math庫常用變量
print("math.pi = ", math.pi)
print('math.e = ', math.e)
print('math.inf = ', math.inf)
print('math.nan = ', math.nan)


# math庫常用函數

print('math.ceil()向上取整,math.ceil(2.3) = ', math.ceil(2.3))
print('math.ceil()向上取整,math.ceil(2.5) = ', math.ceil(2.5))
print('math.ceil()向上取整,math.ceil(2.0) = ', math.ceil(2.0))
print('math.ceil()向上取整,math.ceil(2.8) = ', math.ceil(2.8))
print('math.ceil()向上取整,math.ceil(-2.8) = ', math.ceil(-2.8))

print('math.floor()向下取整,math.floor(2.3) = ', math.floor(2.3))
print('math.floor()向下取整,math.floor(2.5) = ', math.floor(2.5))
print('math.floor()向下取整,math.floor(2.0) = ', math.floor(2.0))
print('math.floor()向下取整,math.floor(2.8) = ', math.floor(2.8))
print('math.floor()向下取整,math.floor(-2.8) = ', math.floor(-2.8))

print('round()四舍五入,round(2.3) = ', round(2.3))
print('round()四舍五入,roundr(2.5) = ', round(2.5))
print('round()四舍五入,round(2.0) = ', round(2.0))
print('round()四舍五入,round(2.8) = ', round(2.8))
print('round()四舍五入,round(-2.8) = ', round(-2.8))

print('math.fabs()獲取絕對值,math.fabs(2.3) = ', math.fabs(2.3))
print('math.fabs()獲取絕對值,math.fabs(-2.3) = ', math.fabs(-2.3))
print('math.fabs()獲取絕對值,math.fabs(-2.0) = ', math.fabs(-2.0))
print('math.fabs()獲取絕對值,math.fabs(-2) = ', math.fabs(-2))

print('abs()獲取絕對值,abs(2.3) = ', abs(2.3))
print('abs()獲取絕對值,abs(-2.3) = ', abs(-2.3))
print('abs()獲取絕對值,abs(-2.0) = ', abs(-2.0))
print('abs()獲取絕對值,abs(-2) = ', abs(-2))

print('math.fmod(x,y)獲取x/y的余數,math.fmod(2,3) = ' ,math.fmod(2,3))
print('math.pow(x,y)獲取x的n次方,math.pow(2,3) = ', math.pow(2,3))
print('math.sqrt()獲取開放根,math.sqrt(4) = ', math.sqrt(4))
print('fsum()獲取序列中所有元素的和,fsum([1,2,3,4,5,6]) = ', math.fsum([1,2,3,4,5,6]))
print('sum()獲取序列中所有元素的和,sum([1,2,3,4,5,6]) = ', sum([1,2,3,4,5,6]))
print('math.modf()獲取浮點數的小數和整數部分,math.modf(2.3) = ', math.modf(2.3))
print('math.trunc()獲取浮點數的整數部分,math.trunc(2.3) = ', math.trunc(2.3))
print('math.copysign(n1,n2)把第二個數的正負號賦值給第一個浮點數,math.copysign(-2.3,1) = ', math.copysign(-2.3,1))
print('math.copysign(n1,n2)把第二個數的正負號賦值給第一個浮點數,math.copysign(2.3,-1) = ', math.copysign(2.3,-1))
print('math.gcd(x,y)獲取x和y的最大公約數,math.gcd(16,24) = ', math.gcd(16,24))
try:
    print('math.factorial()獲取階乘,math.factorial(3) = ', math.factorial(3))
    print('math.factorial()獲取階乘,math.factorial(2.3) = ', math.factorial(2.3))
    print('math.factorial()獲取階乘,math.factorial(-2) = ', math.factorial(-2))
except ValueError as e:
    print(e)
finally:
    pass

二, decimal模塊

decimal模塊提供了一個Decimal數據類型用于浮點數計算。相比內置的二進制浮點數實現float,Decimal有助于金融應用和其它需要精確十進制表達的場合,控制精度,控制舍入以適應法律或者規定要求,確保十進制數位精度,或者用戶希望計算結果與手算相符的場合。
Decimal重現了手工的數學運算,確保了二進制浮點數無法精確保有的數據精度。高精度使Decimal可以執行二進制浮點數無法進行的模運算和等值測試。

1. 什么時候使用decimal

python中小數相加可能計算結果不對,是由于科學計算精度問題,如果需要處理這個問題就需要用到decimal模塊。

2. 使用decimal

設置精度:decimal.getcontext().prec = num,num為有效數字個數

設置小數位數:quantize()

注意:decimal.getcontext().prec 和 quantize()不能同時使用,如果同時使用會提示錯誤:decimal.InvalidOperation: [<class ‘decimal.InvalidOperation’>]

3. decimal使用示例

# -*- coding: utf-8 -*-
import decimal

"""
decimal.getcontext().prec = 3  # 設置有效數字是3位
print(decimal.Decimal(2.32) + decimal.Decimal(3.01))

decimal.getcontext().prec = 2  # 設置有效數字是2位
print(decimal.Decimal(2.32) + decimal.Decimal(3.01))
"""

# quantize()設置小數位數
num = decimal.Decimal(1.23456789).quantize(decimal.Decimal('0.000'))
print(num)

原文鏈接:https://blog.csdn.net/sinat_41752325/article/details/127065279

欄目分類
最近更新