exp2 (problem 3.3.7)

Percentage Accurate: 53.6% → 99.5%
Time: 10.0s
Alternatives: 8
Speedup: 68.7×

Specification

?
\[\left|x\right| \leq 710\]
\[\begin{array}{l} \\ \left(e^{x} - 2\right) + e^{-x} \end{array} \]
(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
double code(double x) {
	return (exp(x) - 2.0) + exp(-x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - 2.0d0) + exp(-x)
end function
public static double code(double x) {
	return (Math.exp(x) - 2.0) + Math.exp(-x);
}
def code(x):
	return (math.exp(x) - 2.0) + math.exp(-x)
function code(x)
	return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x)))
end
function tmp = code(x)
	tmp = (exp(x) - 2.0) + exp(-x);
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(e^{x} - 2\right) + e^{-x}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 8 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 53.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(e^{x} - 2\right) + e^{-x} \end{array} \]
(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
double code(double x) {
	return (exp(x) - 2.0) + exp(-x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - 2.0d0) + exp(-x)
end function
public static double code(double x) {
	return (Math.exp(x) - 2.0) + Math.exp(-x);
}
def code(x):
	return (math.exp(x) - 2.0) + math.exp(-x)
function code(x)
	return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x)))
end
function tmp = code(x)
	tmp = (exp(x) - 2.0) + exp(-x);
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(e^{x} - 2\right) + e^{-x}
\end{array}

