exp2 (problem 3.3.7)

Percentage Accurate: 76.7% → 99.4%
Time: 8.3s
Alternatives: 9
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 9 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: 76.7% 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.4% accurate, 0.3× speedup?

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

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


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

    1. Initial program 52.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.0000000000000001e-4 < (+.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. distribute-neg-in100.0%

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

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

        \[\leadsto e^{x} + \color{blue}{\left(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 53.6%

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

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

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

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

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

Alternative 2: 99.4% accurate, 0.4× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.0002:\\ \;\;\;\;0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 0.0002)
   (+
    (* 0.002777777777777778 (pow x 6.0))
    (fma x x (* 0.08333333333333333 (pow x 4.0))))
   (expm1 x)))
x = abs(x);
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 0.0002) {
		tmp = (0.002777777777777778 * pow(x, 6.0)) + fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
	} else {
		tmp = expm1(x);
	}
	return tmp;
}
x = abs(x)
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 0.0002)
		tmp = Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))));
	else
		tmp = expm1(x);
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 0.0002], N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.0002:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\

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


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

    1. Initial program 52.9%

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

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

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

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

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

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

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

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

      \[\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(0.08333333333333333 \cdot {x}^{4} + {x}^{2}\right)} \]
    5. Step-by-step derivation
      1. +-commutative100.0%

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

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

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

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

    if 2.0000000000000001e-4 < (+.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. distribute-neg-in100.0%

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

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

        \[\leadsto e^{x} + \color{blue}{\left(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 53.6%

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

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

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

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

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

Alternative 3: 99.8% accurate, 0.4× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;{\left({\left(-2 + 2 \cdot \cosh x\right)}^{3}\right)}^{0.3333333333333333}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 5e-9)
   (fma x x (* 0.08333333333333333 (pow x 4.0)))
   (pow (pow (+ -2.0 (* 2.0 (cosh x))) 3.0) 0.3333333333333333)))
x = abs(x);
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 5e-9) {
		tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
	} else {
		tmp = pow(pow((-2.0 + (2.0 * cosh(x))), 3.0), 0.3333333333333333);
	}
	return tmp;
}
x = abs(x)
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 5e-9)
		tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0)));
	else
		tmp = (Float64(-2.0 + Float64(2.0 * cosh(x))) ^ 3.0) ^ 0.3333333333333333;
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 5e-9], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Power[N[Power[N[(-2.0 + N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 3.0], $MachinePrecision], 0.3333333333333333], $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\

\mathbf{else}:\\
\;\;\;\;{\left({\left(-2 + 2 \cdot \cosh x\right)}^{3}\right)}^{0.3333333333333333}\\


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

    1. Initial program 52.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 99.7%

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Step-by-step derivation
      1. add-cbrt-cube99.7%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\left(e^{x} + \left(e^{-x} + -2\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)}} \]
      2. pow1/399.7%

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

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

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

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

        \[\leadsto {\left({\left(-2 + \color{blue}{2 \cdot \cosh x}\right)}^{3}\right)}^{0.3333333333333333} \]
    5. Applied egg-rr99.7%

      \[\leadsto \color{blue}{{\left({\left(-2 + 2 \cdot \cosh x\right)}^{3}\right)}^{0.3333333333333333}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

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

Alternative 4: 99.8% accurate, 0.4× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\log \left(-2 + 2 \cdot \cosh x\right)}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 5e-9)
   (fma x x (* 0.08333333333333333 (pow x 4.0)))
   (exp (log (+ -2.0 (* 2.0 (cosh x)))))))
