exp2 (problem 3.3.7)

Percentage Accurate: 77.0% → 100.0%
Time: 9.8s
Alternatives: 8
Speedup: 2.0×

Specification

?
\[\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: 77.0% 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: 100.0% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.01:\\ \;\;\;\;\mathsf{fma}\left(x, x, 0.002777777777777778 \cdot {x}^{6}\right) + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \cosh x - 2\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 0.01)
   (+
    (fma x x (* 0.002777777777777778 (pow x 6.0)))
    (+
     (* 0.08333333333333333 (pow x 4.0))
     (* 4.96031746031746e-5 (pow x 8.0))))
   (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 0.01) {
		tmp = fma(x, x, (0.002777777777777778 * pow(x, 6.0))) + ((0.08333333333333333 * pow(x, 4.0)) + (4.96031746031746e-5 * pow(x, 8.0)));
	} else {
		tmp = (2.0 * cosh(x)) - 2.0;
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 0.01)
		tmp = Float64(fma(x, x, Float64(0.002777777777777778 * (x ^ 6.0))) + Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(4.96031746031746e-5 * (x ^ 8.0))));
	else
		tmp = Float64(Float64(2.0 * cosh(x)) - 2.0);
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 0.01], N[(N[(x * x + N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(4.96031746031746e-5 * N[Power[x, 8.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.01:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.002777777777777778 \cdot {x}^{6}\right) + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 0.0100000000000000002

    1. Initial program 50.3%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-50.3%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg50.3%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in50.3%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg50.3%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{0.002777777777777778 \cdot {x}^{6} + \left({x}^{2} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
    5. Step-by-step derivation
      1. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.002777777777777778, {x}^{6}, {x}^{2} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      2. unpow2100.0%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \color{blue}{x \cdot x} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right) \]
      3. fma-def100.0%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)}\right) \]
      4. fma-def100.0%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)}\right)\right) \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)\right)} \]
    7. Step-by-step derivation
      1. fma-udef100.0%

        \[\leadsto \color{blue}{0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      2. fma-udef100.0%

        \[\leadsto 0.002777777777777778 \cdot {x}^{6} + \color{blue}{\left(x \cdot x + \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      3. associate-+r+100.0%

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

      \[\leadsto \color{blue}{\left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right) + \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)} \]
    9. Taylor expanded in x around 0 100.0%

      \[\leadsto \left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right) + \color{blue}{\left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)} \]
    10. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{\left(0.002777777777777778 \cdot {x}^{6} + {x}^{2}\right)} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) \]
    11. Step-by-step derivation
      1. unpow2100.0%

        \[\leadsto \left(0.002777777777777778 \cdot {x}^{6} + \color{blue}{x \cdot x}\right) + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) \]
      2. +-commutative100.0%

        \[\leadsto \color{blue}{\left(x \cdot x + 0.002777777777777778 \cdot {x}^{6}\right)} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) \]
      3. fma-udef100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, 0.002777777777777778 \cdot {x}^{6}\right)} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) \]
    12. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, 0.002777777777777778 \cdot {x}^{6}\right)} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) \]

    if 0.0100000000000000002 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x)))

    1. Initial program 100.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg100.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in100.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg100.0%

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

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

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

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

        \[\leadsto \color{blue}{2 \cdot \cosh x} + -2 \]
      3. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, \cosh x, -2\right)} \]
      4. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(2, \cosh x, \color{blue}{-2}\right) \]
      5. fma-neg100.0%

        \[\leadsto \color{blue}{2 \cdot \cosh x - 2} \]
    5. Applied egg-rr100.0%

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

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

