Python has direct counterparts to most of the functions in C’s standard math library.The Python functions have the same name as their C counterparts with three exceptions:
fabs
in C corresponds toabs
in Pythontgamma
in C corresponds togamma
in Pythonlgamma
in C corresponds togammaln
in Python
The function abs
is at the top level of Python. Aside from this exception, all mathematical functions are found in math
, scipy
, or scipy.special
.
All functions in math
have counterparts in scipy
with the same name. However, the scipy
version may behave differently. For example, math.sqrt(-1)
returns a domain error but scipy.sqrt(-1)
returns 1j
.
Python (specifically SciPy) has far more functions than C. The following chart lists Python counterparts for C functions in math.h
.
Function | Python counterpart |
---|---|
acos | math.acos |
acosh | math.acosh |
asin | math.asin |
asinh | math.asinh |
atan | math.atan |
atan2 | math.atan2 |
atanh | math.atanh |
ceil | math.ceil |
copysign | math.copysign |
cos | math.cos |
cosh | math.cosh |
erf | scipy.special.erf |
erfc | scipy.special.erfc |
exp | math.exp |
exp2 | scipy.exp2 |
expm1 | scipy.special.expm1 |
fabs | abs |
fmax | scipy.fmax |
fmin | scipy.fmin |
fmod | scipy.fmod |
hypot | scipy.hypot |
isfinite | ! scipy.lisinf |
isinf | scipy.isinf |
isnan | scipy.isnan |
j0 | scipy.special.j0 |
j1 | scipy.special.j1 |
jn | scipy.special.jn |
ldexp | math.ldexp |
lgamma | scipy.special.gammaln |
log | math.log |
log10 | math.log10 |
log1p | math.log1p |
log2 | scipy.log2 |
nan | scipy.nan |
pow | math.pow |
remainder | scipy.remainder |
sin | math.sin |
sinh | math.sinh |
sqrt | math.sqrt |
tan | math.tan |
tanh | math.tanh |
tgamma | scipy.special.gamma |
y0 | scipy.special.y0 |
y1 | scipy.special.y1 |
yn | scipy.special.yn |