exp2 (problem 3.3.7)

Percentage Accurate: 77.2% → 99.3%
Time: 4.7s
Alternatives: 5
Speedup: 0.7×

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 5 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.2% 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.3% accurate, 0.5× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-8}:\\ \;\;\;\;\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))) 2e-8)
   (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)) <= 2e-8) {
		tmp = 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))) <= 2e-8)
		tmp = 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], 2e-8], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $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 2 \cdot 10^{-8}:\\
\;\;\;\;\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))) < 2e-8

    1. Initial program 51.5%

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

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

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

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

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

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

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

        \[\leadsto e^{x} + \left(e^{-x} + \color{blue}{-2}\right) \]
    3. Simplified51.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 \color{blue}{{x}^{2} + 0.08333333333333333 \cdot {x}^{4}} \]
      2. unpow2100.0%

        \[\leadsto \color{blue}{x \cdot x} + 0.08333333333333333 \cdot {x}^{4} \]
      3. fma-def100.0%

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

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

    if 2e-8 < (+.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. Taylor expanded in x around 0 45.8%

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

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

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

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

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

Alternative 2: 99.2% accurate, 0.7× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-8}:\\ \;\;\;\;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 (<= (+ (- (exp x) 2.0) (exp (- x))) 2e-8) (* x x) (expm1 x)))
x = abs(x);
double code(double x) {
	double tmp;
	if (((exp(x) - 2.0) + exp(-x)) <= 2e-8) {
		tmp = x * x;
	} else {
		tmp = expm1(x);
	}
	return tmp;
}
x = Math.abs(x);
public static double code(double x) {
	double tmp;
	if (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 2e-8) {
		tmp = x * x;
	} else {
		tmp = Math.expm1(x);
	}
	return tmp;
}
x = abs(x)
def code(x):
	tmp = 0
	if ((math.exp(x) - 2.0) + math.exp(-x)) <= 2e-8:
		tmp = x * x
	else:
		tmp = math.expm1(x)
	return tmp
x = abs(x)
function code(x)
	tmp = 0.0
	if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 2e-8)
		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[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 2e-8], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-8}:\\
\;\;\;\;x \cdot x\\

\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))) < 2e-8

    1. Initial program 51.5%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{x \cdot x} \]
    6. Simplified99.7%

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

    if 2e-8 < (+.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. Taylor expanded in x around 0 45.8%

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

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

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

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

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

Alternative 3: 76.5% 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 74.4%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{x \cdot x} \]
  6. Simplified78.7%

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

    \[\leadsto x \cdot x \]

Alternative 4: 3.7% accurate, 206.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto e^{\color{blue}{\sqrt{\left(-x\right) \cdot \left(-x\right)}}} - \left(2 - e^{x}\right) \]
    7. sqr-neg74.3%

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

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

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

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

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

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

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

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

      \[\leadsto e^{x} + \color{blue}{\sqrt{e^{x} + -2} \cdot \sqrt{e^{x} + -2}} \]
    6. fabs-sqr21.5%

      \[\leadsto e^{x} + \color{blue}{\left|\sqrt{e^{x} + -2} \cdot \sqrt{e^{x} + -2}\right|} \]
    7. rem-square-sqrt24.5%

      \[\leadsto e^{x} + \left|\color{blue}{e^{x} + -2}\right| \]
    8. metadata-eval24.5%

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

      \[\leadsto e^{x} + \left|\color{blue}{e^{x} - 2}\right| \]
    10. fabs-sub24.5%

      \[\leadsto e^{x} + \color{blue}{\left|2 - e^{x}\right|} \]
    11. rem-square-sqrt3.0%

      \[\leadsto e^{x} + \left|\color{blue}{\sqrt{2 - e^{x}} \cdot \sqrt{2 - e^{x}}}\right| \]
    12. fabs-sqr3.0%

      \[\leadsto e^{x} + \color{blue}{\sqrt{2 - e^{x}} \cdot \sqrt{2 - e^{x}}} \]
    13. rem-square-sqrt3.0%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{2} \]
  8. Final simplification3.7%

    \[\leadsto 2 \]

Alternative 5: 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 74.4%

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

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

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

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

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

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

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

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

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

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

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

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