Functional Weave
Code in Python

math.exp@1.0.0

README.md

1,028 bytes · view raw

# math.exp

e^x computed with nothing but `+`, `-`, `*` and `/`, so TypeScript, Python
and Rust return the same double for the same input. See `math.ln` for why the
platform's `exp` is not good enough when three languages must agree to the bit.

## Method

1. x = k ln 2 + r with k = floor(x / ln 2 + 1/2) and |r| <= ln 2 / 2, reduced
   with ln 2 in two parts (Cody and Waite) so the remainder keeps its digits.
2. e^r by its Taylor series to r^16 / 16!, by Horner's rule; the coefficients
   are correctly rounded divisions of exact integers.
3. Multiply by 2^k by exact doubling or halving.

The result is within 1 unit in the last place of the true value.

x must lie between -708 and 709, where e^x is a normal double; outside that the
answer would be infinity or lose precision as a subnormal, and it is an error
instead. (`math.pow` treats results below the normal range as 0.)

Sources: W. J. Cody and W. Waite, *Software Manual for the Elementary
Functions* (1980); Sun Microsystems fdlibm, `e_exp.c` (the ln 2 split).