
(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 (exp (- (log1p (exp (- b a))))))
double code(double a, double b) {
return exp(-log1p(exp((b - a))));
}
public static double code(double a, double b) {
return Math.exp(-Math.log1p(Math.exp((b - a))));
}
def code(a, b): return math.exp(-math.log1p(math.exp((b - a))))
function code(a, b) return exp(Float64(-log1p(exp(Float64(b - a))))) end
code[a_, b_] := N[Exp[(-N[Log[1 + N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision]], $MachinePrecision])], $MachinePrecision]
\begin{array}{l}
\\
e^{-\mathsf{log1p}\left(e^{b - a}\right)}
\end{array}
Initial program 98.8%
*-lft-identity98.8%
associate-/l*98.8%
remove-double-div98.8%
exp-neg98.8%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in72.2%
exp-neg72.2%
rgt-mult-inverse99.2%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-udef100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= a -800.0) (exp a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -800.0) {
tmp = 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 <= (-800.0d0)) then
tmp = 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 <= -800.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -800.0: tmp = math.exp(a) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -800.0) tmp = exp(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 <= -800.0) tmp = exp(a); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -800.0], N[Exp[a], $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -800:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -800Initial program 98.6%
Taylor expanded in b around 0 100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
add-exp-log100.0%
log-div100.0%
add-log-exp100.0%
log1p-udef100.0%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
Simplified100.0%
Taylor expanded in a around inf 100.0%
if -800 < a Initial program 98.9%
*-lft-identity98.9%
associate-/l*98.9%
remove-double-div98.9%
exp-neg98.9%
associate-/r/98.9%
/-rgt-identity98.9%
*-commutative98.9%
distribute-rgt-in98.9%
exp-neg98.9%
rgt-mult-inverse99.4%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 97.6%
Final simplification98.2%
(FPCore (a b) :precision binary64 (/ 1.0 (+ (exp (- b a)) 1.0)))
double code(double a, double b) {
return 1.0 / (exp((b - a)) + 1.0);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (exp((b - a)) + 1.0d0)
end function
public static double code(double a, double b) {
return 1.0 / (Math.exp((b - a)) + 1.0);
}
def code(a, b): return 1.0 / (math.exp((b - a)) + 1.0)
function code(a, b) return Float64(1.0 / Float64(exp(Float64(b - a)) + 1.0)) end
function tmp = code(a, b) tmp = 1.0 / (exp((b - a)) + 1.0); end
code[a_, b_] := N[(1.0 / N[(N[Exp[N[(b - a), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{e^{b - a} + 1}
\end{array}
Initial program 98.8%
*-lft-identity98.8%
associate-/l*98.8%
remove-double-div98.8%
exp-neg98.8%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in72.2%
exp-neg72.2%
rgt-mult-inverse99.2%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (or (<= b -0.000475) (not (<= b 4e+17))) (exp a) (/ 1.0 (+ 2.0 (* a (+ -1.0 (* a 0.5)))))))
double code(double a, double b) {
double tmp;
if ((b <= -0.000475) || !(b <= 4e+17)) {
tmp = exp(a);
} else {
tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((b <= (-0.000475d0)) .or. (.not. (b <= 4d+17))) then
tmp = exp(a)
else
tmp = 1.0d0 / (2.0d0 + (a * ((-1.0d0) + (a * 0.5d0))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b <= -0.000475) || !(b <= 4e+17)) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
return tmp;
}
def code(a, b): tmp = 0 if (b <= -0.000475) or not (b <= 4e+17): tmp = math.exp(a) else: tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5)))) return tmp
function code(a, b) tmp = 0.0 if ((b <= -0.000475) || !(b <= 4e+17)) tmp = exp(a); else tmp = Float64(1.0 / Float64(2.0 + Float64(a * Float64(-1.0 + Float64(a * 0.5))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b <= -0.000475) || ~((b <= 4e+17))) tmp = exp(a); else tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5)))); end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[b, -0.000475], N[Not[LessEqual[b, 4e+17]], $MachinePrecision]], N[Exp[a], $MachinePrecision], N[(1.0 / N[(2.0 + N[(a * N[(-1.0 + N[(a * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.000475 \lor \neg \left(b \leq 4 \cdot 10^{+17}\right):\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + a \cdot \left(-1 + a \cdot 0.5\right)}\\
\end{array}
\end{array}
if b < -4.74999999999999999e-4 or 4e17 < b Initial program 98.2%
Taylor expanded in b around 0 28.0%
expm1-log1p-u28.0%
expm1-udef28.0%
add-exp-log28.0%
log-div28.0%
add-log-exp28.0%
log1p-udef28.0%
Applied egg-rr28.0%
expm1-def28.0%
expm1-log1p28.0%
Simplified28.0%
Taylor expanded in a around inf 63.4%
if -4.74999999999999999e-4 < b < 4e17Initial program 99.3%
*-lft-identity99.3%
associate-/l*99.3%
remove-double-div99.3%
exp-neg99.3%
associate-/r/99.3%
/-rgt-identity99.3%
*-commutative99.3%
distribute-rgt-in65.5%
exp-neg65.5%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 96.4%
Taylor expanded in a around 0 80.7%
*-commutative80.7%
*-commutative80.7%
unpow280.7%
associate-*l*80.7%
distribute-lft-out80.7%
Simplified80.7%
Final simplification73.0%
(FPCore (a b) :precision binary64 (/ 1.0 (+ 2.0 (* a (+ -1.0 (* a 0.5))))))
double code(double a, double b) {
return 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (2.0d0 + (a * ((-1.0d0) + (a * 0.5d0))))
end function
public static double code(double a, double b) {
return 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
def code(a, b): return 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))))
function code(a, b) return Float64(1.0 / Float64(2.0 + Float64(a * Float64(-1.0 + Float64(a * 0.5))))) end
function tmp = code(a, b) tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5)))); end
code[a_, b_] := N[(1.0 / N[(2.0 + N[(a * N[(-1.0 + N[(a * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 + a \cdot \left(-1 + a \cdot 0.5\right)}
\end{array}
Initial program 98.8%
*-lft-identity98.8%
associate-/l*98.8%
remove-double-div98.8%
exp-neg98.8%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in72.2%
exp-neg72.2%
rgt-mult-inverse99.2%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 65.9%
Taylor expanded in a around 0 51.8%
*-commutative51.8%
*-commutative51.8%
unpow251.8%
associate-*l*51.8%
distribute-lft-out51.8%
Simplified51.8%
Final simplification51.8%
(FPCore (a b) :precision binary64 (+ 0.5 (* a 0.25)))
double code(double a, double b) {
return 0.5 + (a * 0.25);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 0.5d0 + (a * 0.25d0)
end function
public static double code(double a, double b) {
return 0.5 + (a * 0.25);
}
def code(a, b): return 0.5 + (a * 0.25)
function code(a, b) return Float64(0.5 + Float64(a * 0.25)) end
function tmp = code(a, b) tmp = 0.5 + (a * 0.25); end
code[a_, b_] := N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + a \cdot 0.25
\end{array}
Initial program 98.8%
*-lft-identity98.8%
associate-/l*98.8%
remove-double-div98.8%
exp-neg98.8%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in72.2%
exp-neg72.2%
rgt-mult-inverse99.2%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 65.9%
Taylor expanded in a around 0 38.3%
*-commutative38.3%
Simplified38.3%
Final simplification38.3%
(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.8%
*-lft-identity98.8%
associate-/l*98.8%
remove-double-div98.8%
exp-neg98.8%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in72.2%
exp-neg72.2%
rgt-mult-inverse99.2%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 65.9%
Taylor expanded in a around 0 39.0%
neg-mul-139.0%
unsub-neg39.0%
Simplified39.0%
Final simplification39.0%
(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.8%
*-lft-identity98.8%
associate-/l*98.8%
remove-double-div98.8%
exp-neg98.8%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in72.2%
exp-neg72.2%
rgt-mult-inverse99.2%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 65.9%
Taylor expanded in a around 0 38.2%
Final simplification38.2%
(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 2023308
(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))))