Average Error: 58.3 → 2.1
Time: 4.4s
Precision: binary64
\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
\[\frac{\mathsf{fma}\left(2, x, 0.3333333333333333 \cdot {x}^{3}\right)}{e^{x} + e^{-x}} \]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\frac{\mathsf{fma}\left(2, x, 0.3333333333333333 \cdot {x}^{3}\right)}{e^{x} + e^{-x}}
(FPCore (x)
 :precision binary64
 (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))
(FPCore (x)
 :precision binary64
 (/ (fma 2.0 x (* 0.3333333333333333 (pow x 3.0))) (+ (exp x) (exp (- x)))))
double code(double x) {
	return (exp(x) - exp(-x)) / (exp(x) + exp(-x));
}
double code(double x) {
	return fma(2.0, x, (0.3333333333333333 * pow(x, 3.0))) / (exp(x) + exp(-x));
}

Error

Bits error versus x

Derivation

  1. Initial program 58.3

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \]
  2. Taylor expanded in x around 0 2.1

    \[\leadsto \frac{\color{blue}{0.3333333333333333 \cdot {x}^{3} + 2 \cdot x}}{e^{x} + e^{-x}} \]
  3. Simplified2.1

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(2, x, 0.3333333333333333 \cdot {x}^{3}\right)}}{e^{x} + e^{-x}} \]
  4. Final simplification2.1

    \[\leadsto \frac{\mathsf{fma}\left(2, x, 0.3333333333333333 \cdot {x}^{3}\right)}{e^{x} + e^{-x}} \]

Reproduce

herbie shell --seed 2022125 
(FPCore (x)
  :name "Hyperbolic tangent"
  :precision binary64
  (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))