
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
double code(double a, double b) {
return exp(a) / (exp(a) + exp(b));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / (exp(a) + exp(b))
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(a) + Math.exp(b));
}
def code(a, b): return math.exp(a) / (math.exp(a) + math.exp(b))
function code(a, b) return Float64(exp(a) / Float64(exp(a) + exp(b))) end
function tmp = code(a, b) tmp = exp(a) / (exp(a) + exp(b)); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{e^{a} + e^{b}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
double code(double a, double b) {
return exp(a) / (exp(a) + exp(b));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / (exp(a) + exp(b))
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(a) + Math.exp(b));
}
def code(a, b): return math.exp(a) / (math.exp(a) + math.exp(b))
function code(a, b) return Float64(exp(a) / Float64(exp(a) + exp(b))) end
function tmp = code(a, b) tmp = exp(a) / (exp(a) + exp(b)); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{e^{a} + e^{b}}
\end{array}
(FPCore (a b) :precision binary64 (/ 1.0 (+ 1.0 (exp (- b a)))))
double code(double a, double b) {
return 1.0 / (1.0 + exp((b - a)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (1.0d0 + exp((b - a)))
end function
public static double code(double a, double b) {
return 1.0 / (1.0 + Math.exp((b - a)));
}
def code(a, b): return 1.0 / (1.0 + math.exp((b - a)))
function code(a, b) return Float64(1.0 / Float64(1.0 + exp(Float64(b - a)))) end
function tmp = code(a, b) tmp = 1.0 / (1.0 + exp((b - a))); end
code[a_, b_] := N[(1.0 / N[(1.0 + N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + e^{b - a}}
\end{array}
Initial program 98.0%
*-lft-identity98.0%
associate-/l*98.0%
remove-double-div98.0%
exp-neg98.0%
associate-/r/98.0%
/-rgt-identity98.0%
*-commutative98.0%
distribute-rgt-in73.4%
exp-neg73.4%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (exp b) 2e-11) (+ 1.0 (exp b)) (/ 1.0 (+ b 2.0))))
double code(double a, double b) {
double tmp;
if (exp(b) <= 2e-11) {
tmp = 1.0 + exp(b);
} else {
tmp = 1.0 / (b + 2.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (exp(b) <= 2d-11) then
tmp = 1.0d0 + exp(b)
else
tmp = 1.0d0 / (b + 2.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(b) <= 2e-11) {
tmp = 1.0 + Math.exp(b);
} else {
tmp = 1.0 / (b + 2.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(b) <= 2e-11: tmp = 1.0 + math.exp(b) else: tmp = 1.0 / (b + 2.0) return tmp
function code(a, b) tmp = 0.0 if (exp(b) <= 2e-11) tmp = Float64(1.0 + exp(b)); else tmp = Float64(1.0 / Float64(b + 2.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(b) <= 2e-11) tmp = 1.0 + exp(b); else tmp = 1.0 / (b + 2.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[b], $MachinePrecision], 2e-11], N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(b + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 2 \cdot 10^{-11}:\\
\;\;\;\;1 + e^{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{b + 2}\\
\end{array}
\end{array}
if (exp.f64 b) < 1.99999999999999988e-11Initial program 95.4%
*-lft-identity95.4%
associate-/l*95.4%
remove-double-div95.4%
exp-neg95.4%
associate-/r/95.4%
/-rgt-identity95.4%
*-commutative95.4%
distribute-rgt-in95.4%
exp-neg95.4%
rgt-mult-inverse97.7%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 100.0%
log1p-def100.0%
Simplified100.0%
add-sqr-sqrt97.7%
sqrt-unprod99.4%
sqr-neg99.4%
sqrt-unprod99.4%
add-sqr-sqrt99.4%
log1p-udef99.4%
rem-exp-log99.4%
+-commutative99.4%
Applied egg-rr99.4%
if 1.99999999999999988e-11 < (exp.f64 b) Initial program 98.6%
*-lft-identity98.6%
associate-/l*98.5%
remove-double-div98.5%
exp-neg98.5%
associate-/r/98.6%
/-rgt-identity98.6%
*-commutative98.6%
distribute-rgt-in68.8%
exp-neg68.8%
rgt-mult-inverse98.6%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 79.4%
Taylor expanded in b around 0 47.4%
+-commutative47.4%
Simplified47.4%
Final simplification56.3%
(FPCore (a b) :precision binary64 (if (<= a -3.5e-12) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -3.5e-12) {
tmp = 1.0 / (1.0 + exp(-a));
} else {
tmp = 1.0 / (1.0 + exp(b));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-3.5d-12)) then
tmp = 1.0d0 / (1.0d0 + exp(-a))
else
tmp = 1.0d0 / (1.0d0 + exp(b))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -3.5e-12) {
tmp = 1.0 / (1.0 + Math.exp(-a));
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -3.5e-12: tmp = 1.0 / (1.0 + math.exp(-a)) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -3.5e-12) tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-a)))); else tmp = Float64(1.0 / Float64(1.0 + exp(b))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -3.5e-12) tmp = 1.0 / (1.0 + exp(-a)); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -3.5e-12], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -3.5 \cdot 10^{-12}:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -3.5e-12Initial program 98.5%
*-lft-identity98.5%
associate-/l*98.5%
remove-double-div98.5%
exp-neg98.5%
associate-/r/98.5%
/-rgt-identity98.5%
*-commutative98.5%
distribute-rgt-in5.8%
exp-neg5.8%
rgt-mult-inverse98.5%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 98.5%
if -3.5e-12 < a Initial program 97.8%
*-lft-identity97.8%
associate-/l*97.8%
remove-double-div97.8%
exp-neg97.8%
associate-/r/97.8%
/-rgt-identity97.8%
*-commutative97.8%
distribute-rgt-in97.8%
exp-neg97.8%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 99.5%
Final simplification99.2%
(FPCore (a b) :precision binary64 (if (<= a -480.0) (/ (exp a) a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -480.0) {
tmp = exp(a) / a;
} else {
tmp = 1.0 / (1.0 + exp(b));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-480.0d0)) then
tmp = exp(a) / a
else
tmp = 1.0d0 / (1.0d0 + exp(b))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -480.0) {
tmp = Math.exp(a) / a;
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -480.0: tmp = math.exp(a) / a else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -480.0) tmp = Float64(exp(a) / a); else tmp = Float64(1.0 / Float64(1.0 + exp(b))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -480.0) tmp = exp(a) / a; else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -480.0], N[(N[Exp[a], $MachinePrecision] / a), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -480:\\
\;\;\;\;\frac{e^{a}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -480Initial program 98.5%
Taylor expanded in b around 0 98.5%
Taylor expanded in a around 0 97.0%
+-commutative97.0%
Simplified97.0%
Taylor expanded in a around inf 97.0%
if -480 < a Initial program 97.9%
*-lft-identity97.9%
associate-/l*97.9%
remove-double-div97.9%
exp-neg97.9%
associate-/r/97.9%
/-rgt-identity97.9%
*-commutative97.9%
distribute-rgt-in97.9%
exp-neg97.9%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 98.4%
Final simplification98.1%
(FPCore (a b) :precision binary64 (if (<= a -3.5e-12) (/ (exp a) (+ a 2.0)) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -3.5e-12) {
tmp = exp(a) / (a + 2.0);
} else {
tmp = 1.0 / (1.0 + exp(b));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-3.5d-12)) then
tmp = exp(a) / (a + 2.0d0)
else
tmp = 1.0d0 / (1.0d0 + exp(b))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -3.5e-12) {
tmp = Math.exp(a) / (a + 2.0);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -3.5e-12: tmp = math.exp(a) / (a + 2.0) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -3.5e-12) tmp = Float64(exp(a) / Float64(a + 2.0)); else tmp = Float64(1.0 / Float64(1.0 + exp(b))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -3.5e-12) tmp = exp(a) / (a + 2.0); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -3.5e-12], N[(N[Exp[a], $MachinePrecision] / N[(a + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -3.5 \cdot 10^{-12}:\\
\;\;\;\;\frac{e^{a}}{a + 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -3.5e-12Initial program 98.5%
Taylor expanded in b around 0 98.6%
Taylor expanded in a around 0 94.3%
+-commutative94.3%
Simplified94.3%
if -3.5e-12 < a Initial program 97.8%
*-lft-identity97.8%
associate-/l*97.8%
remove-double-div97.8%
exp-neg97.8%
associate-/r/97.8%
/-rgt-identity97.8%
*-commutative97.8%
distribute-rgt-in97.8%
exp-neg97.8%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 99.5%
Final simplification98.1%
(FPCore (a b) :precision binary64 (if (<= a -2.2) (/ (exp a) a) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (a <= -2.2) {
tmp = exp(a) / a;
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-2.2d0)) then
tmp = exp(a) / a
else
tmp = 0.5d0 + (a * 0.25d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -2.2) {
tmp = Math.exp(a) / a;
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -2.2: tmp = math.exp(a) / a else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (a <= -2.2) tmp = Float64(exp(a) / a); else tmp = Float64(0.5 + Float64(a * 0.25)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -2.2) tmp = exp(a) / a; else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -2.2], N[(N[Exp[a], $MachinePrecision] / a), $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2.2:\\
\;\;\;\;\frac{e^{a}}{a}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if a < -2.2000000000000002Initial program 98.5%
Taylor expanded in b around 0 98.6%
Taylor expanded in a around 0 94.2%
+-commutative94.2%
Simplified94.2%
Taylor expanded in a around inf 94.2%
if -2.2000000000000002 < a Initial program 97.9%
*-lft-identity97.9%
associate-/l*97.8%
remove-double-div97.8%
exp-neg97.8%
associate-/r/97.9%
/-rgt-identity97.9%
*-commutative97.9%
distribute-rgt-in97.9%
exp-neg97.9%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 56.0%
Taylor expanded in a around 0 55.4%
*-commutative55.4%
Simplified55.4%
Final simplification65.5%
(FPCore (a b) :precision binary64 (/ 1.0 (- 2.0 a)))
double code(double a, double b) {
return 1.0 / (2.0 - a);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (2.0d0 - a)
end function
public static double code(double a, double b) {
return 1.0 / (2.0 - a);
}
def code(a, b): return 1.0 / (2.0 - a)
function code(a, b) return Float64(1.0 / Float64(2.0 - a)) end
function tmp = code(a, b) tmp = 1.0 / (2.0 - a); end
code[a_, b_] := N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 - a}
\end{array}
Initial program 98.0%
*-lft-identity98.0%
associate-/l*98.0%
remove-double-div98.0%
exp-neg98.0%
associate-/r/98.0%
/-rgt-identity98.0%
*-commutative98.0%
distribute-rgt-in73.4%
exp-neg73.4%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 67.1%
Taylor expanded in a around 0 42.1%
neg-mul-142.1%
unsub-neg42.1%
Simplified42.1%
Final simplification42.1%
(FPCore (a b) :precision binary64 0.5)
double code(double a, double b) {
return 0.5;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 0.5d0
end function
public static double code(double a, double b) {
return 0.5;
}
def code(a, b): return 0.5
function code(a, b) return 0.5 end
function tmp = code(a, b) tmp = 0.5; end
code[a_, b_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 98.0%
*-lft-identity98.0%
associate-/l*98.0%
remove-double-div98.0%
exp-neg98.0%
associate-/r/98.0%
/-rgt-identity98.0%
*-commutative98.0%
distribute-rgt-in73.4%
exp-neg73.4%
rgt-mult-inverse98.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 82.9%
Taylor expanded in b around 0 41.5%
Final simplification41.5%
(FPCore (a b) :precision binary64 (/ 1.0 (+ 1.0 (exp (- b a)))))
double code(double a, double b) {
return 1.0 / (1.0 + exp((b - a)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (1.0d0 + exp((b - a)))
end function
public static double code(double a, double b) {
return 1.0 / (1.0 + Math.exp((b - a)));
}
def code(a, b): return 1.0 / (1.0 + math.exp((b - a)))
function code(a, b) return Float64(1.0 / Float64(1.0 + exp(Float64(b - a)))) end
function tmp = code(a, b) tmp = 1.0 / (1.0 + exp((b - a))); end
code[a_, b_] := N[(1.0 / N[(1.0 + N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + e^{b - a}}
\end{array}
herbie shell --seed 2023320
(FPCore (a b)
:name "Quotient of sum of exps"
:precision binary64
:herbie-target
(/ 1.0 (+ 1.0 (exp (- b a))))
(/ (exp a) (+ (exp a) (exp b))))