Alternative 2: 100.0% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.01:\\ \;\;\;\;\left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) + \left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \cosh x - 2\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 0.01)
   (+
    (+ (* 0.08333333333333333 (pow x 4.0)) (* 4.96031746031746e-5 (pow x 8.0)))
    (+ (* 0.002777777777777778 (pow x 6.0)) (* x x)))
   (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 0.01) {
		tmp = ((0.08333333333333333 * pow(x, 4.0)) + (4.96031746031746e-5 * pow(x, 8.0))) + ((0.002777777777777778 * pow(x, 6.0)) + (x * x));
	} else {
		tmp = (2.0 * cosh(x)) - 2.0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (((exp(x) - 2.0d0) + exp(-x)) <= 0.01d0) then
        tmp = ((0.08333333333333333d0 * (x ** 4.0d0)) + (4.96031746031746d-5 * (x ** 8.0d0))) + ((0.002777777777777778d0 * (x ** 6.0d0)) + (x * x))
    else
        tmp = (2.0d0 * cosh(x)) - 2.0d0
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 0.01) {
		tmp = ((0.08333333333333333 * Math.pow(x, 4.0)) + (4.96031746031746e-5 * Math.pow(x, 8.0))) + ((0.002777777777777778 * Math.pow(x, 6.0)) + (x * x));
	} else {
		tmp = (2.0 * Math.cosh(x)) - 2.0;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if ((math.exp(x) - 2.0) + math.exp(-x)) <= 0.01:
		tmp = ((0.08333333333333333 * math.pow(x, 4.0)) + (4.96031746031746e-5 * math.pow(x, 8.0))) + ((0.002777777777777778 * math.pow(x, 6.0)) + (x * x))
	else:
		tmp = (2.0 * math.cosh(x)) - 2.0
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 0.01)
		tmp = Float64(Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(4.96031746031746e-5 * (x ^ 8.0))) + Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + Float64(x * x)));
	else
		tmp = Float64(Float64(2.0 * cosh(x)) - 2.0);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (((exp(x) - 2.0) + exp(-x)) <= 0.01)
		tmp = ((0.08333333333333333 * (x ^ 4.0)) + (4.96031746031746e-5 * (x ^ 8.0))) + ((0.002777777777777778 * (x ^ 6.0)) + (x * x));
	else
		tmp = (2.0 * cosh(x)) - 2.0;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 0.01], N[(N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(4.96031746031746e-5 * N[Power[x, 8.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.01:\\
\;\;\;\;\left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) + \left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 0.0100000000000000002

    1. Initial program 50.3%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-50.3%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg50.3%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in50.3%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg50.3%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{0.002777777777777778 \cdot {x}^{6} + \left({x}^{2} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
    5. Step-by-step derivation
      1. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.002777777777777778, {x}^{6}, {x}^{2} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      2. unpow2100.0%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \color{blue}{x \cdot x} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right) \]
      3. fma-def100.0%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)}\right) \]
      4. fma-def100.0%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)}\right)\right) \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)\right)} \]
    7. Step-by-step derivation
      1. fma-udef100.0%

        \[\leadsto \color{blue}{0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      2. fma-udef100.0%

        \[\leadsto 0.002777777777777778 \cdot {x}^{6} + \color{blue}{\left(x \cdot x + \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      3. associate-+r+100.0%

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

      \[\leadsto \color{blue}{\left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right) + \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)} \]
    9. Taylor expanded in x around 0 100.0%

      \[\leadsto \left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right) + \color{blue}{\left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)} \]

    if 0.0100000000000000002 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x)))

    1. Initial program 100.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg100.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in100.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg100.0%

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

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

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

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

        \[\leadsto \color{blue}{2 \cdot \cosh x} + -2 \]
      3. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, \cosh x, -2\right)} \]
      4. metadata-eval100.0%

        \[\leadsto \mathsf{fma}\left(2, \cosh x, \color{blue}{-2}\right) \]
      5. fma-neg100.0%

        \[\leadsto \color{blue}{2 \cdot \cosh x - 2} \]
    5. Applied egg-rr100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.01:\\ \;\;\;\;\left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right) + \left(0.002777777777777778 \cdot {x}^{6} + x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \cosh x - 2\\ \end{array} \]

Alternative 3: 99.5% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(e^{x} - 2\right) + e^{-x}\\ \mathbf{if}\;t_0 \leq 2 \cdot 10^{-16}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x)))))
   (if (<= t_0 2e-16) (* x x) t_0)))
double code(double x) {
	double t_0 = (exp(x) - 2.0) + exp(-x);
	double tmp;
	if (t_0 <= 2e-16) {
		tmp = x * x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (exp(x) - 2.0d0) + exp(-x)
    if (t_0 <= 2d-16) then
        tmp = x * x
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x) {
	double t_0 = (Math.exp(x) - 2.0) + Math.exp(-x);
	double tmp;
	if (t_0 <= 2e-16) {
		tmp = x * x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x):
	t_0 = (math.exp(x) - 2.0) + math.exp(-x)
	tmp = 0
	if t_0 <= 2e-16:
		tmp = x * x
	else:
		tmp = t_0
	return tmp
function code(x)
	t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x)))
	tmp = 0.0
	if (t_0 <= 2e-16)
		tmp = Float64(x * x);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x)
	t_0 = (exp(x) - 2.0) + exp(-x);
	tmp = 0.0;
	if (t_0 <= 2e-16)
		tmp = x * x;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2e-16], N[(x * x), $MachinePrecision], t$95$0]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 2 \cdot 10^{-16}:\\
