
(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:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (- (exp x) 2.0) (exp (- x)))))
(if (<= t_0 1e-5)
(+
(* 0.002777777777777778 (pow x 6.0))
(+ (* 0.08333333333333333 (pow x 4.0)) (pow x 2.0)))
t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 1e-5) {
tmp = (0.002777777777777778 * pow(x, 6.0)) + ((0.08333333333333333 * pow(x, 4.0)) + pow(x, 2.0));
} 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 <= 1d-5) then
tmp = (0.002777777777777778d0 * (x ** 6.0d0)) + ((0.08333333333333333d0 * (x ** 4.0d0)) + (x ** 2.0d0))
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 <= 1e-5) {
tmp = (0.002777777777777778 * Math.pow(x, 6.0)) + ((0.08333333333333333 * Math.pow(x, 4.0)) + Math.pow(x, 2.0));
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = (math.exp(x) - 2.0) + math.exp(-x) tmp = 0 if t_0 <= 1e-5: tmp = (0.002777777777777778 * math.pow(x, 6.0)) + ((0.08333333333333333 * math.pow(x, 4.0)) + math.pow(x, 2.0)) 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 <= 1e-5) tmp = Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + (x ^ 2.0))); 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 <= 1e-5) tmp = (0.002777777777777778 * (x ^ 6.0)) + ((0.08333333333333333 * (x ^ 4.0)) + (x ^ 2.0)); 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, 1e-5], N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 10^{-5}:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6} + \left(0.08333333333333333 \cdot {x}^{4} + {x}^{2}\right)\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 1.00000000000000008e-5Initial program 51.9%
+-commutative51.9%
associate-+r-51.7%
+-commutative51.7%
associate-+r-52.1%
+-commutative52.1%
associate-+l-51.9%
Simplified51.9%
Taylor expanded in x around 0 100.0%
if 1.00000000000000008e-5 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 4e-9) (* x x) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 4e-9) {
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 <= 4d-9) 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 <= 4e-9) {
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 <= 4e-9: 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 <= 4e-9) 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 <= 4e-9) 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, 4e-9], 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 4 \cdot 10^{-9}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 4.00000000000000025e-9Initial program 51.7%
+-commutative51.7%
associate-+r-51.5%
+-commutative51.5%
associate-+r-51.9%
+-commutative51.9%
associate-+l-51.7%
Simplified51.7%
Taylor expanded in x around 0 99.8%
unpow299.8%
Simplified99.8%
if 4.00000000000000025e-9 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 1e-5) (fma x x (* 0.08333333333333333 (pow x 4.0))) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 1e-5) {
tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
} 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 <= 1e-5) tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = t_0; end return 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, 1e-5], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 1.00000000000000008e-5Initial program 51.9%
+-commutative51.9%
associate-+r-51.7%
+-commutative51.7%
associate-+r-52.1%
+-commutative52.1%
associate-+l-51.9%
Simplified51.9%
Taylor expanded in x around 0 99.9%
+-commutative99.9%
unpow299.9%
fma-def99.9%
Simplified99.9%
if 1.00000000000000008e-5 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 6.0) (* x x) (sqrt (* (pow x 6.0) 0.027777777777777776))))
double code(double x) {
double tmp;
if (x <= 6.0) {
tmp = x * x;
} else {
tmp = sqrt((pow(x, 6.0) * 0.027777777777777776));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.0d0) then
tmp = x * x
else
tmp = sqrt(((x ** 6.0d0) * 0.027777777777777776d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.0) {
tmp = x * x;
} else {
tmp = Math.sqrt((Math.pow(x, 6.0) * 0.027777777777777776));
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.0: tmp = x * x else: tmp = math.sqrt((math.pow(x, 6.0) * 0.027777777777777776)) return tmp
function code(x) tmp = 0.0 if (x <= 6.0) tmp = Float64(x * x); else tmp = sqrt(Float64((x ^ 6.0) * 0.027777777777777776)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.0) tmp = x * x; else tmp = sqrt(((x ^ 6.0) * 0.027777777777777776)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.0], N[(x * x), $MachinePrecision], N[Sqrt[N[(N[Power[x, 6.0], $MachinePrecision] * 0.027777777777777776), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\sqrt{{x}^{6} \cdot 0.027777777777777776}\\
\end{array}
\end{array}
if x < 6Initial program 68.0%
+-commutative68.0%
associate-+r-67.9%
+-commutative67.9%
associate-+r-68.2%
+-commutative68.2%
associate-+l-68.0%
Simplified68.0%
Taylor expanded in x around 0 84.5%
unpow284.5%
Simplified84.5%
if 6 < x Initial program 100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+l-100.0%
Simplified100.0%
Taylor expanded in x around 0 74.0%
+-commutative74.0%
neg-mul-174.0%
unsub-neg74.0%
cube-mult74.0%
unpow274.0%
associate-*r*74.0%
distribute-rgt-out74.0%
Simplified74.0%
Taylor expanded in x around inf 74.0%
add-sqr-sqrt74.0%
sqrt-unprod84.5%
pow1/284.5%
swap-sqr84.5%
metadata-eval84.5%
pow-prod-up84.5%
metadata-eval84.5%
Applied egg-rr84.5%
unpow1/284.5%
Simplified84.5%
Final simplification84.5%
(FPCore (x)
:precision binary64
(if (<= x 0.000175)
(* x x)
(-
(exp (- x))
(+ 1.0 (- (* (* x x) (+ -0.5 (* x -0.16666666666666666))) x)))))
double code(double x) {
double tmp;
if (x <= 0.000175) {
tmp = x * x;
} else {
tmp = exp(-x) - (1.0 + (((x * x) * (-0.5 + (x * -0.16666666666666666))) - x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.000175d0) then
tmp = x * x
else
tmp = exp(-x) - (1.0d0 + (((x * x) * ((-0.5d0) + (x * (-0.16666666666666666d0)))) - x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.000175) {
tmp = x * x;
} else {
tmp = Math.exp(-x) - (1.0 + (((x * x) * (-0.5 + (x * -0.16666666666666666))) - x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.000175: tmp = x * x else: tmp = math.exp(-x) - (1.0 + (((x * x) * (-0.5 + (x * -0.16666666666666666))) - x)) return tmp
function code(x) tmp = 0.0 if (x <= 0.000175) tmp = Float64(x * x); else tmp = Float64(exp(Float64(-x)) - Float64(1.0 + Float64(Float64(Float64(x * x) * Float64(-0.5 + Float64(x * -0.16666666666666666))) - x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.000175) tmp = x * x; else tmp = exp(-x) - (1.0 + (((x * x) * (-0.5 + (x * -0.16666666666666666))) - x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.000175], N[(x * x), $MachinePrecision], N[(N[Exp[(-x)], $MachinePrecision] - N[(1.0 + N[(N[(N[(x * x), $MachinePrecision] * N[(-0.5 + N[(x * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.000175:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;e^{-x} - \left(1 + \left(\left(x \cdot x\right) \cdot \left(-0.5 + x \cdot -0.16666666666666666\right) - x\right)\right)\\
\end{array}
\end{array}
if x < 1.74999999999999998e-4Initial program 67.8%
+-commutative67.8%
associate-+r-67.7%
+-commutative67.7%
associate-+r-67.9%
+-commutative67.9%
associate-+l-67.8%
Simplified67.8%
Taylor expanded in x around 0 85.0%
unpow285.0%
Simplified85.0%
if 1.74999999999999998e-4 < x Initial program 99.6%
+-commutative99.6%
associate-+r-99.6%
+-commutative99.6%
associate-+r-99.6%
+-commutative99.6%
associate-+l-99.6%
Simplified99.6%
Taylor expanded in x around 0 72.8%
+-commutative72.8%
neg-mul-172.8%
unsub-neg72.8%
cube-mult72.8%
unpow272.8%
associate-*r*72.8%
distribute-rgt-out72.8%
Simplified72.8%
Final simplification82.0%
(FPCore (x) :precision binary64 (if (<= x 6.0) (* x x) (* (* x x) (* x (- -0.16666666666666666)))))
double code(double x) {
double tmp;
if (x <= 6.0) {
tmp = x * x;
} else {
tmp = (x * x) * (x * -(-0.16666666666666666));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.0d0) then
tmp = x * x
else
tmp = (x * x) * (x * -(-0.16666666666666666d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.0) {
tmp = x * x;
} else {
tmp = (x * x) * (x * -(-0.16666666666666666));
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.0: tmp = x * x else: tmp = (x * x) * (x * -(-0.16666666666666666)) return tmp
function code(x) tmp = 0.0 if (x <= 6.0) tmp = Float64(x * x); else tmp = Float64(Float64(x * x) * Float64(x * Float64(-(-0.16666666666666666)))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.0) tmp = x * x; else tmp = (x * x) * (x * -(-0.16666666666666666)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.0], N[(x * x), $MachinePrecision], N[(N[(x * x), $MachinePrecision] * N[(x * (--0.16666666666666666)), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(x \cdot \left(--0.16666666666666666\right)\right)\\
\end{array}
\end{array}
if x < 6Initial program 68.0%
+-commutative68.0%
associate-+r-67.9%
+-commutative67.9%
associate-+r-68.2%
+-commutative68.2%
associate-+l-68.0%
Simplified68.0%
Taylor expanded in x around 0 84.5%
unpow284.5%
Simplified84.5%
if 6 < x Initial program 100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+l-100.0%
Simplified100.0%
Taylor expanded in x around 0 74.0%
+-commutative74.0%
neg-mul-174.0%
unsub-neg74.0%
cube-mult74.0%
unpow274.0%
associate-*r*74.0%
distribute-rgt-out74.0%
Simplified74.0%
Taylor expanded in x around inf 74.0%
metadata-eval74.0%
distribute-lft-neg-in74.0%
*-commutative74.0%
unpow374.0%
associate-*l*74.0%
distribute-lft-neg-in74.0%
metadata-eval74.0%
unpow274.0%
distribute-lft-neg-in74.0%
*-commutative74.0%
distribute-lft-neg-in74.0%
distribute-lft-in74.0%
distribute-lft-neg-in74.0%
fma-udef74.0%
associate-*r*74.0%
Simplified74.0%
distribute-lft-neg-out74.0%
add-sqr-sqrt74.0%
sqrt-unprod74.0%
sqr-neg74.0%
sqrt-unprod0.0%
add-sqr-sqrt0.1%
associate-*r*0.1%
*-commutative0.1%
add-sqr-sqrt0.0%
sqrt-unprod74.0%
sqr-neg74.0%
sqrt-unprod74.0%
add-sqr-sqrt74.0%
Applied egg-rr74.0%
Taylor expanded in x around inf 74.0%
Final simplification82.0%
(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}
Initial program 75.8%
+-commutative75.8%
associate-+r-75.7%
+-commutative75.7%
associate-+r-75.9%
+-commutative75.9%
associate-+l-75.8%
Simplified75.8%
Taylor expanded in x around 0 76.9%
unpow276.9%
Simplified76.9%
Final simplification76.9%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 75.8%
+-commutative75.8%
associate-+r-75.7%
+-commutative75.7%
associate-+r-75.9%
+-commutative75.9%
associate-+l-75.8%
Simplified75.8%
Applied egg-rr26.4%
Final simplification26.4%
(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}
herbie shell --seed 2023279
(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))))