Alternative 1: 99.5% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 0.000145:\\ \;\;\;\;x\_m \cdot x\_m\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \cosh x\_m - 2\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (if (<= x_m 0.000145) (* x_m x_m) (- (* 2.0 (cosh x_m)) 2.0)))
x_m = fabs(x);
double code(double x_m) {
	double tmp;
	if (x_m <= 0.000145) {
		tmp = x_m * x_m;
	} else {
		tmp = (2.0 * cosh(x_m)) - 2.0;
	}
	return tmp;
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    real(8) :: tmp
    if (x_m <= 0.000145d0) then
        tmp = x_m * x_m
    else
        tmp = (2.0d0 * cosh(x_m)) - 2.0d0
    end if
    code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	double tmp;
	if (x_m <= 0.000145) {
		tmp = x_m * x_m;
	} else {
		tmp = (2.0 * Math.cosh(x_m)) - 2.0;
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m):
	tmp = 0
	if x_m <= 0.000145:
		tmp = x_m * x_m
	else:
		tmp = (2.0 * math.cosh(x_m)) - 2.0
	return tmp
x_m = abs(x)
function code(x_m)
	tmp = 0.0
	if (x_m <= 0.000145)
		tmp = Float64(x_m * x_m);
	else
		tmp = Float64(Float64(2.0 * cosh(x_m)) - 2.0);
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m)
	tmp = 0.0;
	if (x_m <= 0.000145)
		tmp = x_m * x_m;
	else
		tmp = (2.0 * cosh(x_m)) - 2.0;
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := If[LessEqual[x$95$m, 0.000145], N[(x$95$m * x$95$m), $MachinePrecision], N[(N[(2.0 * N[Cosh[x$95$m], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 0.000145:\\
\;\;\;\;x\_m \cdot x\_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.45e-4

    1. Initial program 56.1%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 98.9%

      \[\leadsto \color{blue}{{x}^{2}} \]
    4. Step-by-step derivation
      1. unpow299.4%

        \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
    5. Applied egg-rr98.9%

      \[\leadsto \color{blue}{x \cdot x} \]

    if 1.45e-4 < x

    1. Initial program 94.6%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. +-commutative94.6%

        \[\leadsto \color{blue}{e^{-x} + \left(e^{x} - 2\right)} \]
      2. associate-+r-95.4%

        \[\leadsto \color{blue}{\left(e^{-x} + e^{x}\right) - 2} \]
      3. add-sqr-sqrt0.0%

        \[\leadsto \left(e^{\color{blue}{\sqrt{-x} \cdot \sqrt{-x}}} + e^{x}\right) - 2 \]
      4. sqrt-unprod16.6%

        \[\leadsto \left(e^{\color{blue}{\sqrt{\left(-x\right) \cdot \left(-x\right)}}} + e^{x}\right) - 2 \]
      5. sqr-neg16.6%

        \[\leadsto \left(e^{\sqrt{\color{blue}{x \cdot x}}} + e^{x}\right) - 2 \]
      6. sqrt-unprod16.6%

        \[\leadsto \left(e^{\color{blue}{\sqrt{x} \cdot \sqrt{x}}} + e^{x}\right) - 2 \]
      7. add-sqr-sqrt16.6%

        \[\leadsto \left(e^{\color{blue}{x}} + e^{x}\right) - 2 \]
      8. add-sqr-sqrt16.6%

        \[\leadsto \left(e^{x} + e^{\color{blue}{\sqrt{x} \cdot \sqrt{x}}}\right) - 2 \]
      9. sqrt-unprod16.6%

        \[\leadsto \left(e^{x} + e^{\color{blue}{\sqrt{x \cdot x}}}\right) - 2 \]
      10. sqr-neg16.6%

        \[\leadsto \left(e^{x} + e^{\sqrt{\color{blue}{\left(-x\right) \cdot \left(-x\right)}}}\right) - 2 \]
      11. sqrt-unprod0.0%

        \[\leadsto \left(e^{x} + e^{\color{blue}{\sqrt{-x} \cdot \sqrt{-x}}}\right) - 2 \]
      12. add-sqr-sqrt95.4%

        \[\leadsto \left(e^{x} + e^{\color{blue}{-x}}\right) - 2 \]
      13. cosh-undef95.4%

        \[\leadsto \color{blue}{2 \cdot \cosh x} - 2 \]
    4. Applied egg-rr95.4%

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

Alternative 2: 99.3% accurate, 0.7× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \mathsf{fma}\left(x\_m, x\_m, \mathsf{fma}\left(x\_m \cdot x\_m, 0.002777777777777778, 0.08333333333333333\right) \cdot {x\_m}^{4}\right) \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (fma
  x_m
  x_m
  (*
   (fma (* x_m x_m) 0.002777777777777778 0.08333333333333333)
   (pow x_m 4.0))))
x_m = fabs(x);
double code(double x_m) {
	return fma(x_m, x_m, (fma((x_m * x_m), 0.002777777777777778, 0.08333333333333333) * pow(x_m, 4.0)));
}
x_m = abs(x)
function code(x_m)
	return fma(x_m, x_m, Float64(fma(Float64(x_m * x_m), 0.002777777777777778, 0.08333333333333333) * (x_m ^ 4.0)))
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(x$95$m * x$95$m + N[(N[(N[(x$95$m * x$95$m), $MachinePrecision] * 0.002777777777777778 + 0.08333333333333333), $MachinePrecision] * N[Power[x$95$m, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
\mathsf{fma}\left(x\_m, x\_m, \mathsf{fma}\left(x\_m \cdot x\_m, 0.002777777777777778, 0.08333333333333333\right) \cdot {x\_m}^{4}\right)
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 98.8%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + 0.002777777777777778 \cdot {x}^{2}\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutative98.8%

      \[\leadsto {x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + \color{blue}{{x}^{2} \cdot 0.002777777777777778}\right)\right) \]
  5. Simplified98.8%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right)\right)} \]
  6. Step-by-step derivation
    1. distribute-rgt-in98.8%

      \[\leadsto \color{blue}{1 \cdot {x}^{2} + \left({x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right)\right) \cdot {x}^{2}} \]
    2. *-un-lft-identity98.8%

      \[\leadsto \color{blue}{{x}^{2}} + \left({x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right)\right) \cdot {x}^{2} \]
    3. unpow298.8%

      \[\leadsto \color{blue}{x \cdot x} + \left({x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right)\right) \cdot {x}^{2} \]
    4. fma-define98.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, \left({x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right)\right) \cdot {x}^{2}\right)} \]
    5. *-commutative98.8%

      \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{\left(\left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right) \cdot {x}^{2}\right)} \cdot {x}^{2}\right) \]
    6. associate-*l*98.8%

      \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{\left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right) \cdot \left({x}^{2} \cdot {x}^{2}\right)}\right) \]
    7. +-commutative98.8%

      \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{\left({x}^{2} \cdot 0.002777777777777778 + 0.08333333333333333\right)} \cdot \left({x}^{2} \cdot {x}^{2}\right)\right) \]
    8. fma-define98.8%

      \[\leadsto \mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left({x}^{2}, 0.002777777777777778, 0.08333333333333333\right)} \cdot \left({x}^{2} \cdot {x}^{2}\right)\right) \]
    9. pow-prod-up98.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left({x}^{2}, 0.002777777777777778, 0.08333333333333333\right) \cdot \color{blue}{{x}^{\left(2 + 2\right)}}\right) \]
    10. metadata-eval98.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left({x}^{2}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{\color{blue}{4}}\right) \]
  7. Applied egg-rr98.8%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, \mathsf{fma}\left({x}^{2}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right)} \]
  8. Step-by-step derivation
    1. unpow298.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
  9. Applied egg-rr98.8%

    \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
  10. Add Preprocessing

