cmath
— 用于复数的数学函数
¶
This module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accept any Python object that has either a
__complex__()
或
__float__()
method: these methods are used to convert the object to a complex or floating-point number, respectively, and the function is then applied to the result of the conversion.
注意
For functions involving branch cuts, we have the problem of deciding how to define those functions on the cut itself. Following Kahan’s “Branch cuts for complex elementary functions” paper, as well as Annex G of C99 and later C standards, we use the sign of zero to distinguish one side of the branch cut from the other: for a branch cut along (a portion of) the real axis we look at the sign of the imaginary part, while for a branch cut along the imaginary axis we look at the sign of the real part.
例如,
cmath.sqrt()
function has a branch cut along the negative real axis. An argument of
complex(-2.0, -0.0)
is treated as though it lies
below
the branch cut, and so gives a result on the negative imaginary axis:
>>> cmath.sqrt(complex(-2.0, -0.0)) -1.4142135623730951j