Python3 modf()函式

2019-10-16 23:12:01
modf() 方法返回x的整數和小數部分,並放在元組中兩個專案。兩個部分與x的符號相同。整數部分返回為float。

語法

下面是 modf() 方法的語法:
import math

math.modf( x )

註:此函式無法直接存取,所以我們需要匯入 math 模組,然後用數學靜態物件呼叫這個函式。

引數

  • x -- 這是一個數位表示式

返回值

此方法返回x的整數和小數部分並分別放在元組中的兩專案。 兩個部分與x的符號相同。整數部分返回為float。

範例

下面的範例顯示 modf() 方法的使用。
#!/usr/bin/python3
import math   # This will import math module

print ("math.modf(100.12) : ", math.modf(100.12))
print ("math.modf(100.72) : ", math.modf(100.72))
print ("math.modf(119) : ", math.modf(119))
print ("math.modf(math.pi) : ", math.modf(math.pi))
當我們執行上面的程式,它會產生以下結果:
math.modf(100.12) :  (0.12000000000000455, 100.0)
math.modf(100.72) :  (0.7199999999999989, 100.0)
math.modf(119) :  (0.0, 119.0)
math.modf(math.pi) :  (0.14159265358979312, 3.0)