|
| 1 | +# math Module Complexity |
| 2 | + |
| 3 | +The `math` module provides fast floating-point math functions and a few integer-specific helpers. |
| 4 | + |
| 5 | +## Complexity Reference |
| 6 | + |
| 7 | +| Operation | Time | Space | Notes | |
| 8 | +|-----------|------|-------|-------| |
| 9 | +| `math.sqrt(x)` | O(1) | O(1) | Floating-point operation | |
| 10 | +| `math.log(x[, base])` | O(1) | O(1) | Floating-point operation | |
| 11 | +| `math.sin/cos/tan(x)` | O(1) | O(1) | Floating-point operation | |
| 12 | +| `math.hypot(*coords)` | O(n) | O(1) | n = number of coordinates | |
| 13 | +| `math.fsum(iterable)` | O(n) | O(1) | Accurate summation of n values | |
| 14 | +| `math.isclose(a, b)` | O(1) | O(1) | Floating-point comparison | |
| 15 | +| `math.gcd(a, b)` | Varies | O(1) | Depends on integer size | |
| 16 | +| `math.lcm(a, b)` | Varies | O(1) | Depends on integer size | |
| 17 | +| `math.factorial(n)` | Varies | Varies | Big-int cost grows with n | |
| 18 | +| `math.comb(n, k)` | Varies | Varies | Big-int cost grows with n and k | |
| 19 | +| `math.perm(n, k)` | Varies | Varies | Big-int cost grows with n and k | |
| 20 | + |
| 21 | +!!! warning "Floating-point precision" |
| 22 | + Most `math` functions operate on binary floating-point numbers. Results are fast but may be imprecise |
| 23 | + for some decimal fractions. Use `decimal` for exact decimal arithmetic. |
| 24 | + |
| 25 | +## Common Operations |
| 26 | + |
| 27 | +### Basic Transcendentals |
| 28 | + |
| 29 | +```python |
| 30 | +import math |
| 31 | + |
| 32 | +x = 2.0 |
| 33 | + |
| 34 | +# O(1) |
| 35 | +root = math.sqrt(x) |
| 36 | +log2 = math.log(x, 2) |
| 37 | +angle = math.sin(math.pi / 6) |
| 38 | +``` |
| 39 | + |
| 40 | +### Hypotenuse and Distance |
| 41 | + |
| 42 | +```python |
| 43 | +import math |
| 44 | + |
| 45 | +# O(n) in number of coordinates |
| 46 | +r2 = math.hypot(3.0, 4.0) # 5.0 |
| 47 | +r3 = math.hypot(1.0, 2.0, 2.0) # 3.0 |
| 48 | +``` |
| 49 | + |
| 50 | +### Accurate Summation |
| 51 | + |
| 52 | +```python |
| 53 | +import math |
| 54 | + |
| 55 | +values = [1e100, 1.0, -1e100] |
| 56 | + |
| 57 | +# Regular sum may lose precision |
| 58 | +regular = sum(values) # 0.0 |
| 59 | + |
| 60 | +# fsum keeps extra precision |
| 61 | +accurate = math.fsum(values) # 1.0 |
| 62 | +``` |
| 63 | + |
| 64 | +### Integers: gcd, lcm, factorial, comb, perm |
| 65 | + |
| 66 | +```python |
| 67 | +import math |
| 68 | + |
| 69 | +# gcd/lcm |
| 70 | +g = math.gcd(84, 30) # 6 |
| 71 | +l = math.lcm(6, 10) # 30 |
| 72 | + |
| 73 | +# factorial, combinations, permutations |
| 74 | +f = math.factorial(10) # 3628800 |
| 75 | +c = math.comb(10, 3) # 120 |
| 76 | +p = math.perm(10, 3) # 720 |
| 77 | +``` |
| 78 | + |
| 79 | +### Robust Comparisons |
| 80 | + |
| 81 | +```python |
| 82 | +import math |
| 83 | + |
| 84 | +# Avoid direct equality for floats |
| 85 | +math.isclose(0.1 + 0.2, 0.3) # True |
| 86 | +``` |
| 87 | + |
| 88 | +## Performance Notes |
| 89 | + |
| 90 | +- Floating-point operations are typically constant time and CPU-bound. |
| 91 | +- Integer-heavy functions (`factorial`, `comb`, `perm`, `gcd`, `lcm`) scale with operand size. |
| 92 | +- `fsum` provides better accuracy with a small constant-factor cost over `sum`. |
| 93 | + |
| 94 | +## Related Modules |
| 95 | + |
| 96 | +- [cmath Module](cmath.md) - Complex-number math |
| 97 | +- [decimal Module](decimal.md) - Decimal arithmetic |
| 98 | +- [fractions Module](fractions.md) - Rational arithmetic |
| 99 | +- [statistics Module](statistics.md) - Descriptive statistics |
| 100 | +- [random Module](random.md) - Random numbers |
0 commit comments