Functional Weave
Code in TypeScript

math.exp@1.0.1

README.md

1,519 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).

## Notices

Derived from fdlibm e_exp.c.
Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Developed at SunSoft, a Sun Microsystems, Inc. business.
Permission to use, copy, modify, and distribute this
software is freely granted, provided that this notice
is preserved.

The same notice heads each implementation file.

1.0.1 adds its attribution notices (NOTICE). The code is unchanged apart from that notice at the top of each implementation file; the tests are unchanged.