tanh Software Implementation
The hyperbolic tangent function, commonly referred to as tanh, is a mathematical function that maps real numbers to the range (−1,1). Defined as:
it is a smooth, continuous, and symmetric function centered at the origin. The tanh function is widely used in mathematics, physics, and engineering, particularly in areas involving hyperbolic geometry and signal processing.
In machine learning and deep learning, tanh is a popular activation function because it introduces non-linearity and maps inputs to a centered range around zero, helping to balance gradients during optimization. Its output approaches 1 for large positive inputs and −1 for large negative inputs, with steep transitions around x=0.
Simple Approximation with Exponential Math Function
Id | Formula | Highest ULP Error | ULP <= 2.0 | NaN |
---|---|---|---|---|
exp_v1 | x < 0: 8.55638e+08 at -2.98023e-08 with 0x0p+0 instead of -0x1p-25
x > 0: 8.55638e+08 at 2.98023e-08 with 0x0p+0 instead of 0x1p-25 |
-88.7246 < x < -7.14197
7.14197 < x < 88.7246 |
-inf < x < -88.7246
88.7246 < x < inf | |
exp_v2 | x < 0: 8.47249e+08 at -1.49012e-08 with 0x0p+0 instead of -0x1p-26
x > 0: 8.55638e+08 at 2.98023e-08 with 0x0p+0 instead of 0x1.fffffep-26 |
-inf < x < -0.547821
0.254822 < x < 8.31787 8.38379 < x < 44.3623 |
44.3623 < x < inf | |
exp_v3 | x < 0: 8.59832e+08 at -4.47035e-08 with 0x0p+0 instead of -0x1.8p-25
x > 0: 8.68221e+08 at 8.9407e-08 with 0x0p+0 instead of 0x1.7ffffcp-24 |
-inf < x < -6.26233
0.346382 < x < inf |
- | |
expm1 | x < 0: 2.49585 at -3.95627 with -0x1.ffa00ap-1 instead of -0x1.ffa00ep-1
x > 0: 2.41657 at 0.0155837 with 0x1.fe9b66p-7 instead of 0x1.fe9b62p-7 |
-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.
exp_v2
expm1
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 |