
(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.2%
*-lft-identity99.2%
associate-/l*99.2%
remove-double-div99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
*-commutative99.2%
distribute-rgt-in73.4%
exp-neg73.4%
rgt-mult-inverse100.0%
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 -1550000.0) 0.0 (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -1550000.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 <= (-1550000.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 <= -1550000.0) {
tmp = 0.0;
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -1550000.0: tmp = 0.0 else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -1550000.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 <= -1550000.0) tmp = 0.0; else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -1550000.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 -1550000:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -1.55e6Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in0.0%
exp-neg0.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 41.0%
Applied egg-rr60.9%
+-inverses100.0%
Simplified100.0%
if -1.55e6 < 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-in97.9%
exp-neg97.9%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 98.3%
Final simplification98.7%
(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.2%
*-lft-identity99.2%
associate-/l*99.2%
remove-double-div99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
*-commutative99.2%
distribute-rgt-in73.4%
exp-neg73.4%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b)
:precision binary64
(if (<= b -8.2)
1.0
(if (<= b 1.2e-11)
(/ 1.0 (+ 2.0 (- (* (* a a) (+ 0.5 (* a -0.16666666666666666))) a)))
0.0)))
double code(double a, double b) {
double tmp;
if (b <= -8.2) {
tmp = 1.0;
} else if (b <= 1.2e-11) {
tmp = 1.0 / (2.0 + (((a * a) * (0.5 + (a * -0.16666666666666666))) - a));
} 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 <= (-8.2d0)) then
tmp = 1.0d0
else if (b <= 1.2d-11) then
tmp = 1.0d0 / (2.0d0 + (((a * a) * (0.5d0 + (a * (-0.16666666666666666d0)))) - a))
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -8.2) {
tmp = 1.0;
} else if (b <= 1.2e-11) {
tmp = 1.0 / (2.0 + (((a * a) * (0.5 + (a * -0.16666666666666666))) - a));
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -8.2: tmp = 1.0 elif b <= 1.2e-11: tmp = 1.0 / (2.0 + (((a * a) * (0.5 + (a * -0.16666666666666666))) - a)) else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -8.2) tmp = 1.0; elseif (b <= 1.2e-11) tmp = Float64(1.0 / Float64(2.0 + Float64(Float64(Float64(a * a) * Float64(0.5 + Float64(a * -0.16666666666666666))) - a))); else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -8.2) tmp = 1.0; elseif (b <= 1.2e-11) tmp = 1.0 / (2.0 + (((a * a) * (0.5 + (a * -0.16666666666666666))) - a)); else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -8.2], 1.0, If[LessEqual[b, 1.2e-11], N[(1.0 / N[(2.0 + N[(N[(N[(a * a), $MachinePrecision] * N[(0.5 + N[(a * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -8.2:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 1.2 \cdot 10^{-11}:\\
\;\;\;\;\frac{1}{2 + \left(\left(a \cdot a\right) \cdot \left(0.5 + a \cdot -0.16666666666666666\right) - a\right)}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -8.1999999999999993Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg99.9%
associate-/r/99.9%
/-rgt-identity99.9%
*-commutative99.9%
distribute-rgt-in99.9%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -8.1999999999999993 < b < 1.2000000000000001e-11Initial program 98.4%
*-lft-identity98.4%
associate-/l*98.4%
remove-double-div98.4%
exp-neg98.4%
associate-/r/98.4%
/-rgt-identity98.4%
*-commutative98.4%
distribute-rgt-in68.0%
exp-neg68.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in b around 0 99.8%
Taylor expanded in a around 0 84.9%
+-commutative84.9%
mul-1-neg84.9%
unsub-neg84.9%
+-commutative84.9%
cube-mult84.9%
unpow284.9%
associate-*r*84.9%
distribute-rgt-out84.9%
unpow284.9%
*-commutative84.9%
Simplified84.9%
if 1.2000000000000001e-11 < b Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg100.0%
associate-/r/100.0%
/-rgt-identity100.0%
*-commutative100.0%
distribute-rgt-in62.6%
exp-neg62.6%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 98.7%
Applied egg-rr1.5%
+-inverses96.1%
Simplified96.1%
Final simplification91.5%
(FPCore (a b) :precision binary64 (if (<= b -0.098) 1.0 (if (<= b 6.7e-17) 0.5 0.0)))
double code(double a, double b) {
double tmp;
if (b <= -0.098) {
tmp = 1.0;
} else if (b <= 6.7e-17) {
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.098d0)) then
tmp = 1.0d0
else if (b <= 6.7d-17) 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.098) {
tmp = 1.0;
} else if (b <= 6.7e-17) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.098: tmp = 1.0 elif b <= 6.7e-17: tmp = 0.5 else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -0.098) tmp = 1.0; elseif (b <= 6.7e-17) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.098) tmp = 1.0; elseif (b <= 6.7e-17) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.098], 1.0, If[LessEqual[b, 6.7e-17], 0.5, 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.098:\\
\;\;\;\;1\\
\mathbf{elif}\;b \leq 6.7 \cdot 10^{-17}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -0.098000000000000004Initial program 100.0%
*-lft-identity100.0%
associate-/l*100.0%
remove-double-div100.0%
exp-neg99.9%
associate-/r/99.9%
/-rgt-identity99.9%
*-commutative99.9%
distribute-rgt-in99.9%
exp-neg100.0%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 100.0%
Applied egg-rr100.0%
*-inverses100.0%
Simplified100.0%
if -0.098000000000000004 < b < 6.7000000000000004e-17Initial program 98.4%
*-lft-identity98.4%
associate-/l*98.4%
remove-double-div98.4%
exp-neg98.4%
associate-/r/98.4%
/-rgt-identity98.4%
*-commutative98.4%
distribute-rgt-in68.9%
exp-neg68.9%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 68.8%
Taylor expanded in b around 0 68.8%
if 6.7000000000000004e-17 < b Initial program 99.9%
*-lft-identity99.9%
associate-/l*99.9%
remove-double-div99.9%
exp-neg99.9%
associate-/r/99.9%
/-rgt-identity99.9%
*-commutative99.9%
distribute-rgt-in61.5%
exp-neg61.5%
rgt-mult-inverse99.9%
prod-exp99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in a around 0 96.2%
Applied egg-rr4.0%
+-inverses95.0%
Simplified95.0%
Final simplification83.6%
(FPCore (a b) :precision binary64 (if (<= b 6.7e-17) 0.5 0.0))
double code(double a, double b) {
double tmp;
if (b <= 6.7e-17) {
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 <= 6.7d-17) 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 <= 6.7e-17) {
tmp = 0.5;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 6.7e-17: tmp = 0.5 else: tmp = 0.0 return tmp
function code(a, b) tmp = 0.0 if (b <= 6.7e-17) tmp = 0.5; else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 6.7e-17) tmp = 0.5; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 6.7e-17], 0.5, 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.7 \cdot 10^{-17}:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < 6.7000000000000004e-17Initial program 98.9%
*-lft-identity98.9%
associate-/l*98.9%
remove-double-div98.9%
exp-neg98.9%
associate-/r/98.8%
/-rgt-identity98.8%
*-commutative98.8%
distribute-rgt-in78.6%
exp-neg78.6%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 78.6%
Taylor expanded in b around 0 53.1%
if 6.7000000000000004e-17 < b Initial program 99.9%
*-lft-identity99.9%
associate-/l*99.9%
remove-double-div99.9%
exp-neg99.9%
associate-/r/99.9%
/-rgt-identity99.9%
*-commutative99.9%
distribute-rgt-in61.5%
exp-neg61.5%
rgt-mult-inverse99.9%
prod-exp99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in a around 0 96.2%
Applied egg-rr4.0%
+-inverses95.0%
Simplified95.0%
Final simplification65.8%
(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.2%
*-lft-identity99.2%
associate-/l*99.2%
remove-double-div99.2%
exp-neg99.2%
associate-/r/99.2%
/-rgt-identity99.2%
*-commutative99.2%
distribute-rgt-in73.4%
exp-neg73.4%
rgt-mult-inverse100.0%
prod-exp100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in a around 0 84.0%
Applied egg-rr17.0%
+-inverses44.8%
Simplified44.8%
Final simplification44.8%
(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 2023290
(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))))