Average Error: 29.7 → 0.2
Time: 10.6s
Precision: binary64
Cost: 20356
\[\left(e^{x} - 2\right) + e^{-x} \]
\[\begin{array}{l} \mathbf{if}\;\left(e^{x} + -2\right) + e^{-x} \leq 5 \cdot 10^{-10}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-2 + 2 \cdot \cosh x\\ \end{array} \]
(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
(FPCore (x)
 :precision binary64
 (if (<= (+ (+ (exp x) -2.0) (exp (- x))) 5e-10)
   (fma x x (* 0.08333333333333333 (* (* x x) (* x x))))
   (+ -2.0 (* 2.0 (cosh x)))))
double code(double x) {
	return (exp(x) - 2.0) + exp(-x);
}
double code(double x) {
	double tmp;
	if (((exp(x) + -2.0) + exp(-x)) <= 5e-10) {
		tmp = fma(x, x, (0.08333333333333333 * ((x * x) * (x * x))));
	} else {
		tmp = -2.0 + (2.0 * cosh(x));
	}
	return tmp;
}
function code(x)
	return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x)))
end
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) + -2.0) + exp(Float64(-x))) <= 5e-10)
		tmp = fma(x, x, Float64(0.08333333333333333 * Float64(Float64(x * x) * Float64(x * x))));
	else
		tmp = Float64(-2.0 + Float64(2.0 * cosh(x)));
	end
	return tmp
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] + -2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 5e-10], N[(x * x + N[(0.08333333333333333 * N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-2.0 + N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\left(e^{x} - 2\right) + e^{-x}
\begin{array}{l}
\mathbf{if}\;\left(e^{x} + -2\right) + e^{-x} \leq 5 \cdot 10^{-10}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;-2 + 2 \cdot \cosh x\\


\end{array}

Error

Target

Original29.7
Target0.0
Herbie0.2
\[4 \cdot {\sinh \left(\frac{x}{2}\right)}^{2} \]

Derivation

  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 5.00000000000000031e-10

    1. Initial program 30.2

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Simplified30.3

      \[\leadsto \color{blue}{-2 + \left(e^{x} + e^{-x}\right)} \]
      Proof
      (+.f64 -2 (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= metadata-eval (neg.f64 2)) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (neg.f64 2) (exp.f64 x)) (exp.f64 (neg.f64 x)))): 1 points increase in error, 3 points decrease in error
      (+.f64 (Rewrite<= +-commutative_binary64 (+.f64 (exp.f64 x) (neg.f64 2))) (exp.f64 (neg.f64 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= sub-neg_binary64 (-.f64 (exp.f64 x) 2)) (exp.f64 (neg.f64 x))): 0 points increase in error, 0 points decrease in error
    3. Taylor expanded in x around 0 0.0

      \[\leadsto \color{blue}{0.002777777777777778 \cdot {x}^{6} + \left({x}^{2} + 0.08333333333333333 \cdot {x}^{4}\right)} \]
    4. Simplified0

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, \mathsf{fma}\left(0.002777777777777778, {x}^{6}, 0.08333333333333333 \cdot {x}^{4}\right)\right)} \]
      Proof
      (fma.f64 x x (fma.f64 1/360 (pow.f64 x 6) (*.f64 1/12 (pow.f64 x 4)))): 0 points increase in error, 0 points decrease in error
      (fma.f64 x x (Rewrite<= fma-def_binary64 (+.f64 (*.f64 1/360 (pow.f64 x 6)) (*.f64 1/12 (pow.f64 x 4))))): 2 points increase in error, 0 points decrease in error
      (fma.f64 x x (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 1/12 (pow.f64 x 4)) (*.f64 1/360 (pow.f64 x 6))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x x) (+.f64 (*.f64 1/12 (pow.f64 x 4)) (*.f64 1/360 (pow.f64 x 6))))): 1 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) (+.f64 (*.f64 1/12 (pow.f64 x 4)) (*.f64 1/360 (pow.f64 x 6)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (pow.f64 x 2) (*.f64 1/12 (pow.f64 x 4))) (*.f64 1/360 (pow.f64 x 6)))): 1 points increase in error, 2 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 1/360 (pow.f64 x 6)) (+.f64 (pow.f64 x 2) (*.f64 1/12 (pow.f64 x 4))))): 0 points increase in error, 0 points decrease in error
    5. Taylor expanded in x around 0 0

      \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{0.08333333333333333 \cdot {x}^{4}}\right) \]
    6. Applied egg-rr0

      \[\leadsto \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)}\right) \]

    if 5.00000000000000031e-10 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x)))

    1. Initial program 8.6

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Simplified9.1

      \[\leadsto \color{blue}{-2 + \left(e^{x} + e^{-x}\right)} \]
      Proof
      (+.f64 -2 (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= metadata-eval (neg.f64 2)) (+.f64 (exp.f64 x) (exp.f64 (neg.f64 x)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (neg.f64 2) (exp.f64 x)) (exp.f64 (neg.f64 x)))): 1 points increase in error, 3 points decrease in error
      (+.f64 (Rewrite<= +-commutative_binary64 (+.f64 (exp.f64 x) (neg.f64 2))) (exp.f64 (neg.f64 x))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= sub-neg_binary64 (-.f64 (exp.f64 x) 2)) (exp.f64 (neg.f64 x))): 0 points increase in error, 0 points decrease in error
    3. Applied egg-rr9.1

      \[\leadsto -2 + \color{blue}{2 \cdot \cosh x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(e^{x} + -2\right) + e^{-x} \leq 5 \cdot 10^{-10}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-2 + 2 \cdot \cosh x\\ \end{array} \]

Alternatives

Alternative 1
Error0.8
Cost704
\[\left(x \cdot x\right) \cdot \left(0.08333333333333333 \cdot \left(x \cdot x\right) + 1\right) \]
Alternative 2
Error1.2
Cost192
\[x \cdot x \]
Alternative 3
Error60.2
Cost128
\[-x \]

Error

Reproduce

herbie shell --seed 2022308 
(FPCore (x)
  :name "exp2 (problem 3.3.7)"
  :precision binary64

  :herbie-target
  (* 4.0 (pow (sinh (/ x 2.0)) 2.0))

  (+ (- (exp x) 2.0) (exp (- x))))