Functional Weave
Code in Rust

math.pow@1.0.0

README.md

1,109 bytes · view raw

# math.pow

`base` to the power `exponent`, bit-identical in TypeScript, Python and Rust.

- **Whole-number exponents** are done by repeated squaring, so `pow(10, 3)`
  is exactly `1000` and `pow(-2, 3)` is `-8`. A negative whole exponent is
  the reciprocal. `exp(3 x ln 10)` would give 999.9999999999998.
- **Other exponents** are `exp(exponent x ln(base))`, from `math.exp` and
  `math.ln`, which use only IEEE-exact operations. The error grows with the
  size of `exponent x ln(base)`: within a few dozen units in the last place for
  everyday arguments (cube roots, the sRGB gamma 2.4), which is far below any
  rounding a caller applies, and identical in every language, which is the
  point.

Edge cases: `0^0` is 1, `0^positive` is 0, `0^negative` is an error. A
negative base needs a whole-number exponent (the real answer does not exist
otherwise). A result too large for a double is an error; one smaller than the
smallest normal double (about 1e-308) is 0.

`**`, `Math.pow`, `math.pow` and `f64::powf` are not used because they come
from each platform's maths library, whose last bit may differ.