
(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 (if (<= (exp a) 5e-128) (/ (exp a) (+ (exp a) 1.0)) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 5e-128) {
tmp = exp(a) / (exp(a) + 1.0);
} 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 (exp(a) <= 5d-128) then
tmp = exp(a) / (exp(a) + 1.0d0)
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 (Math.exp(a) <= 5e-128) {
tmp = Math.exp(a) / (Math.exp(a) + 1.0);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 5e-128: tmp = math.exp(a) / (math.exp(a) + 1.0) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 5e-128) tmp = Float64(exp(a) / Float64(exp(a) + 1.0)); else tmp = Float64(1.0 / Float64(exp(b) + 1.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 5e-128) tmp = exp(a) / (exp(a) + 1.0); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 5e-128], N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 5 \cdot 10^{-128}:\\
\;\;\;\;\frac{e^{a}}{e^{a} + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 5.0000000000000001e-128Initial program 100.0%
Taylor expanded in b around 0 100.0%
if 5.0000000000000001e-128 < (exp.f64 a) Initial program 98.4%
Taylor expanded in a around 0 98.6%
Final simplification99.0%
(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 98.8%
Final simplification98.8%
(FPCore (a b) :precision binary64 (if (<= a -700.0) 0.0 (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (a <= -700.0) {
tmp = 0.0;
} 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 <= (-700.0d0)) then
tmp = 0.0d0
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 <= -700.0) {
tmp = 0.0;
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -700.0: tmp = 0.0 else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (a <= -700.0) tmp = 0.0; 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 <= -700.0) tmp = 0.0; else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -700.0], 0.0, N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -700:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if a < -700Initial program 100.0%
Taylor expanded in a around 0 45.2%
+-commutative45.2%
flip-+0.2%
pow20.2%
metadata-eval0.2%
expm1-undefine1.0%
Applied egg-rr1.0%
unpow21.0%
prod-exp1.0%
expm1-define1.8%
Simplified1.8%
add-sqr-sqrt1.8%
sqrt-unprod1.8%
pow21.8%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified45.2%
Taylor expanded in b around 0 3.1%
Simplified100.0%
if -700 < a Initial program 98.4%
Taylor expanded in a around 0 98.1%
Final simplification98.6%
(FPCore (a b) :precision binary64 (if (<= b -2.7e-6) 1.0 (if (<= b 3e-40) (+ 0.5 (* a 0.25)) 0.0)))
double code(double a, double b) {
double tmp;
if (b <= -2.7e-6) {
tmp = 1.0;
} else if (b <= 3e-40) {
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 <= (-2.7d-6)) then
tmp = 1.0d0
else if (b <= 3d-40) 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 <= -2.7e-6) {
tmp = 1.0;
} else if (b <= 3e-40) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -2.7e-6: tmp = 1.0 elif b <= 3e-40: tmp = 0.5 + (a * 0.25) else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -2.7e-6) tmp = 1.0; elseif (b <= 3e-40) 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 <= -2.7e-6) tmp = 1.0; elseif (b <= 3e-40) tmp = 0.5 + (a * 0.25); else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -2.7e-6], 1.0, If[LessEqual[b, 3e-40], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.7 \cdot 10^{-6}:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 3 \cdot 10^{-40}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -2.69999999999999998e-6Initial program 95.3%
Taylor expanded in a around 0 93.6%
+-commutative93.6%
flip-+93.4%
pow293.4%
metadata-eval93.4%
expm1-undefine93.4%
Applied egg-rr93.4%
unpow293.4%
prod-exp93.5%
expm1-define93.6%
Simplified93.6%
add-sqr-sqrt93.6%
sqrt-unprod93.6%
pow293.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified4.9%
Taylor expanded in b around 0 91.7%
if -2.69999999999999998e-6 < b < 3.0000000000000002e-40Initial program 100.0%
Taylor expanded in b around 0 99.8%
Taylor expanded in a around 0 74.8%
*-commutative74.8%
Simplified74.8%
if 3.0000000000000002e-40 < b Initial program 98.9%
Taylor expanded in a around 0 91.0%
+-commutative91.0%
flip-+0.7%
pow20.7%
metadata-eval0.7%
expm1-undefine0.9%
Applied egg-rr0.9%
unpow20.9%
prod-exp0.9%
expm1-define3.6%
Simplified3.6%
add-sqr-sqrt3.6%
sqrt-unprod3.6%
pow23.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified88.5%
Taylor expanded in b around 0 3.9%
Simplified95.9%
Final simplification85.5%
(FPCore (a b) :precision binary64 (if (<= b -2.7e-6) 1.0 (if (<= b 3e-40) 0.5 0.0)))
double code(double a, double b) {
double tmp;
if (b <= -2.7e-6) {
tmp = 1.0;
} else if (b <= 3e-40) {
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.7d-6)) then
tmp = 1.0d0
else if (b <= 3d-40) 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.7e-6) {
tmp = 1.0;
} else if (b <= 3e-40) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -2.7e-6: tmp = 1.0 elif b <= 3e-40: tmp = 0.5 else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -2.7e-6) tmp = 1.0; elseif (b <= 3e-40) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -2.7e-6) tmp = 1.0; elseif (b <= 3e-40) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -2.7e-6], 1.0, If[LessEqual[b, 3e-40], 0.5, 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.7 \cdot 10^{-6}:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 3 \cdot 10^{-40}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -2.69999999999999998e-6Initial program 95.3%
Taylor expanded in a around 0 93.6%
+-commutative93.6%
flip-+93.4%
pow293.4%
metadata-eval93.4%
expm1-undefine93.4%
Applied egg-rr93.4%
unpow293.4%
prod-exp93.5%
expm1-define93.6%
Simplified93.6%
add-sqr-sqrt93.6%
sqrt-unprod93.6%
pow293.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified4.9%
Taylor expanded in b around 0 91.7%
if -2.69999999999999998e-6 < b < 3.0000000000000002e-40Initial program 100.0%
Taylor expanded in a around 0 74.5%
Taylor expanded in b around 0 74.3%
if 3.0000000000000002e-40 < b Initial program 98.9%
Taylor expanded in a around 0 91.0%
+-commutative91.0%
flip-+0.7%
pow20.7%
metadata-eval0.7%
expm1-undefine0.9%
Applied egg-rr0.9%
unpow20.9%
prod-exp0.9%
expm1-define3.6%
Simplified3.6%
add-sqr-sqrt3.6%
sqrt-unprod3.6%
pow23.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified88.5%
Taylor expanded in b around 0 3.9%
Simplified95.9%
Final simplification85.2%
(FPCore (a b) :precision binary64 (if (<= b 3e-40) 0.5 0.0))
double code(double a, double b) {
double tmp;
if (b <= 3e-40) {
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 <= 3d-40) 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 <= 3e-40) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 3e-40: tmp = 0.5 else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= 3e-40) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 3e-40) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 3e-40], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3 \cdot 10^{-40}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < 3.0000000000000002e-40Initial program 98.7%
Taylor expanded in a around 0 79.6%
Taylor expanded in b around 0 59.4%
if 3.0000000000000002e-40 < b Initial program 98.9%
Taylor expanded in a around 0 91.0%
+-commutative91.0%
flip-+0.7%
pow20.7%
metadata-eval0.7%
expm1-undefine0.9%
Applied egg-rr0.9%
unpow20.9%
prod-exp0.9%
expm1-define3.6%
Simplified3.6%
add-sqr-sqrt3.6%
sqrt-unprod3.6%
pow23.6%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified88.5%
Taylor expanded in b around 0 3.9%
Simplified95.9%
Final simplification72.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 98.8%
Taylor expanded in a around 0 83.8%
+-commutative83.8%
flip-+16.3%
pow216.3%
metadata-eval16.3%
expm1-undefine17.1%
Applied egg-rr17.1%
unpow217.1%
prod-exp17.1%
expm1-define51.4%
Simplified51.4%
add-sqr-sqrt50.6%
sqrt-unprod51.4%
pow251.4%
flip-+0.0%
+-inverses0.0%
+-inverses0.0%
Applied egg-rr0.0%
Simplified40.5%
Taylor expanded in b around 0 23.7%
Simplified49.3%
Final simplification49.3%
(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 2024040
(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))))