
(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 7 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 99.6%
*-lft-identity99.6%
associate-*l/99.6%
associate-/r/99.6%
remove-double-neg99.6%
unsub-neg99.6%
div-sub72.2%
*-lft-identity72.2%
associate-*l/72.2%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-define100.0%
Applied egg-rr100.0%
(FPCore (a b) :precision binary64 (if (<= a -710.0) 0.0 (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -710.0) {
tmp = 0.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 <= (-710.0d0)) then
tmp = 0.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 <= -710.0) {
tmp = 0.0;
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -710.0: tmp = 0.0 else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -710.0) tmp = 0.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 <= -710.0) tmp = 0.0; else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -710.0], 0.0, N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -710:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -710Initial program 100.0%
*-lft-identity100.0%
associate-*l/100.0%
associate-/r/100.0%
remove-double-neg100.0%
unsub-neg100.0%
div-sub0.0%
*-lft-identity0.0%
associate-*l/0.0%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-define100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 43.3%
log1p-define43.3%
Simplified43.3%
Applied egg-rr100.0%
if -710 < a Initial program 99.4%
Taylor expanded in a around 0 98.5%
(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 99.6%
*-lft-identity99.6%
associate-*l/99.6%
associate-/r/99.6%
remove-double-neg99.6%
unsub-neg99.6%
div-sub72.2%
*-lft-identity72.2%
associate-*l/72.2%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b)
:precision binary64
(if (<= b -7.5e-7)
1.0
(if (or (<= b 1.95e-85) (and (not (<= b 2.1e-66)) (<= b 0.011)))
(+ 0.5 (* a 0.25))
0.0)))
double code(double a, double b) {
double tmp;
if (b <= -7.5e-7) {
tmp = 1.0;
} else if ((b <= 1.95e-85) || (!(b <= 2.1e-66) && (b <= 0.011))) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-7.5d-7)) then
tmp = 1.0d0
else if ((b <= 1.95d-85) .or. (.not. (b <= 2.1d-66)) .and. (b <= 0.011d0)) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -7.5e-7) {
tmp = 1.0;
} else if ((b <= 1.95e-85) || (!(b <= 2.1e-66) && (b <= 0.011))) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -7.5e-7: tmp = 1.0 elif (b <= 1.95e-85) or (not (b <= 2.1e-66) and (b <= 0.011)): tmp = 0.5 + (a * 0.25) else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -7.5e-7) tmp = 1.0; elseif ((b <= 1.95e-85) || (!(b <= 2.1e-66) && (b <= 0.011))) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -7.5e-7) tmp = 1.0; elseif ((b <= 1.95e-85) || (~((b <= 2.1e-66)) && (b <= 0.011))) tmp = 0.5 + (a * 0.25); else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -7.5e-7], 1.0, If[Or[LessEqual[b, 1.95e-85], And[N[Not[LessEqual[b, 2.1e-66]], $MachinePrecision], LessEqual[b, 0.011]]], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -7.5 \cdot 10^{-7}:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 1.95 \cdot 10^{-85} \lor \neg \left(b \leq 2.1 \cdot 10^{-66}\right) \land b \leq 0.011:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -7.5000000000000002e-7Initial program 97.7%
*-lft-identity97.7%
associate-*l/97.7%
associate-/r/97.7%
remove-double-neg97.7%
unsub-neg97.7%
div-sub93.3%
*-lft-identity93.3%
associate-*l/93.3%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
expm1-log1p-u99.9%
expm1-undefine99.9%
Applied egg-rr99.9%
sub-neg99.9%
metadata-eval99.9%
+-commutative99.9%
log1p-undefine99.9%
rem-exp-log99.9%
associate-+r+99.9%
metadata-eval99.9%
Simplified99.9%
add-exp-log99.9%
associate-+r+100.0%
metadata-eval100.0%
log1p-undefine100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod88.9%
add-sqr-sqrt92.2%
exp-neg92.2%
log1p-undefine92.2%
metadata-eval92.2%
associate-+r+92.2%
add-exp-log92.2%
add-sqr-sqrt92.2%
Applied egg-rr92.2%
*-inverses92.3%
Simplified92.3%
if -7.5000000000000002e-7 < b < 1.94999999999999994e-85 or 2.1e-66 < b < 0.010999999999999999Initial program 99.9%
Taylor expanded in b around 0 99.0%
Taylor expanded in a around 0 71.5%
*-commutative71.5%
Simplified71.5%
if 1.94999999999999994e-85 < b < 2.1e-66 or 0.010999999999999999 < b Initial program 100.0%
*-lft-identity100.0%
associate-*l/100.0%
associate-/r/100.0%
remove-double-neg100.0%
unsub-neg100.0%
div-sub61.5%
*-lft-identity61.5%
associate-*l/61.5%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-define100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 93.6%
log1p-define93.6%
Simplified93.6%
Applied egg-rr98.9%
Final simplification84.9%
(FPCore (a b) :precision binary64 (if (<= b -0.85) 1.0 (if (<= b 2e-91) 0.5 (if (<= b 2.1e-66) 0.0 (if (<= b 4e-10) 0.5 0.0)))))
double code(double a, double b) {
double tmp;
if (b <= -0.85) {
tmp = 1.0;
} else if (b <= 2e-91) {
tmp = 0.5;
} else if (b <= 2.1e-66) {
tmp = 0.0;
} else if (b <= 4e-10) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-0.85d0)) then
tmp = 1.0d0
else if (b <= 2d-91) then
tmp = 0.5d0
else if (b <= 2.1d-66) then
tmp = 0.0d0
else if (b <= 4d-10) then
tmp = 0.5d0
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -0.85) {
tmp = 1.0;
} else if (b <= 2e-91) {
tmp = 0.5;
} else if (b <= 2.1e-66) {
tmp = 0.0;
} else if (b <= 4e-10) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.85: tmp = 1.0 elif b <= 2e-91: tmp = 0.5 elif b <= 2.1e-66: tmp = 0.0 elif b <= 4e-10: tmp = 0.5 else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -0.85) tmp = 1.0; elseif (b <= 2e-91) tmp = 0.5; elseif (b <= 2.1e-66) tmp = 0.0; elseif (b <= 4e-10) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.85) tmp = 1.0; elseif (b <= 2e-91) tmp = 0.5; elseif (b <= 2.1e-66) tmp = 0.0; elseif (b <= 4e-10) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.85], 1.0, If[LessEqual[b, 2e-91], 0.5, If[LessEqual[b, 2.1e-66], 0.0, If[LessEqual[b, 4e-10], 0.5, 0.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.85:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 2 \cdot 10^{-91}:\\
\;\;\;\;0.5\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{-66}:\\
\;\;\;\;0\\
\mathbf{elif}\;b \leq 4 \cdot 10^{-10}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -0.849999999999999978Initial program 97.6%
*-lft-identity97.6%
associate-*l/97.6%
associate-/r/97.6%
remove-double-neg97.6%
unsub-neg97.6%
div-sub97.6%
*-lft-identity97.6%
associate-*l/97.6%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
expm1-log1p-u99.9%
expm1-undefine99.9%
Applied egg-rr99.9%
sub-neg99.9%
metadata-eval99.9%
+-commutative99.9%
log1p-undefine99.9%
rem-exp-log99.9%
associate-+r+99.9%
metadata-eval99.9%
Simplified99.9%
add-exp-log99.9%
associate-+r+100.0%
metadata-eval100.0%
log1p-undefine100.0%
add-sqr-sqrt100.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod93.0%
add-sqr-sqrt96.4%
exp-neg96.4%
log1p-undefine96.4%
metadata-eval96.4%
associate-+r+96.4%
add-exp-log96.4%
add-sqr-sqrt96.4%
Applied egg-rr96.4%
*-inverses96.4%
Simplified96.4%
if -0.849999999999999978 < b < 2.00000000000000004e-91 or 2.1e-66 < b < 4.00000000000000015e-10Initial program 99.9%
Taylor expanded in b around 0 99.0%
Taylor expanded in a around 0 69.1%
if 2.00000000000000004e-91 < b < 2.1e-66 or 4.00000000000000015e-10 < b Initial program 100.0%
*-lft-identity100.0%
associate-*l/100.0%
associate-/r/100.0%
remove-double-neg100.0%
unsub-neg100.0%
div-sub61.5%
*-lft-identity61.5%
associate-*l/61.5%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-define100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 93.6%
log1p-define93.6%
Simplified93.6%
Applied egg-rr98.9%
Final simplification84.3%
(FPCore (a b) :precision binary64 (if (<= b 2.05e-85) 0.5 (if (<= b 2.1e-66) 0.0 (if (<= b 0.07) 0.5 0.0))))
double code(double a, double b) {
double tmp;
if (b <= 2.05e-85) {
tmp = 0.5;
} else if (b <= 2.1e-66) {
tmp = 0.0;
} else if (b <= 0.07) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 2.05d-85) then
tmp = 0.5d0
else if (b <= 2.1d-66) then
tmp = 0.0d0
else if (b <= 0.07d0) then
tmp = 0.5d0
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 2.05e-85) {
tmp = 0.5;
} else if (b <= 2.1e-66) {
tmp = 0.0;
} else if (b <= 0.07) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 2.05e-85: tmp = 0.5 elif b <= 2.1e-66: tmp = 0.0 elif b <= 0.07: tmp = 0.5 else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= 2.05e-85) tmp = 0.5; elseif (b <= 2.1e-66) tmp = 0.0; elseif (b <= 0.07) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 2.05e-85) tmp = 0.5; elseif (b <= 2.1e-66) tmp = 0.0; elseif (b <= 0.07) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 2.05e-85], 0.5, If[LessEqual[b, 2.1e-66], 0.0, If[LessEqual[b, 0.07], 0.5, 0.0]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.05 \cdot 10^{-85}:\\
\;\;\;\;0.5\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{-66}:\\
\;\;\;\;0\\
\mathbf{elif}\;b \leq 0.07:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < 2.04999999999999997e-85 or 2.1e-66 < b < 0.070000000000000007Initial program 99.3%
Taylor expanded in b around 0 78.0%
Taylor expanded in a around 0 56.0%
if 2.04999999999999997e-85 < b < 2.1e-66 or 0.070000000000000007 < b Initial program 100.0%
*-lft-identity100.0%
associate-*l/100.0%
associate-/r/100.0%
remove-double-neg100.0%
unsub-neg100.0%
div-sub61.5%
*-lft-identity61.5%
associate-*l/61.5%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-define100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 93.6%
log1p-define93.6%
Simplified93.6%
Applied egg-rr98.9%
(FPCore (a b) :precision binary64 0.0)
double code(double a, double b) {
return 0.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 0.0d0
end function
public static double code(double a, double b) {
return 0.0;
}
def code(a, b): return 0.0
function code(a, b) return 0.0 end
function tmp = code(a, b) tmp = 0.0; end
code[a_, b_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 99.6%
*-lft-identity99.6%
associate-*l/99.6%
associate-/r/99.6%
remove-double-neg99.6%
unsub-neg99.6%
div-sub72.2%
*-lft-identity72.2%
associate-*l/72.2%
lft-mult-inverse100.0%
sub-neg100.0%
distribute-frac-neg100.0%
remove-double-neg100.0%
div-exp100.0%
Simplified100.0%
add-exp-log100.0%
log-rec100.0%
log1p-define100.0%
Applied egg-rr100.0%
Taylor expanded in a around 0 83.4%
log1p-define83.4%
Simplified83.4%
Applied egg-rr50.4%
(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 2024111
(FPCore (a b)
:name "Quotient of sum of exps"
:precision binary64
:alt
(/ 1.0 (+ 1.0 (exp (- b a))))
(/ (exp a) (+ (exp a) (exp b))))