
(FPCore (a b eps) :precision binary64 (/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))
double code(double a, double b, double eps) {
return (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 1.0));
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = (eps * (exp(((a + b) * eps)) - 1.0d0)) / ((exp((a * eps)) - 1.0d0) * (exp((b * eps)) - 1.0d0))
end function
public static double code(double a, double b, double eps) {
return (eps * (Math.exp(((a + b) * eps)) - 1.0)) / ((Math.exp((a * eps)) - 1.0) * (Math.exp((b * eps)) - 1.0));
}
def code(a, b, eps): return (eps * (math.exp(((a + b) * eps)) - 1.0)) / ((math.exp((a * eps)) - 1.0) * (math.exp((b * eps)) - 1.0))
function code(a, b, eps) return Float64(Float64(eps * Float64(exp(Float64(Float64(a + b) * eps)) - 1.0)) / Float64(Float64(exp(Float64(a * eps)) - 1.0) * Float64(exp(Float64(b * eps)) - 1.0))) end
function tmp = code(a, b, eps) tmp = (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 1.0)); end
code[a_, b_, eps_] := N[(N[(eps * N[(N[Exp[N[(N[(a + b), $MachinePrecision] * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Exp[N[(a * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] * N[(N[Exp[N[(b * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon \cdot \left(e^{\left(a + b\right) \cdot \varepsilon} - 1\right)}{\left(e^{a \cdot \varepsilon} - 1\right) \cdot \left(e^{b \cdot \varepsilon} - 1\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b eps) :precision binary64 (/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))
double code(double a, double b, double eps) {
return (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 1.0));
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = (eps * (exp(((a + b) * eps)) - 1.0d0)) / ((exp((a * eps)) - 1.0d0) * (exp((b * eps)) - 1.0d0))
end function
public static double code(double a, double b, double eps) {
return (eps * (Math.exp(((a + b) * eps)) - 1.0)) / ((Math.exp((a * eps)) - 1.0) * (Math.exp((b * eps)) - 1.0));
}
def code(a, b, eps): return (eps * (math.exp(((a + b) * eps)) - 1.0)) / ((math.exp((a * eps)) - 1.0) * (math.exp((b * eps)) - 1.0))
function code(a, b, eps) return Float64(Float64(eps * Float64(exp(Float64(Float64(a + b) * eps)) - 1.0)) / Float64(Float64(exp(Float64(a * eps)) - 1.0) * Float64(exp(Float64(b * eps)) - 1.0))) end
function tmp = code(a, b, eps) tmp = (eps * (exp(((a + b) * eps)) - 1.0)) / ((exp((a * eps)) - 1.0) * (exp((b * eps)) - 1.0)); end
code[a_, b_, eps_] := N[(N[(eps * N[(N[Exp[N[(N[(a + b), $MachinePrecision] * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Exp[N[(a * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] * N[(N[Exp[N[(b * eps), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon \cdot \left(e^{\left(a + b\right) \cdot \varepsilon} - 1\right)}{\left(e^{a \cdot \varepsilon} - 1\right) \cdot \left(e^{b \cdot \varepsilon} - 1\right)}
\end{array}
(FPCore (a b eps)
:precision binary64
(let* ((t_0 (* eps (+ a b)))
(t_1
(/
(* eps (+ (exp t_0) -1.0))
(* (+ (exp (* eps a)) -1.0) (+ (exp (* eps b)) -1.0)))))
(if (or (<= t_1 (- INFINITY)) (not (<= t_1 5e-33)))
(+ (/ 1.0 b) (/ 1.0 a))
(* (expm1 t_0) (/ eps (* (expm1 (* eps a)) (expm1 (* eps b))))))))
double code(double a, double b, double eps) {
double t_0 = eps * (a + b);
double t_1 = (eps * (exp(t_0) + -1.0)) / ((exp((eps * a)) + -1.0) * (exp((eps * b)) + -1.0));
double tmp;
if ((t_1 <= -((double) INFINITY)) || !(t_1 <= 5e-33)) {
tmp = (1.0 / b) + (1.0 / a);
} else {
tmp = expm1(t_0) * (eps / (expm1((eps * a)) * expm1((eps * b))));
}
return tmp;
}
public static double code(double a, double b, double eps) {
double t_0 = eps * (a + b);
double t_1 = (eps * (Math.exp(t_0) + -1.0)) / ((Math.exp((eps * a)) + -1.0) * (Math.exp((eps * b)) + -1.0));
double tmp;
if ((t_1 <= -Double.POSITIVE_INFINITY) || !(t_1 <= 5e-33)) {
tmp = (1.0 / b) + (1.0 / a);
} else {
tmp = Math.expm1(t_0) * (eps / (Math.expm1((eps * a)) * Math.expm1((eps * b))));
}
return tmp;
}
def code(a, b, eps): t_0 = eps * (a + b) t_1 = (eps * (math.exp(t_0) + -1.0)) / ((math.exp((eps * a)) + -1.0) * (math.exp((eps * b)) + -1.0)) tmp = 0 if (t_1 <= -math.inf) or not (t_1 <= 5e-33): tmp = (1.0 / b) + (1.0 / a) else: tmp = math.expm1(t_0) * (eps / (math.expm1((eps * a)) * math.expm1((eps * b)))) return tmp
function code(a, b, eps) t_0 = Float64(eps * Float64(a + b)) t_1 = Float64(Float64(eps * Float64(exp(t_0) + -1.0)) / Float64(Float64(exp(Float64(eps * a)) + -1.0) * Float64(exp(Float64(eps * b)) + -1.0))) tmp = 0.0 if ((t_1 <= Float64(-Inf)) || !(t_1 <= 5e-33)) tmp = Float64(Float64(1.0 / b) + Float64(1.0 / a)); else tmp = Float64(expm1(t_0) * Float64(eps / Float64(expm1(Float64(eps * a)) * expm1(Float64(eps * b))))); end return tmp end
code[a_, b_, eps_] := Block[{t$95$0 = N[(eps * N[(a + b), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(eps * N[(N[Exp[t$95$0], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Exp[N[(eps * a), $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision] * N[(N[Exp[N[(eps * b), $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$1, (-Infinity)], N[Not[LessEqual[t$95$1, 5e-33]], $MachinePrecision]], N[(N[(1.0 / b), $MachinePrecision] + N[(1.0 / a), $MachinePrecision]), $MachinePrecision], N[(N[(Exp[t$95$0] - 1), $MachinePrecision] * N[(eps / N[(N[(Exp[N[(eps * a), $MachinePrecision]] - 1), $MachinePrecision] * N[(Exp[N[(eps * b), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \varepsilon \cdot \left(a + b\right)\\
t_1 := \frac{\varepsilon \cdot \left(e^{t_0} + -1\right)}{\left(e^{\varepsilon \cdot a} + -1\right) \cdot \left(e^{\varepsilon \cdot b} + -1\right)}\\
\mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 5 \cdot 10^{-33}\right):\\
\;\;\;\;\frac{1}{b} + \frac{1}{a}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(t_0\right) \cdot \frac{\varepsilon}{\mathsf{expm1}\left(\varepsilon \cdot a\right) \cdot \mathsf{expm1}\left(\varepsilon \cdot b\right)}\\
\end{array}
\end{array}
if (/.f64 (*.f64 eps (-.f64 (exp.f64 (*.f64 (+.f64 a b) eps)) 1)) (*.f64 (-.f64 (exp.f64 (*.f64 a eps)) 1) (-.f64 (exp.f64 (*.f64 b eps)) 1))) < -inf.0 or 5.00000000000000028e-33 < (/.f64 (*.f64 eps (-.f64 (exp.f64 (*.f64 (+.f64 a b) eps)) 1)) (*.f64 (-.f64 (exp.f64 (*.f64 a eps)) 1) (-.f64 (exp.f64 (*.f64 b eps)) 1))) Initial program 0.9%
associate-*l/0.9%
*-commutative0.9%
expm1-def2.7%
*-commutative2.7%
expm1-def11.7%
*-commutative11.7%
expm1-def38.1%
*-commutative38.1%
Simplified38.1%
Taylor expanded in eps around 0 82.9%
Taylor expanded in a around 0 100.0%
if -inf.0 < (/.f64 (*.f64 eps (-.f64 (exp.f64 (*.f64 (+.f64 a b) eps)) 1)) (*.f64 (-.f64 (exp.f64 (*.f64 a eps)) 1) (-.f64 (exp.f64 (*.f64 b eps)) 1))) < 5.00000000000000028e-33Initial program 92.0%
associate-*l/92.0%
*-commutative92.0%
expm1-def92.0%
*-commutative92.0%
expm1-def100.0%
*-commutative100.0%
expm1-def100.0%
*-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b eps) :precision binary64 (- (+ (/ 1.0 b) (/ 1.0 a)) (* eps 0.5)))
double code(double a, double b, double eps) {
return ((1.0 / b) + (1.0 / a)) - (eps * 0.5);
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = ((1.0d0 / b) + (1.0d0 / a)) - (eps * 0.5d0)
end function
public static double code(double a, double b, double eps) {
return ((1.0 / b) + (1.0 / a)) - (eps * 0.5);
}
def code(a, b, eps): return ((1.0 / b) + (1.0 / a)) - (eps * 0.5)
function code(a, b, eps) return Float64(Float64(Float64(1.0 / b) + Float64(1.0 / a)) - Float64(eps * 0.5)) end
function tmp = code(a, b, eps) tmp = ((1.0 / b) + (1.0 / a)) - (eps * 0.5); end
code[a_, b_, eps_] := N[(N[(N[(1.0 / b), $MachinePrecision] + N[(1.0 / a), $MachinePrecision]), $MachinePrecision] - N[(eps * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{b} + \frac{1}{a}\right) - \varepsilon \cdot 0.5
\end{array}
Initial program 4.8%
times-frac4.8%
expm1-def14.3%
*-commutative14.3%
expm1-def15.6%
*-commutative15.6%
expm1-def55.7%
*-commutative55.7%
Simplified55.7%
Taylor expanded in eps around 0 53.3%
Taylor expanded in a around 0 96.8%
Final simplification96.8%
(FPCore (a b eps) :precision binary64 (+ (/ 1.0 b) (/ 1.0 a)))
double code(double a, double b, double eps) {
return (1.0 / b) + (1.0 / a);
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = (1.0d0 / b) + (1.0d0 / a)
end function
public static double code(double a, double b, double eps) {
return (1.0 / b) + (1.0 / a);
}
def code(a, b, eps): return (1.0 / b) + (1.0 / a)
function code(a, b, eps) return Float64(Float64(1.0 / b) + Float64(1.0 / a)) end
function tmp = code(a, b, eps) tmp = (1.0 / b) + (1.0 / a); end
code[a_, b_, eps_] := N[(N[(1.0 / b), $MachinePrecision] + N[(1.0 / a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{b} + \frac{1}{a}
\end{array}
Initial program 4.8%
associate-*l/4.8%
*-commutative4.8%
expm1-def6.6%
*-commutative6.6%
expm1-def15.5%
*-commutative15.5%
expm1-def40.8%
*-commutative40.8%
Simplified40.8%
Taylor expanded in eps around 0 80.5%
Taylor expanded in a around 0 96.4%
Final simplification96.4%
(FPCore (a b eps) :precision binary64 (if (<= b 6.4e-127) (/ 1.0 b) (/ 1.0 a)))
double code(double a, double b, double eps) {
double tmp;
if (b <= 6.4e-127) {
tmp = 1.0 / b;
} else {
tmp = 1.0 / a;
}
return tmp;
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
real(8) :: tmp
if (b <= 6.4d-127) then
tmp = 1.0d0 / b
else
tmp = 1.0d0 / a
end if
code = tmp
end function
public static double code(double a, double b, double eps) {
double tmp;
if (b <= 6.4e-127) {
tmp = 1.0 / b;
} else {
tmp = 1.0 / a;
}
return tmp;
}
def code(a, b, eps): tmp = 0 if b <= 6.4e-127: tmp = 1.0 / b else: tmp = 1.0 / a return tmp
function code(a, b, eps) tmp = 0.0 if (b <= 6.4e-127) tmp = Float64(1.0 / b); else tmp = Float64(1.0 / a); end return tmp end
function tmp_2 = code(a, b, eps) tmp = 0.0; if (b <= 6.4e-127) tmp = 1.0 / b; else tmp = 1.0 / a; end tmp_2 = tmp; end
code[a_, b_, eps_] := If[LessEqual[b, 6.4e-127], N[(1.0 / b), $MachinePrecision], N[(1.0 / a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.4 \cdot 10^{-127}:\\
\;\;\;\;\frac{1}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{a}\\
\end{array}
\end{array}
if b < 6.40000000000000035e-127Initial program 4.1%
associate-*l/4.1%
*-commutative4.1%
expm1-def5.8%
*-commutative5.8%
expm1-def13.4%
*-commutative13.4%
expm1-def35.1%
*-commutative35.1%
Simplified35.1%
Taylor expanded in b around 0 59.6%
if 6.40000000000000035e-127 < b Initial program 6.2%
associate-*l/6.2%
*-commutative6.2%
expm1-def7.9%
*-commutative7.9%
expm1-def19.3%
*-commutative19.3%
expm1-def50.8%
*-commutative50.8%
Simplified50.8%
Taylor expanded in a around 0 67.5%
Final simplification62.5%
(FPCore (a b eps) :precision binary64 (* eps -0.5))
double code(double a, double b, double eps) {
return eps * -0.5;
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = eps * (-0.5d0)
end function
public static double code(double a, double b, double eps) {
return eps * -0.5;
}
def code(a, b, eps): return eps * -0.5
function code(a, b, eps) return Float64(eps * -0.5) end
function tmp = code(a, b, eps) tmp = eps * -0.5; end
code[a_, b_, eps_] := N[(eps * -0.5), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot -0.5
\end{array}
Initial program 4.8%
times-frac4.8%
expm1-def14.3%
*-commutative14.3%
expm1-def15.6%
*-commutative15.6%
expm1-def55.7%
*-commutative55.7%
Simplified55.7%
Taylor expanded in eps around 0 53.3%
Taylor expanded in a around 0 96.8%
Taylor expanded in eps around inf 3.0%
*-commutative3.0%
Simplified3.0%
Final simplification3.0%
(FPCore (a b eps) :precision binary64 (/ 1.0 a))
double code(double a, double b, double eps) {
return 1.0 / a;
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = 1.0d0 / a
end function
public static double code(double a, double b, double eps) {
return 1.0 / a;
}
def code(a, b, eps): return 1.0 / a
function code(a, b, eps) return Float64(1.0 / a) end
function tmp = code(a, b, eps) tmp = 1.0 / a; end
code[a_, b_, eps_] := N[(1.0 / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{a}
\end{array}
Initial program 4.8%
associate-*l/4.8%
*-commutative4.8%
expm1-def6.6%
*-commutative6.6%
expm1-def15.5%
*-commutative15.5%
expm1-def40.8%
*-commutative40.8%
Simplified40.8%
Taylor expanded in a around 0 49.1%
Final simplification49.1%
(FPCore (a b eps) :precision binary64 (/ (+ a b) (* a b)))
double code(double a, double b, double eps) {
return (a + b) / (a * b);
}
real(8) function code(a, b, eps)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: eps
code = (a + b) / (a * b)
end function
public static double code(double a, double b, double eps) {
return (a + b) / (a * b);
}
def code(a, b, eps): return (a + b) / (a * b)
function code(a, b, eps) return Float64(Float64(a + b) / Float64(a * b)) end
function tmp = code(a, b, eps) tmp = (a + b) / (a * b); end
code[a_, b_, eps_] := N[(N[(a + b), $MachinePrecision] / N[(a * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a + b}{a \cdot b}
\end{array}
herbie shell --seed 2023242
(FPCore (a b eps)
:name "expq3 (problem 3.4.2)"
:precision binary64
:pre (and (< -1.0 eps) (< eps 1.0))
:herbie-target
(/ (+ a b) (* a b))
(/ (* eps (- (exp (* (+ a b) eps)) 1.0)) (* (- (exp (* a eps)) 1.0) (- (exp (* b eps)) 1.0))))