Average Error: 58.2 → 0.7
Time: 4.2s
Precision: 64
\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
\[\mathsf{expm1}\left(x + x\right) \cdot \frac{1}{e^{2 \cdot x} + 1}\]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\mathsf{expm1}\left(x + x\right) \cdot \frac{1}{e^{2 \cdot x} + 1}
double code(double x) {
	return ((exp(x) - exp(-x)) / (exp(x) + exp(-x)));
}
double code(double x) {
	return (expm1((x + x)) * (1.0 / (exp((2.0 * x)) + 1.0)));
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Initial program 58.2

    \[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
  2. Simplified0.6

    \[\leadsto \color{blue}{1 \cdot \frac{\mathsf{expm1}\left(x + x\right)}{\mathsf{fma}\left(1, 1, e^{x + x}\right)}}\]
  3. Using strategy rm
  4. Applied div-inv0.7

    \[\leadsto 1 \cdot \color{blue}{\left(\mathsf{expm1}\left(x + x\right) \cdot \frac{1}{\mathsf{fma}\left(1, 1, e^{x + x}\right)}\right)}\]
  5. Simplified0.7

    \[\leadsto 1 \cdot \left(\mathsf{expm1}\left(x + x\right) \cdot \color{blue}{\frac{1}{e^{2 \cdot x} + 1}}\right)\]
  6. Final simplification0.7

    \[\leadsto \mathsf{expm1}\left(x + x\right) \cdot \frac{1}{e^{2 \cdot x} + 1}\]

Reproduce

herbie shell --seed 2020103 +o rules:numerics
(FPCore (x)
  :name "Hyperbolic tangent"
  :precision binary64
  (/ (- (exp x) (exp (- x))) (+ (exp x) (exp (- x)))))