Python: Math Library Function Manual (Other Common Functions)

In addition to common power, logarithmic, trigonometric, and hyperbolic functions, the math library also provides some practical basic mathematical tool functions, including factorial, product, greatest common divisor, least common multiple, combinations, floating-point comparison, and special value judgment. These functions are very common in number theory, combinatorial mathematics, algorithm design, and engineering calculations.

math.factorial(n)

Returns n! (factorial).

Parameters:

n: Non-negative integer.

Return value: Integer.

Note:

When n is too large, the result can be very large, but Python’s integers are of arbitrary precision and will not overflow. If n < 0, a ValueError will be raised.

import math
print("factorial(5) =", math.factorial(5))  # 120

math.prod(iterable, *, start=1)

Returns the product of all elements in the iterable. Python 3.8+.

Parameters:

iterable: An iterable object (such as a list, tuple, or generator).

start: Initial value (default 1), the result will be multiplied by this value.

Return value: Numeric (int or float).

Note:

If the sequence is empty, the return value is start (default 1).

More efficient than functools.reduce(operator.mul, …).

import math
print("prod([2,3,4]) =", math.prod([2, 3, 4]))          # 24
print("prod([2,3,4], start=10) =", math.prod([2, 3, 4], start=10)) # 240
print("prod([]) =", math.prod([]))  # 1

math.gcd(a, b)

Returns the greatest common divisor (GCD) of two integers.

Parameters:

a: Integer.

b: Integer.

Return value: Integer.

import math
print("gcd(48, 18) =", math.gcd(48, 18))  # 6

math.lcm(*integers)

Returns the least common multiple (LCM) of a set of integers. Python 3.9+.

Parameters:

integers: One or more integers.

Return value: Integer.

Note:

If no parameters are provided, returns 1. If any parameter is 0, the result is 0.

import math
print("lcm(4, 6) =", math.lcm(4, 6))           # 12
print("lcm(3, 5, 10) =", math.lcm(3, 5, 10))   # 30
print("lcm() =", math.lcm())                   # 1

math.comb(n, k)

Returns the combination C(n, k). That is, the number of ways to choose k items from n items without regard to the order. Python 3.8+.

Parameters:

n: Non-negative integer.

k: Non-negative integer.

Return value: Integer.

Note:

If k > n, returns 0 without error.

import math
print(math.comb(5, 2))  # 10, choosing 2 from 5 elements
print(math.comb(6, 0))  # 1, choosing 0 elements
print(math.comb(3, 5))  # 0, impossible to choose

math.perm(n, k=None)

Returns the permutation P(n, k). That is, the number of ways to arrange k items from n items. Python 3.8+.

Parameters:

n: Non-negative integer.

k: Non-negative integer (optional, defaults to n if omitted).

Return value: Integer.

import math
print(math.perm(5, 2))  # 20, arranging 2 from 5 elements
print(math.perm(5))     # 120, full arrangement of 5!
print(math.perm(3, 5))  # 0, impossible to choose

math.isfinite(x)

Determines whether x is a finite number (not infinity, not NaN).

Parameters:

x: Numeric value.

Return value: Boolean.

import math
print("isfinite(3.14) =", math.isfinite(3.14))   # True
print("isfinite(math.inf) =", math.isfinite(math.inf))  # False

math.isinf(x)

Determines whether x is infinite.

Parameters:

x: Numeric value.

Return value: Boolean.

import math
print("isinf(math.inf) =", math.isinf(math.inf))  # True

math.isnan(x)

Determines whether x is NaN (not a number).

Parameters:

x: Numeric value.

Return value: Boolean.

import math
print("isnan(math.nan) =", math.isnan(math.nan))  # True

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

Determines whether two floating-point numbers are close.

Parameters:

a, b: Numeric values to compare.

rel_tol: Relative tolerance (default 1e-09).

abs_tol: Absolute tolerance (default 0.0).

Return value: Boolean.

Note:

Recommended for floating-point comparisons to avoid precision traps like 0.1 + 0.2 != 0.3.

import math
print("isclose(0.1+0.2, 0.3) =", math.isclose(0.1+0.2, 0.3))  # True

math.ulp(x)

Returns the smallest positive interval (ULP, Unit in the Last Place) of the floating-point number x in its binary representation. Python 3.9+.

Parameters:

x: Floating-point number.

Return value: Floating-point number (in scientific notation).

Note:

Commonly used for numerical precision and stability analysis. The ULP also varies for very large or very small x.

import math
print("ulp(1.0) =", math.ulp(1.0))       # 2.220446049250313e-16
print("ulp(1000.0) =", math.ulp(1000.0)) # 1.1368683772161603e-13
print("ulp(0.0) =", math.ulp(0.0))       # 5e-324
print("ulp(inf) =", math.ulp(math.inf))  # inf
print("ulp(nan) =", math.ulp(math.nan))  # nan

Python: Math Library Function Manual (Other Common Functions)Likes are meaningful, appreciation is encouragement

Leave a Comment