| Alternative 1 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 19972 |

(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
(FPCore (x) :precision binary64 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 2e-13) (+ (* x x) (* 0.08333333333333333 (* (* x x) (* x x)))) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
return (exp(x) - 2.0) + exp(-x);
}
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 2e-13) {
tmp = (x * x) + (0.08333333333333333 * ((x * x) * (x * x)));
} else {
tmp = (2.0 * cosh(x)) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (exp(x) - 2.0d0) + exp(-x)
end function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((exp(x) - 2.0d0) + exp(-x)) <= 2d-13) then
tmp = (x * x) + (0.08333333333333333d0 * ((x * x) * (x * x)))
else
tmp = (2.0d0 * cosh(x)) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
return (Math.exp(x) - 2.0) + Math.exp(-x);
}
public static double code(double x) {
double tmp;
if (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 2e-13) {
tmp = (x * x) + (0.08333333333333333 * ((x * x) * (x * x)));
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): return (math.exp(x) - 2.0) + math.exp(-x)
def code(x): tmp = 0 if ((math.exp(x) - 2.0) + math.exp(-x)) <= 2e-13: tmp = (x * x) + (0.08333333333333333 * ((x * x) * (x * x))) else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) end
function code(x) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 2e-13) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * Float64(Float64(x * x) * Float64(x * x)))); else tmp = Float64(Float64(2.0 * cosh(x)) - 2.0); end return tmp end
function tmp = code(x) tmp = (exp(x) - 2.0) + exp(-x); end
function tmp_2 = code(x) tmp = 0.0; if (((exp(x) - 2.0) + exp(-x)) <= 2e-13) tmp = (x * x) + (0.08333333333333333 * ((x * x) * (x * x))); else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 2e-13], N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\left(e^{x} - 2\right) + e^{-x}
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-13}:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 76.5% |
|---|---|
| Target | 100.0% |
| Herbie | 99.7% |
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2.0000000000000001e-13Initial program 55.6%
Simplified55.6%
[Start]55.6% | \[ \left(e^{x} - 2\right) + e^{-x}
\] |
|---|---|
associate-+l- [=>]55.6% | \[ \color{blue}{e^{x} - \left(2 - e^{-x}\right)}
\] |
sub-neg [=>]55.6% | \[ \color{blue}{e^{x} + \left(-\left(2 - e^{-x}\right)\right)}
\] |
sub-neg [=>]55.6% | \[ e^{x} + \left(-\color{blue}{\left(2 + \left(-e^{-x}\right)\right)}\right)
\] |
+-commutative [=>]55.6% | \[ e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right)
\] |
distribute-neg-in [=>]55.6% | \[ e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)}
\] |
remove-double-neg [=>]55.6% | \[ e^{x} + \left(\color{blue}{e^{-x}} + \left(-2\right)\right)
\] |
metadata-eval [=>]55.6% | \[ e^{x} + \left(e^{-x} + \color{blue}{-2}\right)
\] |
Taylor expanded in x around 0 100.0%
Simplified100.0%
[Start]100.0% | \[ {x}^{2} + 0.08333333333333333 \cdot {x}^{4}
\] |
|---|---|
unpow2 [=>]100.0% | \[ \color{blue}{x \cdot x} + 0.08333333333333333 \cdot {x}^{4}
\] |
Applied egg-rr100.0%
[Start]100.0% | \[ x \cdot x + 0.08333333333333333 \cdot {x}^{4}
\] |
|---|---|
metadata-eval [<=]100.0% | \[ x \cdot x + 0.08333333333333333 \cdot {x}^{\color{blue}{\left(2 + 2\right)}}
\] |
pow-prod-up [<=]100.0% | \[ x \cdot x + 0.08333333333333333 \cdot \color{blue}{\left({x}^{2} \cdot {x}^{2}\right)}
\] |
pow-prod-down [=>]100.0% | \[ x \cdot x + 0.08333333333333333 \cdot \color{blue}{{\left(x \cdot x\right)}^{2}}
\] |
pow2 [<=]100.0% | \[ x \cdot x + 0.08333333333333333 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)}
\] |
if 2.0000000000000001e-13 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Simplified100.0%
[Start]100.0% | \[ \left(e^{x} - 2\right) + e^{-x}
\] |
|---|---|
associate-+l- [=>]100.0% | \[ \color{blue}{e^{x} - \left(2 - e^{-x}\right)}
\] |
sub-neg [=>]100.0% | \[ \color{blue}{e^{x} + \left(-\left(2 - e^{-x}\right)\right)}
\] |
sub-neg [=>]100.0% | \[ e^{x} + \left(-\color{blue}{\left(2 + \left(-e^{-x}\right)\right)}\right)
\] |
+-commutative [=>]100.0% | \[ e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right)
\] |
distribute-neg-in [=>]100.0% | \[ e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)}
\] |
remove-double-neg [=>]100.0% | \[ e^{x} + \left(\color{blue}{e^{-x}} + \left(-2\right)\right)
\] |
metadata-eval [=>]100.0% | \[ e^{x} + \left(e^{-x} + \color{blue}{-2}\right)
\] |
Applied egg-rr100.0%
[Start]100.0% | \[ e^{x} + \left(e^{-x} + -2\right)
\] |
|---|---|
associate-+r+ [=>]100.0% | \[ \color{blue}{\left(e^{x} + e^{-x}\right) + -2}
\] |
cosh-undef [=>]100.0% | \[ \color{blue}{2 \cdot \cosh x} + -2
\] |
fma-def [=>]100.0% | \[ \color{blue}{\mathsf{fma}\left(2, \cosh x, -2\right)}
\] |
metadata-eval [<=]100.0% | \[ \mathsf{fma}\left(2, \cosh x, \color{blue}{-2}\right)
\] |
fma-neg [<=]100.0% | \[ \color{blue}{2 \cdot \cosh x - 2}
\] |
Final simplification100.0%
| Alternative 1 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 19972 |
| Alternative 2 | |
|---|---|
| Accuracy | 93.7% |
| Cost | 6596 |
| Alternative 3 | |
|---|---|
| Accuracy | 87.9% |
| Cost | 832 |
| Alternative 4 | |
|---|---|
| Accuracy | 76.1% |
| Cost | 192 |
| Alternative 5 | |
|---|---|
| Accuracy | 4.4% |
| Cost | 64 |
herbie shell --seed 2023178
(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))))