x = abs(x);
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 5e-9) {
		tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
	} else {
		tmp = exp(log((-2.0 + (2.0 * cosh(x)))));
	}
	return tmp;
}
x = abs(x)
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 5e-9)
		tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0)));
	else
		tmp = exp(log(Float64(-2.0 + Float64(2.0 * cosh(x)))));
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 5e-9], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Exp[N[Log[N[(-2.0 + N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\

\mathbf{else}:\\
\;\;\;\;e^{\log \left(-2 + 2 \cdot \cosh x\right)}\\


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

    1. Initial program 52.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 99.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\log \left(-2 + \color{blue}{2 \cdot \cosh x}\right)} \]
    5. Applied egg-rr99.7%

      \[\leadsto \color{blue}{e^{\log \left(-2 + 2 \cdot \cosh x\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

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

Alternative 5: 99.8% accurate, 0.5× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \cosh x - 2\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 5e-9)
   (fma x x (* 0.08333333333333333 (pow x 4.0)))
   (- (* 2.0 (cosh x)) 2.0)))
x = abs(x);
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 5e-9) {
		tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
	} else {
		tmp = (2.0 * cosh(x)) - 2.0;
	}
	return tmp;
}
x = abs(x)
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 5e-9)
		tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0)));
	else
		tmp = Float64(Float64(2.0 * cosh(x)) - 2.0);
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 5e-9], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\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))) < 5.0000000000000001e-9

    1. Initial program 52.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 99.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 99.8% accurate, 0.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \cosh x - 2\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x)
 :precision binary64
 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 5e-9)
   (* x x)
   (- (* 2.0 (cosh x)) 2.0)))
x = abs(x);
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 5e-9) {
		tmp = x * x;
	} else {
		tmp = (2.0 * cosh(x)) - 2.0;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (((exp(x) - 2.0d0) + exp(-x)) <= 5d-9) then
        tmp = x * x
    else
        tmp = (2.0d0 * cosh(x)) - 2.0d0
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
	double tmp;
	if (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 5e-9) {
		tmp = x * x;
	} else {
		tmp = (2.0 * Math.cosh(x)) - 2.0;
	}
	return tmp;
}
x = abs(x)
def code(x):
	tmp = 0
	if ((math.exp(x) - 2.0) + math.exp(-x)) <= 5e-9:
		tmp = x * x
	else:
		tmp = (2.0 * math.cosh(x)) - 2.0
	return tmp
x = abs(x)
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 5e-9)
		tmp = Float64(x * x);
	else
		tmp = Float64(Float64(2.0 * cosh(x)) - 2.0);
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x)
	tmp = 0.0;
	if (((exp(x) - 2.0) + exp(-x)) <= 5e-9)
		tmp = x * x;
	else
		tmp = (2.0 * cosh(x)) - 2.0;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 5e-9], N[(x * x), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 5 \cdot 10^{-9}:\\
\;\;\;\;x \cdot x\\

\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))) < 5.0000000000000001e-9

    1. Initial program 52.5%

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt[3]{\left(\left(e^{x} + \left(e^{-x} + -2\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)}} \]
      2. pow1/352.5%

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

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

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

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

        \[\leadsto {\left({\left(-2 + \color{blue}{2 \cdot \cosh x}\right)}^{3}\right)}^{0.3333333333333333} \]
    5. Applied egg-rr52.5%

      \[\leadsto \color{blue}{{\left({\left(-2 + 2 \cdot \cosh x\right)}^{3}\right)}^{0.3333333333333333}} \]
    6. Taylor expanded in x around 0 67.5%

      \[\leadsto {\color{blue}{\left({x}^{6}\right)}}^{0.3333333333333333} \]
    7. Step-by-step derivation
      1. pow-pow99.6%

        \[\leadsto \color{blue}{{x}^{\left(6 \cdot 0.3333333333333333\right)}} \]
      2. metadata-eval99.6%

        \[\leadsto {x}^{\color{blue}{2}} \]
      3. pow299.6%

        \[\leadsto \color{blue}{x \cdot x} \]
    8. Applied egg-rr99.6%

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

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

    1. Initial program 99.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 7: 99.2% accurate, 2.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.66:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 (if (<= x 1.66) (* x x) (expm1 x)))
x = abs(x);
double code(double x) {
	double tmp;
	if (x <= 1.66) {
		tmp = x * x;
	} else {
		tmp = expm1(x);
	}
	return tmp;
}
x = Math.abs(x);
public static double code(double x) {
	double tmp;
	if (x <= 1.66) {
		tmp = x * x;
	} else {
		tmp = Math.expm1(x);
	}
	return tmp;
}
x = abs(x)
def code(x):
	tmp = 0
	if x <= 1.66:
		tmp = x * x
	else:
		tmp = math.expm1(x)
	return tmp