Alternative 3: 99.3% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ {x\_m}^{2} \cdot \left(1 + {x\_m}^{2} \cdot \left(0.08333333333333333 + \left(x\_m \cdot x\_m\right) \cdot 0.002777777777777778\right)\right) \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (*
  (pow x_m 2.0)
  (+
   1.0
   (*
    (pow x_m 2.0)
    (+ 0.08333333333333333 (* (* x_m x_m) 0.002777777777777778))))))
x_m = fabs(x);
double code(double x_m) {
	return pow(x_m, 2.0) * (1.0 + (pow(x_m, 2.0) * (0.08333333333333333 + ((x_m * x_m) * 0.002777777777777778))));
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = (x_m ** 2.0d0) * (1.0d0 + ((x_m ** 2.0d0) * (0.08333333333333333d0 + ((x_m * x_m) * 0.002777777777777778d0))))
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return Math.pow(x_m, 2.0) * (1.0 + (Math.pow(x_m, 2.0) * (0.08333333333333333 + ((x_m * x_m) * 0.002777777777777778))));
}
x_m = math.fabs(x)
def code(x_m):
	return math.pow(x_m, 2.0) * (1.0 + (math.pow(x_m, 2.0) * (0.08333333333333333 + ((x_m * x_m) * 0.002777777777777778))))
x_m = abs(x)
function code(x_m)
	return Float64((x_m ^ 2.0) * Float64(1.0 + Float64((x_m ^ 2.0) * Float64(0.08333333333333333 + Float64(Float64(x_m * x_m) * 0.002777777777777778)))))
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = (x_m ^ 2.0) * (1.0 + ((x_m ^ 2.0) * (0.08333333333333333 + ((x_m * x_m) * 0.002777777777777778))));
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(N[Power[x$95$m, 2.0], $MachinePrecision] * N[(1.0 + N[(N[Power[x$95$m, 2.0], $MachinePrecision] * N[(0.08333333333333333 + N[(N[(x$95$m * x$95$m), $MachinePrecision] * 0.002777777777777778), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
{x\_m}^{2} \cdot \left(1 + {x\_m}^{2} \cdot \left(0.08333333333333333 + \left(x\_m \cdot x\_m\right) \cdot 0.002777777777777778\right)\right)
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 98.8%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + 0.002777777777777778 \cdot {x}^{2}\right)\right)} \]
  4. Step-by-step derivation
    1. *-commutative98.8%

      \[\leadsto {x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + \color{blue}{{x}^{2} \cdot 0.002777777777777778}\right)\right) \]
  5. Simplified98.8%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + {x}^{2} \cdot 0.002777777777777778\right)\right)} \]
  6. Step-by-step derivation
    1. unpow298.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
  7. Applied egg-rr98.8%

    \[\leadsto {x}^{2} \cdot \left(1 + {x}^{2} \cdot \left(0.08333333333333333 + \color{blue}{\left(x \cdot x\right)} \cdot 0.002777777777777778\right)\right) \]
  8. Add Preprocessing

