Average Error: 58.1 → 0.6
Time: 5.8s
Precision: 64
\[\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\]
\[\frac{\mathsf{expm1}\left(x + x\right)}{e^{\mathsf{log1p}\left(e^{2 \cdot x}\right)}}\]
\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
\frac{\mathsf{expm1}\left(x + x\right)}{e^{\mathsf{log1p}\left(e^{2 \cdot x}\right)}}
double code(double x) {
	return ((exp(x) - exp(-x)) / (exp(x) + exp(-x)));
}
double code(double x) {
	return (expm1((x + x)) / exp(log1p(exp((2.0 * x)))));
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Initial program 58.1

    \[\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 add-exp-log0.6

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

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

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

Reproduce

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