The math standard library module provides C-level speed for logarithmic, trigonometric, hyperbolic, and rounding functions on real floating-point numbers.
flowchart LR
A["math Module"] --> B["Constants: pi, e, tau, inf, nan"]
A --> C["Trigonometry: sin, cos, tan, asin, acos, atan2"]
A --> D["Logs & Exponents: log, log10, log2, exp, pow"]
A --> E["Number Theory: gcd, lcm, factorial, comb, perm"]
import math
def advanced_math_demo() -> None:
# Number Theory & Combinatorics
n_items = 5
k_choices = 2
combinations = math.comb(n_items, k_choices)
permutations = math.perm(n_items, k_choices)
gcd_val = math.gcd(24, 36)
print(f"5 Choose 2 Combinations: {combinations}")
print(f"5 Pick 2 Permutations : {permutations}")
print(f"GCD of 24 and 36 : {gcd_val}")
# Logarithms & Exponents
val = 100.0
print(f"ln({val}) = {math.log(val):.4f}")
print(f"log10({val}) = {math.log10(val):.4f}")
print(f"log2({val}) = {math.log2(val):.4f}")
# Special Constants & Checks
print(f"math.isnan(float('nan')): {math.isnan(float('nan'))}")
print(f"math.isinf(math.inf) : {math.isinf(math.inf)}")
if __name__ == "__main__":
advanced_math_demo()
math module does not support complex numbers (3+4j). Use cmath for complex domain math.math trigonometric functions (sin, cos, tan) expect angles in radians. Convert degrees using math.radians(deg).math.comb: Use math.comb(n, k) instead of manually writing factorial divisions.Write a function calculate_compound_interest(principal: float, rate: float, years: int) -> float using math.pow().
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With