python math.frexp()方法
frexp()语法
math.frexp(x, /)
python源码中对frexp()方法的介绍:
Return the mantissa and exponent of x, as pair (m, e).
m is a float and e is an int, such that x = m * 2.**e.
If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
即,python标准库math中的frexp()方法,可以用来以(m, e)值对的方式返回x的尾数和指数,公式为x=m * 2.**e。且m为浮点数,e为整数。如果x为0,则m为0.0,e为0,如果x不为0,m的绝对值介于[0.5,1.0)之间。
参数
参数 | 描述 |
---|---|
x | real number |
math.frexp()实例代码
>>> import math
>>> math.frexp(1)
(0.5, 1)
>>> math.frexp(1.5)
(0.75, 1)
>>> math.frexp('0') #参数必须是real number
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be real number, not str
>>> math.frexp(0)
(0.0, 0)