1. 程式人生 > 實用技巧 >11. math庫函式

11. math庫函式

一、math庫介紹

內建數學類函式庫,math庫不支援複數型別,僅支援整數和浮點數運算。

math庫一共提供了:

4個數字常數

44個函式,分為4類:

16個數值表示函式

8個冪對數函式

16個三角對數函式

4個高等特殊函式

二、math庫使用

庫中函式不能直接使用,需使用保留字import引用:

import math

math.<函式名>(...)

或者

from math import <函式名>

<函式名>(...)

三、math庫函式

3.1 math庫的數字常數(4個)

3.2 math庫的數值表示函式(16個)

3.3 math庫的冪對數函式(8個)

3.4 math庫的三角運算函式(16個)

3.5 math庫的高等特殊函式(4個)

四:舉例

# math_constants.pyimportmathprint(' π: {:.30f}'.format(math.pi))print(' e: {:.30f}'.format(math.e))print('nan: {:.30f}'.format(math.nan))print('inf: {:.30f}'.format(math.inf))

π和 e 的精度僅受平臺浮點C語言庫限制。

$ python3 math_constants.py

π: 3.141592653589793115997963468544

e: 2.718281828459045090795598298428

nan: nan

inf: inf

其他一些具體詳細操作:https://zhuanlan.zhihu.com/p/107101718