http://docs.python.org/2/library/math.html從:爲什麼math.modf返回浮動?
math.frexp(X)
返回尾數和x作爲對指數(M,E)。 m是浮點數,e是整數,使得x == m * 2 ** e準確。如果x爲零,則返回(0.0,0),否則返回0.5 < = abs(m)< 1.這用於以便攜方式「分離」浮點的內部表示形式。
math.modf(X)
返回x的整數和小數部分。兩個結果都帶有x 的符號,並且浮標爲。
在this related question,它指出,返回浮動並沒有真正意義上的小區和地板,所以在Python 3,便改爲返回整數。 modf
的整數結果是不是以整數形式返回,是否有某些原因?在Python 2.7:
In [2]: math.floor(5.5)
Out[2]: 5.0
In [3]: math.modf(5.5)
Out[3]: (0.5, 5.0)
In [4]: math.frexp(5.5)
Out[4]: (0.6875, 3)
在Python 3:
In [2]: math.floor(5.5)
Out[2]: 5
In [3]: math.modf(5.5)
Out[3]: (0.5, 5.0)
In [4]: math.frexp(5.5)
Out[4]: (0.6875, 3)
你想要使用它的大多數地方你可能想要一個浮動。 (雖然在3.x中,現在int/int返回浮點數,但它可能並不重要。)你的用例是什麼? – abarnert