Alternative 4: 99.1% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \mathsf{fma}\left(x\_m, x\_m, 0.08333333333333333 \cdot {x\_m}^{4}\right) \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (fma x_m x_m (* 0.08333333333333333 (pow x_m 4.0))))
x_m = fabs(x);
double code(double x_m) {
	return fma(x_m, x_m, (0.08333333333333333 * pow(x_m, 4.0)));
}
x_m = abs(x)
function code(x_m)
	return fma(x_m, x_m, Float64(0.08333333333333333 * (x_m ^ 4.0)))
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(x$95$m * x$95$m + N[(0.08333333333333333 * N[Power[x$95$m, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
\mathsf{fma}\left(x\_m, x\_m, 0.08333333333333333 \cdot {x\_m}^{4}\right)
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 98.5%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + 0.08333333333333333 \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. distribute-rgt-in98.5%

      \[\leadsto \color{blue}{1 \cdot {x}^{2} + \left(0.08333333333333333 \cdot {x}^{2}\right) \cdot {x}^{2}} \]
    2. *-lft-identity98.5%

      \[\leadsto \color{blue}{{x}^{2}} + \left(0.08333333333333333 \cdot {x}^{2}\right) \cdot {x}^{2} \]
    3. associate-*l*98.5%

      \[\leadsto {x}^{2} + \color{blue}{0.08333333333333333 \cdot \left({x}^{2} \cdot {x}^{2}\right)} \]
    4. pow-sqr98.5%

      \[\leadsto {x}^{2} + 0.08333333333333333 \cdot \color{blue}{{x}^{\left(2 \cdot 2\right)}} \]
    5. metadata-eval98.5%

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

    \[\leadsto \color{blue}{{x}^{2} + 0.08333333333333333 \cdot {x}^{4}} \]
  6. Step-by-step derivation
    1. unpow298.5%

      \[\leadsto \color{blue}{x \cdot x} + 0.08333333333333333 \cdot {x}^{4} \]
    2. fma-define98.5%

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)} \]
  8. Add Preprocessing

Alternative 5: 99.1% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ {x\_m}^{2} \cdot \left(1 + \left(x\_m \cdot x\_m\right) \cdot 0.08333333333333333\right) \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (* (pow x_m 2.0) (+ 1.0 (* (* x_m x_m) 0.08333333333333333))))
x_m = fabs(x);
double code(double x_m) {
	return pow(x_m, 2.0) * (1.0 + ((x_m * x_m) * 0.08333333333333333));
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = (x_m ** 2.0d0) * (1.0d0 + ((x_m * x_m) * 0.08333333333333333d0))
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return Math.pow(x_m, 2.0) * (1.0 + ((x_m * x_m) * 0.08333333333333333));
}
x_m = math.fabs(x)
def code(x_m):
	return math.pow(x_m, 2.0) * (1.0 + ((x_m * x_m) * 0.08333333333333333))
x_m = abs(x)
function code(x_m)
	return Float64((x_m ^ 2.0) * Float64(1.0 + Float64(Float64(x_m * x_m) * 0.08333333333333333)))
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = (x_m ^ 2.0) * (1.0 + ((x_m * x_m) * 0.08333333333333333));
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(N[Power[x$95$m, 2.0], $MachinePrecision] * N[(1.0 + N[(N[(x$95$m * x$95$m), $MachinePrecision] * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
{x\_m}^{2} \cdot \left(1 + \left(x\_m \cdot x\_m\right) \cdot 0.08333333333333333\right)
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 98.5%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + 0.08333333333333333 \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. *-commutative98.5%

      \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{{x}^{2} \cdot 0.08333333333333333}\right) \]
  5. Simplified98.5%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + {x}^{2} \cdot 0.08333333333333333\right)} \]
  6. Step-by-step derivation
    1. unpow298.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
  7. Applied egg-rr98.5%

    \[\leadsto {x}^{2} \cdot \left(1 + \color{blue}{\left(x \cdot x\right)} \cdot 0.08333333333333333\right) \]
  8. Add Preprocessing

Alternative 6: 99.1% accurate, 1.9× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x\_m \cdot x\_m + 0.08333333333333333 \cdot {x\_m}^{4} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m)
 :precision binary64
 (+ (* x_m x_m) (* 0.08333333333333333 (pow x_m 4.0))))