x = abs(x)
function code(x)
	tmp = 0.0
	if (x <= 1.66)
		tmp = Float64(x * x);
	else
		tmp = expm1(x);
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_] := If[LessEqual[x, 1.66], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.66:\\
\;\;\;\;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.65999999999999992

    1. Initial program 67.0%

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
    4. Step-by-step derivation
      1. add-cbrt-cube67.0%

        \[\leadsto \color{blue}{\sqrt[3]{\left(\left(e^{x} + \left(e^{-x} + -2\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)}} \]
      2. pow1/366.9%

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

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

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

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

        \[\leadsto {\left({\left(-2 + \color{blue}{2 \cdot \cosh x}\right)}^{3}\right)}^{0.3333333333333333} \]
    5. Applied egg-rr67.0%

      \[\leadsto \color{blue}{{\left({\left(-2 + 2 \cdot \cosh x\right)}^{3}\right)}^{0.3333333333333333}} \]
    6. Taylor expanded in x around 0 71.9%

      \[\leadsto {\color{blue}{\left({x}^{6}\right)}}^{0.3333333333333333} \]
    7. Step-by-step derivation
      1. pow-pow80.3%

        \[\leadsto \color{blue}{{x}^{\left(6 \cdot 0.3333333333333333\right)}} \]
      2. metadata-eval80.3%

        \[\leadsto {x}^{\color{blue}{2}} \]
      3. pow280.3%

        \[\leadsto \color{blue}{x \cdot x} \]
    8. Applied egg-rr80.3%

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

    if 1.65999999999999992 < 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. distribute-neg-in100.0%

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

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

        \[\leadsto e^{x} + \color{blue}{\left(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 simplification85.3%

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

Alternative 8: 75.9% accurate, 68.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ x \cdot x \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 (* x x))
x = abs(x);
double code(double x) {
	return x * x;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    code = x * x
end function
x = Math.abs(x);
public static double code(double x) {
	return x * x;
}
x = abs(x)
def code(x):
	return x * x
x = abs(x)
function code(x)
	return Float64(x * x)
end
x = abs(x)
function tmp = code(x)
	tmp = x * x;
end
NOTE: x should be positive before calling this function
code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
x \cdot x
\end{array}
Derivation
  1. Initial program 75.4%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{e^{x} + \left(e^{-x} + -2\right)} \]
  4. Step-by-step derivation
    1. add-cbrt-cube75.3%

      \[\leadsto \color{blue}{\sqrt[3]{\left(\left(e^{x} + \left(e^{-x} + -2\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)\right) \cdot \left(e^{x} + \left(e^{-x} + -2\right)\right)}} \]
    2. pow1/375.3%

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

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

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

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

      \[\leadsto {\left({\left(-2 + \color{blue}{2 \cdot \cosh x}\right)}^{3}\right)}^{0.3333333333333333} \]
  5. Applied egg-rr75.4%

    \[\leadsto \color{blue}{{\left({\left(-2 + 2 \cdot \cosh x\right)}^{3}\right)}^{0.3333333333333333}} \]
  6. Taylor expanded in x around 0 74.5%

    \[\leadsto {\color{blue}{\left({x}^{6}\right)}}^{0.3333333333333333} \]
  7. Step-by-step derivation
    1. pow-pow71.2%

      \[\leadsto \color{blue}{{x}^{\left(6 \cdot 0.3333333333333333\right)}} \]
    2. metadata-eval71.2%

      \[\leadsto {x}^{\color{blue}{2}} \]
    3. pow271.2%

      \[\leadsto \color{blue}{x \cdot x} \]
  8. Applied egg-rr71.2%

    \[\leadsto \color{blue}{x \cdot x} \]
  9. Final simplification71.2%

    \[\leadsto x \cdot x \]

Alternative 9: 6.2% accurate, 206.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ x \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x) :precision binary64 x)
x = abs(x);
double code(double x) {
	return x;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
    real(8), intent (in) :: x
    code = x
end function
x = Math.abs(x);
public static double code(double x) {
	return x;
}
x = abs(x)
def code(x):
	return x
x = abs(x)
function code(x)
	return x
end
x = abs(x)
function tmp = code(x)
	tmp = x;
end
NOTE: x should be positive before calling this function
code[x_] := x
\begin{array}{l}
x = |x|\\
\\
x
\end{array}
Derivation
  1. Initial program 75.4%

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

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

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

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

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

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

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

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

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

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

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

    \[\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 2023308 
(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))))