
(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}
NOTE: a and b should be sorted in increasing order before calling this function.
(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 1e-68)))
(+ (/ 1.0 b) (/ 1.0 a))
(* (/ eps (expm1 (* eps a))) (/ (expm1 t_0) (expm1 (* eps b)))))))assert(a < 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 <= 1e-68)) {
tmp = (1.0 / b) + (1.0 / a);
} else {
tmp = (eps / expm1((eps * a))) * (expm1(t_0) / expm1((eps * b)));
}
return tmp;
}
assert a < b;
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 <= 1e-68)) {
tmp = (1.0 / b) + (1.0 / a);
} else {
tmp = (eps / Math.expm1((eps * a))) * (Math.expm1(t_0) / Math.expm1((eps * b)));
}
return tmp;
}
[a, b] = sort([a, b]) 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 <= 1e-68): tmp = (1.0 / b) + (1.0 / a) else: tmp = (eps / math.expm1((eps * a))) * (math.expm1(t_0) / math.expm1((eps * b))) return tmp
a, b = sort([a, b]) 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 <= 1e-68)) tmp = Float64(Float64(1.0 / b) + Float64(1.0 / a)); else tmp = Float64(Float64(eps / expm1(Float64(eps * a))) * Float64(expm1(t_0) / expm1(Float64(eps * b)))); end return tmp end
NOTE: a and b should be sorted in increasing order before calling this function.
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, 1e-68]], $MachinePrecision]], N[(N[(1.0 / b), $MachinePrecision] + N[(1.0 / a), $MachinePrecision]), $MachinePrecision], N[(N[(eps / N[(Exp[N[(eps * a), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision] * N[(N[(Exp[t$95$0] - 1), $MachinePrecision] / N[(Exp[N[(eps * b), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\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 10^{-68}\right):\\
\;\;\;\;\frac{1}{b} + \frac{1}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{\varepsilon}{\mathsf{expm1}\left(\varepsilon \cdot a\right)} \cdot \frac{\mathsf{expm1}\left(t_0\right)}{\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 1.00000000000000007e-68 < (/.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.6%
times-frac0.6%
expm1-def9.8%
*-commutative9.8%
expm1-def11.3%
*-commutative11.3%
expm1-def50.5%
*-commutative50.5%
Simplified50.5%
Taylor expanded in eps around 0 76.0%
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))) < 1.00000000000000007e-68Initial program 99.3%
times-frac99.3%
expm1-def99.9%
*-commutative99.9%
expm1-def99.9%
*-commutative99.9%
expm1-def99.9%
*-commutative99.9%
Simplified99.9%
Final simplification100.0%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (- (+ (/ 1.0 b) (/ 1.0 a)) (* eps 0.5)))
assert(a < b);
double code(double a, double b, double eps) {
return ((1.0 / b) + (1.0 / a)) - (eps * 0.5);
}
NOTE: a and b should be sorted in increasing order before calling this function.
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
assert a < b;
public static double code(double a, double b, double eps) {
return ((1.0 / b) + (1.0 / a)) - (eps * 0.5);
}
[a, b] = sort([a, b]) def code(a, b, eps): return ((1.0 / b) + (1.0 / a)) - (eps * 0.5)
a, b = sort([a, b]) function code(a, b, eps) return Float64(Float64(Float64(1.0 / b) + Float64(1.0 / a)) - Float64(eps * 0.5)) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b, eps)
tmp = ((1.0 / b) + (1.0 / a)) - (eps * 0.5);
end
NOTE: a and b should be sorted in increasing order before calling this function. 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}
[a, b] = \mathsf{sort}([a, b])\\
\\
\left(\frac{1}{b} + \frac{1}{a}\right) - \varepsilon \cdot 0.5
\end{array}
Initial program 5.6%
times-frac5.6%
expm1-def14.4%
*-commutative14.4%
expm1-def15.8%
*-commutative15.8%
expm1-def53.0%
*-commutative53.0%
Simplified53.0%
Taylor expanded in eps around 0 55.4%
Taylor expanded in a around 0 95.5%
Final simplification95.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (+ (/ 1.0 b) (/ 1.0 a)))
assert(a < b);
double code(double a, double b, double eps) {
return (1.0 / b) + (1.0 / a);
}
NOTE: a and b should be sorted in increasing order before calling this function.
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
assert a < b;
public static double code(double a, double b, double eps) {
return (1.0 / b) + (1.0 / a);
}
[a, b] = sort([a, b]) def code(a, b, eps): return (1.0 / b) + (1.0 / a)
a, b = sort([a, b]) function code(a, b, eps) return Float64(Float64(1.0 / b) + Float64(1.0 / a)) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b, eps)
tmp = (1.0 / b) + (1.0 / a);
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_, eps_] := N[(N[(1.0 / b), $MachinePrecision] + N[(1.0 / a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\frac{1}{b} + \frac{1}{a}
\end{array}
Initial program 5.6%
times-frac5.6%
expm1-def14.4%
*-commutative14.4%
expm1-def15.8%
*-commutative15.8%
expm1-def53.0%
*-commutative53.0%
Simplified53.0%
Taylor expanded in eps around 0 74.2%
Taylor expanded in a around 0 95.3%
Final simplification95.3%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (if (<= a -2.8e-150) (/ 1.0 b) (/ 1.0 a)))
assert(a < b);
double code(double a, double b, double eps) {
double tmp;
if (a <= -2.8e-150) {
tmp = 1.0 / b;
} else {
tmp = 1.0 / a;
}
return tmp;
}
NOTE: a and b should be sorted in increasing order before calling this function.
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 (a <= (-2.8d-150)) then
tmp = 1.0d0 / b
else
tmp = 1.0d0 / a
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b, double eps) {
double tmp;
if (a <= -2.8e-150) {
tmp = 1.0 / b;
} else {
tmp = 1.0 / a;
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b, eps): tmp = 0 if a <= -2.8e-150: tmp = 1.0 / b else: tmp = 1.0 / a return tmp
a, b = sort([a, b]) function code(a, b, eps) tmp = 0.0 if (a <= -2.8e-150) tmp = Float64(1.0 / b); else tmp = Float64(1.0 / a); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b, eps)
tmp = 0.0;
if (a <= -2.8e-150)
tmp = 1.0 / b;
else
tmp = 1.0 / a;
end
tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_, eps_] := If[LessEqual[a, -2.8e-150], N[(1.0 / b), $MachinePrecision], N[(1.0 / a), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2.8 \cdot 10^{-150}:\\
\;\;\;\;\frac{1}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{a}\\
\end{array}
\end{array}
if a < -2.79999999999999996e-150Initial program 6.8%
times-frac6.8%
expm1-def17.7%
*-commutative17.7%
expm1-def18.6%
*-commutative18.6%
expm1-def63.8%
*-commutative63.8%
Simplified63.8%
Taylor expanded in b around 0 67.3%
if -2.79999999999999996e-150 < a Initial program 5.0%
times-frac5.0%
expm1-def12.9%
*-commutative12.9%
expm1-def14.5%
*-commutative14.5%
expm1-def48.0%
*-commutative48.0%
Simplified48.0%
Taylor expanded in eps around 0 71.5%
Taylor expanded in a around 0 58.3%
Final simplification61.1%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (* eps -0.5))
assert(a < b);
double code(double a, double b, double eps) {
return eps * -0.5;
}
NOTE: a and b should be sorted in increasing order before calling this function.
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
assert a < b;
public static double code(double a, double b, double eps) {
return eps * -0.5;
}
[a, b] = sort([a, b]) def code(a, b, eps): return eps * -0.5
a, b = sort([a, b]) function code(a, b, eps) return Float64(eps * -0.5) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b, eps)
tmp = eps * -0.5;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_, eps_] := N[(eps * -0.5), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\varepsilon \cdot -0.5
\end{array}
Initial program 5.6%
times-frac5.6%
expm1-def14.4%
*-commutative14.4%
expm1-def15.8%
*-commutative15.8%
expm1-def53.0%
*-commutative53.0%
Simplified53.0%
Taylor expanded in eps around 0 55.4%
Taylor expanded in a around 0 95.5%
Taylor expanded in b around inf 48.8%
Taylor expanded in a around inf 3.0%
Final simplification3.0%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b eps) :precision binary64 (/ 1.0 a))
assert(a < b);
double code(double a, double b, double eps) {
return 1.0 / a;
}
NOTE: a and b should be sorted in increasing order before calling this function.
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
assert a < b;
public static double code(double a, double b, double eps) {
return 1.0 / a;
}
[a, b] = sort([a, b]) def code(a, b, eps): return 1.0 / a
a, b = sort([a, b]) function code(a, b, eps) return Float64(1.0 / a) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b, eps)
tmp = 1.0 / a;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_, eps_] := N[(1.0 / a), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\frac{1}{a}
\end{array}
Initial program 5.6%
times-frac5.6%
expm1-def14.4%
*-commutative14.4%
expm1-def15.8%
*-commutative15.8%
expm1-def53.0%
*-commutative53.0%
Simplified53.0%
Taylor expanded in eps around 0 74.2%
Taylor expanded in a around 0 48.5%
Final simplification48.5%
(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 2023207
(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))))