\;\;\;\;x \cdot x\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2e-16

    1. Initial program 50.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-50.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg50.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in50.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg50.0%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    5. Step-by-step derivation
      1. unpow2100.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    6. Simplified100.0%

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

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

    1. Initial program 99.9%

      \[\left(e^{x} - 2\right) + e^{-x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

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

Alternative 4: 99.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -0.000185 \lor \neg \left(x \leq 0.000142\right):\\ \;\;\;\;2 \cdot \cosh x - 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (or (<= x -0.000185) (not (<= x 0.000142)))
   (- (* 2.0 (cosh x)) 2.0)
   (* x x)))
double code(double x) {
	double tmp;
	if ((x <= -0.000185) || !(x <= 0.000142)) {
		tmp = (2.0 * cosh(x)) - 2.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((x <= (-0.000185d0)) .or. (.not. (x <= 0.000142d0))) then
        tmp = (2.0d0 * cosh(x)) - 2.0d0
    else
        tmp = x * x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((x <= -0.000185) || !(x <= 0.000142)) {
		tmp = (2.0 * Math.cosh(x)) - 2.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x <= -0.000185) or not (x <= 0.000142):
		tmp = (2.0 * math.cosh(x)) - 2.0
	else:
		tmp = x * x
	return tmp
function code(x)
	tmp = 0.0
	if ((x <= -0.000185) || !(x <= 0.000142))
		tmp = Float64(Float64(2.0 * cosh(x)) - 2.0);
	else
		tmp = Float64(x * x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x <= -0.000185) || ~((x <= 0.000142)))
		tmp = (2.0 * cosh(x)) - 2.0;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
code[x_] := If[Or[LessEqual[x, -0.000185], N[Not[LessEqual[x, 0.000142]], $MachinePrecision]], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.000185 \lor \neg \left(x \leq 0.000142\right):\\
\;\;\;\;2 \cdot \cosh x - 2\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


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

    1. Initial program 99.9%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-99.9%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg99.9%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in99.9%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg99.9%

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

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

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

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

        \[\leadsto \color{blue}{2 \cdot \cosh x} + -2 \]
      3. fma-def99.9%

        \[\leadsto \color{blue}{\mathsf{fma}\left(2, \cosh x, -2\right)} \]
      4. metadata-eval99.9%

        \[\leadsto \mathsf{fma}\left(2, \cosh x, \color{blue}{-2}\right) \]
      5. fma-neg99.9%

        \[\leadsto \color{blue}{2 \cdot \cosh x - 2} \]
    5. Applied egg-rr99.9%

      \[\leadsto \color{blue}{2 \cdot \cosh x - 2} \]

    if -1.85e-4 < x < 1.42000000000000009e-4

    1. Initial program 50.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-50.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg50.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in50.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg50.0%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    5. Step-by-step derivation
      1. unpow2100.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -0.000185 \lor \neg \left(x \leq 0.000142\right):\\ \;\;\;\;2 \cdot \cosh x - 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 5: 96.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -5.2:\\ \;\;\;\;4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\\ \mathbf{elif}\;x \leq 1.65:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x -5.2)
   (* 4.96031746031746e-5 (pow x 8.0))
   (if (<= x 1.65) (* x x) (expm1 x))))
double code(double x) {
	double tmp;
	if (x <= -5.2) {
		tmp = 4.96031746031746e-5 * pow(x, 8.0);
	} else if (x <= 1.65) {
		tmp = x * x;
	} else {
		tmp = expm1(x);
	}
	return tmp;
}
public static double code(double x) {
	double tmp;
	if (x <= -5.2) {
		tmp = 4.96031746031746e-5 * Math.pow(x, 8.0);
	} else if (x <= 1.65) {
		tmp = x * x;
	} else {
		tmp = Math.expm1(x);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= -5.2:
		tmp = 4.96031746031746e-5 * math.pow(x, 8.0)
	elif x <= 1.65:
		tmp = x * x
	else:
		tmp = math.expm1(x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= -5.2)
		tmp = Float64(4.96031746031746e-5 * (x ^ 8.0));
	elseif (x <= 1.65)
		tmp = Float64(x * x);
	else
		tmp = expm1(x);
	end
	return tmp
end
code[x_] := If[LessEqual[x, -5.2], N[(4.96031746031746e-5 * N[Power[x, 8.0], $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.65], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.2:\\
\;\;\;\;4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\\

\mathbf{elif}\;x \leq 1.65:\\
\;\;\;\;x \cdot x\\

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -5.20000000000000018

    1. Initial program 100.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg100.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in100.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg100.0%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 92.2%

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(0.002777777777777778, {x}^{6}, {x}^{2} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)} \]
      2. unpow292.2%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \color{blue}{x \cdot x} + \left(0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right) \]
      3. fma-def92.2%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \color{blue}{\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)}\right) \]
      4. fma-def92.2%

        \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \color{blue}{\mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)}\right)\right) \]
    6. Simplified92.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \mathsf{fma}\left(0.08333333333333333, {x}^{4}, 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\right)\right)\right)} \]
    7. Taylor expanded in x around 0 92.2%

      \[\leadsto \mathsf{fma}\left(0.002777777777777778, {x}^{6}, \mathsf{fma}\left(x, x, \color{blue}{0.08333333333333333 \cdot {x}^{4} + 4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}}\right)\right) \]
    8. Taylor expanded in x around inf 92.2%

      \[\leadsto \color{blue}{4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}} \]

    if -5.20000000000000018 < x < 1.6499999999999999

    1. Initial program 50.7%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-50.7%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg50.7%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in50.7%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg50.7%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 98.9%

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

        \[\leadsto \color{blue}{x \cdot x} \]
    6. Simplified98.9%

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

    if 1.6499999999999999 < x

    1. Initial program 100.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg100.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in100.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg100.0%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto e^{x} + \color{blue}{-1} \]
    5. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{e^{x} - 1} \]
    6. Step-by-step derivation
      1. expm1-def100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(x\right)} \]
    7. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(x\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification97.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5.2:\\ \;\;\;\;4.96031746031746 \cdot 10^{-5} \cdot {x}^{8}\\ \mathbf{elif}\;x \leq 1.65:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]

