
(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 (if (or (<= (exp b) 0.0) (not (<= (exp b) 1.0000002))) (/ 1.0 (+ (exp b) 1.0)) (/ 1.0 (+ 1.0 (exp (- a))))))
double code(double a, double b) {
double tmp;
if ((exp(b) <= 0.0) || !(exp(b) <= 1.0000002)) {
tmp = 1.0 / (exp(b) + 1.0);
} else {
tmp = 1.0 / (1.0 + exp(-a));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((exp(b) <= 0.0d0) .or. (.not. (exp(b) <= 1.0000002d0))) then
tmp = 1.0d0 / (exp(b) + 1.0d0)
else
tmp = 1.0d0 / (1.0d0 + exp(-a))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((Math.exp(b) <= 0.0) || !(Math.exp(b) <= 1.0000002)) {
tmp = 1.0 / (Math.exp(b) + 1.0);
} else {
tmp = 1.0 / (1.0 + Math.exp(-a));
}
return tmp;
}
def code(a, b): tmp = 0 if (math.exp(b) <= 0.0) or not (math.exp(b) <= 1.0000002): tmp = 1.0 / (math.exp(b) + 1.0) else: tmp = 1.0 / (1.0 + math.exp(-a)) return tmp
function code(a, b) tmp = 0.0 if ((exp(b) <= 0.0) || !(exp(b) <= 1.0000002)) tmp = Float64(1.0 / Float64(exp(b) + 1.0)); else tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-a)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((exp(b) <= 0.0) || ~((exp(b) <= 1.0000002))) tmp = 1.0 / (exp(b) + 1.0); else tmp = 1.0 / (1.0 + exp(-a)); end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[N[Exp[b], $MachinePrecision], 0.0], N[Not[LessEqual[N[Exp[b], $MachinePrecision], 1.0000002]], $MachinePrecision]], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0 \lor \neg \left(e^{b} \leq 1.0000002\right):\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\end{array}
\end{array}
if (exp.f64 b) < 0.0 or 1.00000019999999989 < (exp.f64 b) Initial program 99.1%
Taylor expanded in a around 0 100.0%
if 0.0 < (exp.f64 b) < 1.00000019999999989Initial program 99.2%
add-exp-log99.2%
div-exp99.3%
Applied egg-rr99.3%
Taylor expanded in b around 0 99.3%
log1p-def99.3%
Simplified99.3%
exp-diff99.2%
log1p-udef99.2%
add-exp-log99.2%
frac-2neg99.2%
distribute-neg-in99.2%
metadata-eval99.2%
Applied egg-rr99.2%
neg-mul-199.2%
associate-/l*99.2%
unsub-neg99.2%
div-sub69.1%
sub-neg69.1%
*-inverses100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in a around inf 100.0%
rec-exp100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (exp (- a (log (+ (exp a) (exp b))))))
double code(double a, double b) {
return exp((a - log((exp(a) + exp(b)))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp((a - log((exp(a) + exp(b)))))
end function
public static double code(double a, double b) {
return Math.exp((a - Math.log((Math.exp(a) + Math.exp(b)))));
}
def code(a, b): return math.exp((a - math.log((math.exp(a) + math.exp(b)))))
function code(a, b) return exp(Float64(a - log(Float64(exp(a) + exp(b))))) end
function tmp = code(a, b) tmp = exp((a - log((exp(a) + exp(b))))); end
code[a_, b_] := N[Exp[N[(a - N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
e^{a - \log \left(e^{a} + e^{b}\right)}
\end{array}
Initial program 99.2%
add-exp-log99.2%
div-exp99.2%
Applied egg-rr99.2%
Final simplification99.2%
(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}
Initial program 99.2%
Final simplification99.2%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.0) (exp a) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.0) {
tmp = exp(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 (exp(a) <= 0.0d0) then
tmp = exp(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 (Math.exp(a) <= 0.0) {
tmp = Math.exp(a);
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.0: tmp = math.exp(a) else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.0) tmp = exp(a); else tmp = Float64(0.5 + Float64(a * 0.25)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 0.0) tmp = exp(a); else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.0], N[Exp[a], $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if (exp.f64 a) < 0.0Initial program 98.5%
add-exp-log98.5%
div-exp98.5%
Applied egg-rr98.5%
Taylor expanded in a around inf 98.5%
if 0.0 < (exp.f64 a) Initial program 99.4%
clear-num99.4%
associate-/r/99.4%
Applied egg-rr99.4%
Taylor expanded in b around 0 55.8%
Taylor expanded in a around 0 54.9%
Final simplification66.0%
(FPCore (a b) :precision binary64 (if (<= a -38000.0) (exp a) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (a <= -38000.0) {
tmp = exp(a);
} else {
tmp = 1.0 / (exp(b) + 1.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-38000.0d0)) then
tmp = exp(a)
else
tmp = 1.0d0 / (exp(b) + 1.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -38000.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -38000.0: tmp = math.exp(a) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (a <= -38000.0) tmp = exp(a); else tmp = Float64(1.0 / Float64(exp(b) + 1.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -38000.0) tmp = exp(a); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -38000.0], N[Exp[a], $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -38000:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if a < -38000Initial program 100.0%
add-exp-log100.0%
div-exp100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
if -38000 < a Initial program 98.9%
Taylor expanded in a around 0 97.3%
Final simplification98.0%
(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 99.2%
clear-num99.2%
associate-/r/99.2%
Applied egg-rr99.2%
Taylor expanded in b around 0 66.6%
Taylor expanded in a around 0 41.5%
Final simplification41.5%
(FPCore (a b) :precision binary64 (/ -1.0 (- a 2.0)))
double code(double a, double b) {
return -1.0 / (a - 2.0);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (-1.0d0) / (a - 2.0d0)
end function
public static double code(double a, double b) {
return -1.0 / (a - 2.0);
}
def code(a, b): return -1.0 / (a - 2.0)
function code(a, b) return Float64(-1.0 / Float64(a - 2.0)) end
function tmp = code(a, b) tmp = -1.0 / (a - 2.0); end
code[a_, b_] := N[(-1.0 / N[(a - 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{a - 2}
\end{array}
Initial program 99.2%
add-exp-log99.2%
div-exp99.2%
Applied egg-rr99.2%
Taylor expanded in b around 0 66.7%
log1p-def66.7%
Simplified66.7%
exp-diff66.6%
log1p-udef66.6%
add-exp-log66.6%
frac-2neg66.6%
distribute-neg-in66.6%
metadata-eval66.6%
Applied egg-rr66.6%
neg-mul-166.6%
associate-/l*66.6%
unsub-neg66.6%
div-sub41.6%
sub-neg41.6%
*-inverses67.0%
metadata-eval67.0%
Simplified67.0%
Taylor expanded in a around 0 41.8%
Final simplification41.8%
(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 99.2%
Taylor expanded in a around 0 82.5%
Taylor expanded in b around 0 40.6%
Final simplification40.6%
(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 2023338
(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))))