tanh Software Implementation

From emmtrix Wiki
Jump to navigation Jump to search

Approximation with exp (Version 1)

Id Formula Highest ULP ULP <= 2.0 NaN
exp_v1 x < 0: 1.6777e+07 at -7.45047e-09 with 0 instead of -7.45046691e-09

x > 0: 1.6777e+07 at 2.35095e-38 with 0 instead of 2.35095283e-38

-88.7246 < x < -0.125652

0.125599 < x < 88.7246

-inf < x < -88.7246

88.7246 < x < inf

exp_v2 x > 0: 1.67772e+07 at -4.65661e-10 with 0 instead of -4.6566126e-10

x < 0: 1.6777e+07 at 2.35095e-38 with 0 instead of 2.35095283e-38

-inf < x < -0.547821

0.254822 < x < 8.31787 8.38379 < x < 44.3623

44.3623 < x < inf
exp_v3 x < 0: 2.09713e+07 at -4.47044e-08 with -1.1920929e-07 instead of -4.47043895e-08

x > 0: 1.6777e+07 at 2.35095e-38 with 0 instead of 2.35095283e-38

-inf < x < -6.26233

0.346382 < x < inf

-
expm1 x < 0: 2.47839 at -3.56512 with -0.998400092 instead of -0.99840024

x > 0: 2.26089 at 0.0155272 with 0.0155259985 instead of 0.0155260007

-inf < x < -5.57227

-0.42981 < x < 0.00777948

0.125351 < x < 44.3623

44.3623 < x < inf

The first equation is the definition of the hyperbolic tangent function. This equation requires two exponentials with different values. Since exponentials are expensive to compute, we can use the second equation to reduce the number of exponentials to one.

The second equation has the drawback that both the numerator and the denominator can become infinity for large values of x which results in NaN. That happens for .

The third equation solves this problem but has a slightly higher error for . In the following graph you can see the error chart for equation 3. The error charts for the other two equations are very similar. All three equations have an error of up to 16 mio ULPs for which means that up to 24 bits could be wrong.

Taylor

Localized Approximation Functions

Id Formula ULP <= 2.0 NAN
signum -inf < x < -8.31763

8.31763 < x < inf

-
poly1 -0.000704229 < x < 0.000704229 -
poly3 -0.0291781 < x < 0.0291781 -
lampert7 -0.000947416 < x < 0.000947207 -inf < x < -1.51629e+06

1.51629e+06 < x < inf

pade -0.0016973 < x < 0.0016973 -inf < x < -6.14896e+18

6.14896e+18 < x < inf

External Links