Python counterparts for C math functions

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 to abs in Python
  • tgamma in C corresponds to gamma in Python
  • lgamma in C corresponds to gammaln 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.

FunctionPython counterpart
acosmath.acos
acoshmath.acosh
asinmath.asin
asinhmath.asinh
atanmath.atan
atan2math.atan2
atanhmath.atanh
ceilmath.ceil
copysignmath.copysign
cosmath.cos
coshmath.cosh
erfscipy.special.erf
erfcscipy.special.erfc
expmath.exp
exp2scipy.exp2
expm1scipy.special.expm1
fabsabs
fmaxscipy.fmax
fminscipy.fmin
fmodscipy.fmod
hypotscipy.hypot
isfinite! scipy.lisinf
isinfscipy.isinf
isnanscipy.isnan
j0scipy.special.j0
j1scipy.special.j1
jnscipy.special.jn
ldexpmath.ldexp
lgammascipy.special.gammaln
logmath.log
log10math.log10
log1pmath.log1p
log2scipy.log2
nanscipy.nan
powmath.pow
remainderscipy.remainder
sinmath.sin
sinhmath.sinh
sqrtmath.sqrt
tanmath.tan
tanhmath.tanh
tgammascipy.special.gamma
y0scipy.special.y0
y1scipy.special.y1
ynscipy.special.yn

 

Related resources