Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame^] | 1 | /* Definitions of some C99 math library functions, for those platforms |
| 2 | that don't implement these functions already. */ |
| 3 | |
| 4 | #include <float.h> |
| 5 | #include <math.h> |
| 6 | |
| 7 | /* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed |
| 8 | to avoid the significant loss of precision that arises from direct |
| 9 | evaluation of the expression exp(x) - 1, for x near 0. */ |
| 10 | |
| 11 | double |
| 12 | _Py_expm1(double x) |
| 13 | { |
| 14 | /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this |
| 15 | also works fine for infinities and nans. |
| 16 | |
| 17 | For smaller x, we can use a method due to Kahan that achieves close to |
| 18 | full accuracy. |
| 19 | */ |
| 20 | |
| 21 | if (fabs(x) < 0.7) { |
| 22 | double u; |
| 23 | u = exp(x); |
| 24 | if (u == 1.0) |
| 25 | return x; |
| 26 | else |
| 27 | return (u - 1.0) * x / log(u); |
| 28 | } |
| 29 | else |
| 30 | return exp(x) - 1.0; |
| 31 | } |