Average Error: 29.9 → 0.1
Time: 10.1s
Precision: binary64
Cost: 26436
\[\left(e^{x} - 2\right) + e^{-x} \]
\[\begin{array}{l} \mathbf{if}\;\left(e^{x} + -2\right) + e^{-x} \leq 2 \cdot 10^{-5}:\\ \;\;\;\;x \cdot \mathsf{fma}\left(0.08333333333333333, {x}^{3}, x\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))) 2e-5)
   (* x (fma 0.08333333333333333 (pow x 3.0) 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)) <= 2e-5) {
		tmp = x * fma(0.08333333333333333, pow(x, 3.0), 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))) <= 2e-5)
		tmp = Float64(x * fma(0.08333333333333333, (x ^ 3.0), 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], 2e-5], N[(x * N[(0.08333333333333333 * N[Power[x, 3.0], $MachinePrecision] + x), $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 2 \cdot 10^{-5}:\\
\;\;\;\;x \cdot \mathsf{fma}\left(0.08333333333333333, {x}^{3}, x\right)\\

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


\end{array}

Error

Target

Original29.9
Target0.0
Herbie0.1
\[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))) < 2.00000000000000016e-5

    1. Initial program 30.3

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

      \[\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, 7 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. Taylor expanded in x around 0 0.0

      \[\leadsto \color{blue}{{x}^{2} + 0.08333333333333333 \cdot {x}^{4}} \]
    5. Simplified0.0

      \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(0.08333333333333333, {x}^{3}, x\right)} \]
      Proof
      (*.f64 x (fma.f64 1/12 (pow.f64 x 3) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 x (fma.f64 1/12 (pow.f64 x (Rewrite<= metadata-eval (/.f64 6 2))) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 x (Rewrite<= fma-def_binary64 (+.f64 (*.f64 1/12 (pow.f64 x (/.f64 6 2))) x))): 0 points increase in error, 0 points decrease in error
      (*.f64 x (+.f64 (*.f64 1/12 (pow.f64 x (Rewrite=> metadata-eval 3))) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 x (+.f64 (*.f64 1/12 (Rewrite=> unpow3_binary64 (*.f64 (*.f64 x x) x))) x)): 0 points increase in error, 1 points decrease in error
      (*.f64 x (+.f64 (*.f64 1/12 (*.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) x)) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 x (+.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 1/12 (pow.f64 x 2)) x)) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 x (Rewrite=> distribute-lft1-in_binary64 (*.f64 (+.f64 (*.f64 1/12 (pow.f64 x 2)) 1) x))): 3 points increase in error, 0 points decrease in error
      (*.f64 x (Rewrite<= distribute-rgt1-in_binary64 (+.f64 x (*.f64 (*.f64 1/12 (pow.f64 x 2)) x)))): 0 points increase in error, 3 points decrease in error
      (Rewrite<= distribute-rgt-out_binary64 (+.f64 (*.f64 x x) (*.f64 (*.f64 (*.f64 1/12 (pow.f64 x 2)) x) x))): 5 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) (*.f64 (*.f64 (*.f64 1/12 (pow.f64 x 2)) x) x)): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (Rewrite<= associate-*r*_binary64 (*.f64 (*.f64 1/12 (pow.f64 x 2)) (*.f64 x x)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (*.f64 (*.f64 1/12 (pow.f64 x 2)) (Rewrite<= unpow2_binary64 (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (Rewrite<= associate-*r*_binary64 (*.f64 1/12 (*.f64 (pow.f64 x 2) (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (*.f64 1/12 (Rewrite=> pow-sqr_binary64 (pow.f64 x (*.f64 2 2))))): 1 points increase in error, 0 points decrease in error
      (+.f64 (pow.f64 x 2) (*.f64 1/12 (pow.f64 x (Rewrite=> metadata-eval 4)))): 0 points increase in error, 0 points decrease in error

    if 2.00000000000000016e-5 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x)))

    1. Initial program 3.6

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

      \[\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, 7 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-rr3.7

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

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

Alternatives

Alternative 1
Error0.5
Cost26688
\[0.002777777777777778 \cdot {x}^{6} + \left({x}^{2} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right) \]
Alternative 2
Error0.6
Cost26176
\[\mathsf{fma}\left(x, x, \mathsf{fma}\left(0.002777777777777778, {x}^{6}, 0.08333333333333333 \cdot {x}^{4}\right)\right) \]
Alternative 3
Error0.6
Cost13632
\[0.08333333333333333 \cdot {x}^{4} + \left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right) \]
Alternative 4
Error0.7
Cost6912
\[0.08333333333333333 \cdot {x}^{4} + x \cdot x \]
Alternative 5
Error1.0
Cost192
\[x \cdot x \]
Alternative 6
Error60.3
Cost128
\[-x \]

Error

Reproduce

herbie shell --seed 2022297 
(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))))