math

Numeric functions and constants for scientific and general-purpose computation.

Import

import math

Constants

math.PI / math.pi - pi (3.14159...)
math.E / math.e - Euler's number
math.TAU / math.tau - 2 * pi
math.INF / math.inf - positive infinity
math.NAN / math.nan - not-a-number

Functions

Trigonometry

sin, cos, tan, asin, acos, atan, atan2(y, x), sinh, cosh, tanh, asinh, acosh, atanh - standard trig and hyperbolic functions.

Exponents and logarithms

sqrt(x), cbrt(x), exp(x), expm1(x), log(x), log2(x), log10(x), log1p(x)

Rounding

floor(x), ceil(x), round(x), trunc(x)

Utility

  • abs(x) - absolute value
  • pow(x, y) - x raised to y
  • hypot(x, y) - Euclidean distance
  • gcd(a, b) - greatest common divisor
  • lcm(a, b) - least common multiple
  • factorial(n) - n!
  • clamp(x, lo, hi) - clamp x to [lo, hi]
  • lerp(a, b, t) - linear interpolation
  • sign(x) - -1, 0, or 1
  • degrees(r) / radians(d) - angle conversion
  • fmod(x, y) - floating-point remainder
  • modf(x) - integer and fractional parts
  • copysign(x, y) - x with sign of y
  • isclose(a, b) - approximate equality
  • frexp(x) / ldexp(m, e) - mantissa/exponent split and compose
  • comb(n, k) - n choose k
  • perm(n, k) - n permute k

Predicates

is_nan(x) - true if x is NaN
is_inf(x) - true if x is infinite

Aggregate

prod(arr), sum(arr), min(arr), max(arr), mean(arr) - operate on arrays.

Special functions

erf(x), erfc(x), gamma(x), lgamma(x)

Examples

scratch.xs