x_m = fabs(x);
double code(double x_m) {
	return (x_m * x_m) + (0.08333333333333333 * pow(x_m, 4.0));
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = (x_m * x_m) + (0.08333333333333333d0 * (x_m ** 4.0d0))
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return (x_m * x_m) + (0.08333333333333333 * Math.pow(x_m, 4.0));
}
x_m = math.fabs(x)
def code(x_m):
	return (x_m * x_m) + (0.08333333333333333 * math.pow(x_m, 4.0))
x_m = abs(x)
function code(x_m)
	return Float64(Float64(x_m * x_m) + Float64(0.08333333333333333 * (x_m ^ 4.0)))
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = (x_m * x_m) + (0.08333333333333333 * (x_m ^ 4.0));
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(N[(x$95$m * x$95$m), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x$95$m, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
x\_m \cdot x\_m + 0.08333333333333333 \cdot {x\_m}^{4}
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 98.5%

    \[\leadsto \color{blue}{{x}^{2} \cdot \left(1 + 0.08333333333333333 \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. distribute-rgt-in98.5%

      \[\leadsto \color{blue}{1 \cdot {x}^{2} + \left(0.08333333333333333 \cdot {x}^{2}\right) \cdot {x}^{2}} \]
    2. *-lft-identity98.5%

      \[\leadsto \color{blue}{{x}^{2}} + \left(0.08333333333333333 \cdot {x}^{2}\right) \cdot {x}^{2} \]
    3. associate-*l*98.5%

      \[\leadsto {x}^{2} + \color{blue}{0.08333333333333333 \cdot \left({x}^{2} \cdot {x}^{2}\right)} \]
    4. pow-sqr98.5%

      \[\leadsto {x}^{2} + 0.08333333333333333 \cdot \color{blue}{{x}^{\left(2 \cdot 2\right)}} \]
    5. metadata-eval98.5%

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

    \[\leadsto \color{blue}{{x}^{2} + 0.08333333333333333 \cdot {x}^{4}} \]
  6. Step-by-step derivation
    1. unpow298.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
  7. Applied egg-rr98.5%

    \[\leadsto \color{blue}{x \cdot x} + 0.08333333333333333 \cdot {x}^{4} \]
  8. Add Preprocessing

Alternative 7: 98.5% accurate, 68.7× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x\_m \cdot x\_m \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m) :precision binary64 (* x_m x_m))
x_m = fabs(x);
double code(double x_m) {
	return x_m * x_m;
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = x_m * x_m
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return x_m * x_m;
}
x_m = math.fabs(x)
def code(x_m):
	return x_m * x_m
x_m = abs(x)
function code(x_m)
	return Float64(x_m * x_m)
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = x_m * x_m;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := N[(x$95$m * x$95$m), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
x\_m \cdot x\_m
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 98.0%

    \[\leadsto \color{blue}{{x}^{2}} \]
  4. Step-by-step derivation
    1. unpow298.8%

      \[\leadsto \mathsf{fma}\left(x, x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.002777777777777778, 0.08333333333333333\right) \cdot {x}^{4}\right) \]
  5. Applied egg-rr98.0%

    \[\leadsto \color{blue}{x \cdot x} \]
  6. Add Preprocessing

Alternative 8: 4.3% accurate, 206.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ 2 \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m) :precision binary64 2.0)
x_m = fabs(x);
double code(double x_m) {
	return 2.0;
}
x_m = abs(x)
real(8) function code(x_m)
    real(8), intent (in) :: x_m
    code = 2.0d0
end function
x_m = Math.abs(x);
public static double code(double x_m) {
	return 2.0;
}
x_m = math.fabs(x)
def code(x_m):
	return 2.0
x_m = abs(x)
function code(x_m)
	return 2.0
end
x_m = abs(x);
function tmp = code(x_m)
	tmp = 2.0;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := 2.0
\begin{array}{l}
x_m = \left|x\right|

\\
2
\end{array}
Derivation
  1. Initial program 56.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. flip--56.2%

      \[\leadsto \color{blue}{\frac{e^{x} \cdot e^{x} - 2 \cdot 2}{e^{x} + 2}} + e^{-x} \]
    2. clear-num56.1%

      \[\leadsto \color{blue}{\frac{1}{\frac{e^{x} + 2}{e^{x} \cdot e^{x} - 2 \cdot 2}}} + e^{-x} \]
    3. sub-neg56.1%

      \[\leadsto \frac{1}{\frac{e^{x} + 2}{\color{blue}{e^{x} \cdot e^{x} + \left(-2 \cdot 2\right)}}} + e^{-x} \]
    4. pow256.1%

      \[\leadsto \frac{1}{\frac{e^{x} + 2}{\color{blue}{{\left(e^{x}\right)}^{2}} + \left(-2 \cdot 2\right)}} + e^{-x} \]
    5. metadata-eval56.1%

      \[\leadsto \frac{1}{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + \left(-\color{blue}{4}\right)}} + e^{-x} \]
    6. metadata-eval56.1%

      \[\leadsto \frac{1}{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + \color{blue}{-4}}} + e^{-x} \]
  4. Applied egg-rr56.1%

    \[\leadsto \color{blue}{\frac{1}{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + -4}}} + e^{-x} \]
  5. Step-by-step derivation
    1. frac-2neg56.1%

      \[\leadsto \frac{1}{\color{blue}{\frac{-\left(e^{x} + 2\right)}{-\left({\left(e^{x}\right)}^{2} + -4\right)}}} + e^{-x} \]
    2. distribute-frac-neg56.1%

      \[\leadsto \frac{1}{\color{blue}{-\frac{e^{x} + 2}{-\left({\left(e^{x}\right)}^{2} + -4\right)}}} + e^{-x} \]
    3. distribute-neg-frac256.1%

      \[\leadsto \frac{1}{-\color{blue}{\left(-\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + -4}\right)}} + e^{-x} \]
    4. /-rgt-identity56.1%

      \[\leadsto \frac{1}{-\left(-\color{blue}{\frac{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + -4}}{1}}\right)} + e^{-x} \]
    5. clear-num56.1%

      \[\leadsto \frac{1}{-\left(-\color{blue}{\frac{1}{\frac{1}{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + -4}}}}\right)} + e^{-x} \]
    6. distribute-neg-frac56.1%

      \[\leadsto \frac{1}{-\color{blue}{\frac{-1}{\frac{1}{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + -4}}}}} + e^{-x} \]
    7. metadata-eval56.1%

      \[\leadsto \frac{1}{-\frac{\color{blue}{-1}}{\frac{1}{\frac{e^{x} + 2}{{\left(e^{x}\right)}^{2} + -4}}}} + e^{-x} \]
    8. clear-num56.2%

      \[\leadsto \frac{1}{-\frac{-1}{\color{blue}{\frac{{\left(e^{x}\right)}^{2} + -4}{e^{x} + 2}}}} + e^{-x} \]
    9. metadata-eval56.2%

      \[\leadsto \frac{1}{-\frac{-1}{\frac{{\left(e^{x}\right)}^{2} + \color{blue}{\left(-4\right)}}{e^{x} + 2}}} + e^{-x} \]
    10. sub-neg56.2%

      \[\leadsto \frac{1}{-\frac{-1}{\frac{\color{blue}{{\left(e^{x}\right)}^{2} - 4}}{e^{x} + 2}}} + e^{-x} \]
    11. unpow256.2%

      \[\leadsto \frac{1}{-\frac{-1}{\frac{\color{blue}{e^{x} \cdot e^{x}} - 4}{e^{x} + 2}}} + e^{-x} \]
    12. metadata-eval56.2%

      \[\leadsto \frac{1}{-\frac{-1}{\frac{e^{x} \cdot e^{x} - \color{blue}{2 \cdot 2}}{e^{x} + 2}}} + e^{-x} \]
    13. flip--56.5%

      \[\leadsto \frac{1}{-\frac{-1}{\color{blue}{e^{x} - 2}}} + e^{-x} \]
    14. sub-neg56.5%

      \[\leadsto \frac{1}{-\frac{-1}{\color{blue}{e^{x} + \left(-2\right)}}} + e^{-x} \]
    15. metadata-eval56.5%

      \[\leadsto \frac{1}{-\frac{-1}{e^{x} + \color{blue}{-2}}} + e^{-x} \]
  6. Applied egg-rr56.5%

    \[\leadsto \frac{1}{\color{blue}{-\frac{-1}{e^{x} + -2}}} + e^{-x} \]
  7. Step-by-step derivation
    1. +-commutative56.5%

      \[\leadsto \color{blue}{e^{-x} + \frac{1}{-\frac{-1}{e^{x} + -2}}} \]
    2. distribute-frac-neg256.5%

      \[\leadsto e^{-x} + \color{blue}{\left(-\frac{1}{\frac{-1}{e^{x} + -2}}\right)} \]
    3. clear-num56.5%

      \[\leadsto e^{-x} + \left(-\color{blue}{\frac{e^{x} + -2}{-1}}\right) \]
    4. unsub-neg56.5%

      \[\leadsto \color{blue}{e^{-x} - \frac{e^{x} + -2}{-1}} \]
    5. clear-num56.5%

      \[\leadsto e^{-x} - \color{blue}{\frac{1}{\frac{-1}{e^{x} + -2}}} \]
    6. add-sqr-sqrt55.6%

      \[\leadsto e^{-x} - \frac{1}{\color{blue}{\sqrt{\frac{-1}{e^{x} + -2}} \cdot \sqrt{\frac{-1}{e^{x} + -2}}}} \]
    7. sqrt-unprod55.8%

      \[\leadsto e^{-x} - \frac{1}{\color{blue}{\sqrt{\frac{-1}{e^{x} + -2} \cdot \frac{-1}{e^{x} + -2}}}} \]
    8. sqr-neg55.8%

      \[\leadsto e^{-x} - \frac{1}{\sqrt{\color{blue}{\left(-\frac{-1}{e^{x} + -2}\right) \cdot \left(-\frac{-1}{e^{x} + -2}\right)}}} \]
    9. sqrt-unprod0.1%

      \[\leadsto e^{-x} - \frac{1}{\color{blue}{\sqrt{-\frac{-1}{e^{x} + -2}} \cdot \sqrt{-\frac{-1}{e^{x} + -2}}}} \]
    10. add-sqr-sqrt4.6%

      \[\leadsto e^{-x} - \frac{1}{\color{blue}{-\frac{-1}{e^{x} + -2}}} \]
  8. Applied egg-rr4.2%

    \[\leadsto \color{blue}{e^{x} - \left(e^{x} + -2\right)} \]
  9. Step-by-step derivation
    1. associate--r+4.2%

      \[\leadsto \color{blue}{\left(e^{x} - e^{x}\right) - -2} \]
    2. +-inverses4.2%

      \[\leadsto \color{blue}{0} - -2 \]
    3. metadata-eval4.2%

      \[\leadsto \color{blue}{2} \]
  10. Simplified4.2%

    \[\leadsto \color{blue}{2} \]
  11. Add Preprocessing