Alternative 6: 87.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 1.65:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= x 1.65) (* x x) (expm1 x)))
double code(double x) {
	double tmp;
	if (x <= 1.65) {
		tmp = x * x;
	} else {
		tmp = expm1(x);
	}
	return tmp;
}
public static double code(double x) {
	double tmp;
	if (x <= 1.65) {
		tmp = x * x;
	} else {
		tmp = Math.expm1(x);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= 1.65:
		tmp = x * x
	else:
		tmp = math.expm1(x)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= 1.65)
		tmp = Float64(x * x);
	else
		tmp = expm1(x);
	end
	return tmp
end
code[x_] := If[LessEqual[x, 1.65], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.6499999999999999

    1. Initial program 66.3%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-66.3%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg66.3%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in66.3%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg66.3%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 83.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    5. Step-by-step derivation
      1. unpow283.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    6. Simplified83.0%

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

    if 1.6499999999999999 < x

    1. Initial program 100.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Step-by-step derivation
      1. associate-+l-100.0%

        \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
      2. sub-neg100.0%

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

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

        \[\leadsto e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]
      5. distribute-neg-in100.0%

        \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
      6. remove-double-neg100.0%

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto e^{x} + \color{blue}{-1} \]
    5. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{e^{x} - 1} \]
    6. Step-by-step derivation
      1. expm1-def100.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(x\right)} \]
    7. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.65:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]

Alternative 7: 76.3% accurate, 68.7× speedup?

\[\begin{array}{l} \\ x \cdot x \end{array} \]
(FPCore (x) :precision binary64 (* x x))
double code(double x) {
	return x * x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x * x
end function
public static double code(double x) {
	return x * x;
}
def code(x):
	return x * x
function code(x)
	return Float64(x * x)
end
function tmp = code(x)
	tmp = x * x;
end
code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}

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

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Step-by-step derivation
    1. associate-+l-75.5%

      \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
    2. sub-neg75.5%

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

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

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

      \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
    6. remove-double-neg75.5%

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

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

    \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
  4. Taylor expanded in x around 0 74.0%

    \[\leadsto \color{blue}{{x}^{2}} \]
  5. Step-by-step derivation
    1. unpow274.0%

      \[\leadsto \color{blue}{x \cdot x} \]
  6. Simplified74.0%

    \[\leadsto \color{blue}{x \cdot x} \]
  7. Final simplification74.0%

    \[\leadsto x \cdot x \]

Alternative 8: 4.4% accurate, 206.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x) :precision binary64 x)
double code(double x) {
	return x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x
end function
public static double code(double x) {
	return x;
}
def code(x):
	return x
function code(x)
	return x
end
function tmp = code(x)
	tmp = x;
end
code[x_] := x
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 75.5%

    \[\left(e^{x} - 2\right) + e^{-x} \]
  2. Step-by-step derivation
    1. associate-+l-75.5%

      \[\leadsto \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]
    2. sub-neg75.5%

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

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

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

      \[\leadsto e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]
    6. remove-double-neg75.5%

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

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

    \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
  4. Taylor expanded in x around 0 52.1%

    \[\leadsto e^{x} + \color{blue}{-1} \]
  5. Taylor expanded in x around 0 4.6%

    \[\leadsto \color{blue}{x} \]
  6. Final simplification4.6%

    \[\leadsto x \]

Developer target: 100.0% accurate, 1.0× speedup?

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

\\
4 \cdot {\sinh \left(\frac{x}{2}\right)}^{2}
\end{array}

Reproduce

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