Developer Target 1: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sinh \left(\frac{x}{2}\right)\\ 4 \cdot \left(t\_0 \cdot t\_0\right) \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (sinh (/ x 2.0)))) (* 4.0 (* t_0 t_0))))
double code(double x) {
	double t_0 = sinh((x / 2.0));
	return 4.0 * (t_0 * t_0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = sinh((x / 2.0d0))
    code = 4.0d0 * (t_0 * t_0)
end function
public static double code(double x) {
	double t_0 = Math.sinh((x / 2.0));
	return 4.0 * (t_0 * t_0);
}
def code(x):
	t_0 = math.sinh((x / 2.0))
	return 4.0 * (t_0 * t_0)
function code(x)
	t_0 = sinh(Float64(x / 2.0))
	return Float64(4.0 * Float64(t_0 * t_0))
end
function tmp = code(x)
	t_0 = sinh((x / 2.0));
	tmp = 4.0 * (t_0 * t_0);
end
code[x_] := Block[{t$95$0 = N[Sinh[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(4.0 * N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sinh \left(\frac{x}{2}\right)\\
4 \cdot \left(t\_0 \cdot t\_0\right)
\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2024149 
(FPCore (x)
  :name "exp2 (problem 3.3.7)"
  :precision binary64
  :pre (<= (fabs x) 710.0)

  :alt
  (! :herbie-platform default (* 4 (* (sinh (/ x 2)) (sinh (/ x 2)))))

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