
(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 (/ 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}
Initial program 98.0%
*-lft-identity98.0%
associate-*l/98.0%
associate-/r/98.0%
remove-double-neg98.0%
unsub-neg98.0%
div-sub69.1%
*-lft-identity69.1%
associate-*l/69.1%
lft-mult-inverse98.8%
sub-neg98.8%
distribute-frac-neg98.8%
remove-double-neg98.8%
div-exp100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= a -47000000.0) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -47000000.0) {
tmp = 1.0 / (1.0 + 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 <= (-47000000.0d0)) then
tmp = 1.0d0 / (1.0d0 + 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 <= -47000000.0) {
tmp = 1.0 / (1.0 + Math.exp(-a));
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -47000000.0: tmp = 1.0 / (1.0 + math.exp(-a)) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -47000000.0) tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-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 <= -47000000.0) tmp = 1.0 / (1.0 + exp(-a)); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -47000000.0], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -47000000:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -4.7e7Initial program 98.6%
*-lft-identity98.6%
associate-*l/98.6%
associate-/r/98.6%
remove-double-neg98.6%
unsub-neg98.6%
div-sub0.0%
*-lft-identity0.0%
associate-*l/0.0%
lft-mult-inverse98.6%
sub-neg98.6%
distribute-frac-neg98.6%
remove-double-neg98.6%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 100.0%
if -4.7e7 < a Initial program 97.8%
*-lft-identity97.8%
associate-*l/97.8%
associate-/r/97.8%
remove-double-neg97.8%
unsub-neg97.8%
div-sub97.2%
*-lft-identity97.2%
associate-*l/97.2%
lft-mult-inverse98.9%
sub-neg98.9%
distribute-frac-neg98.9%
remove-double-neg98.9%
div-exp100.0%
Simplified100.0%
Taylor expanded in a around 0 98.2%
Final simplification98.7%
(FPCore (a b) :precision binary64 (/ 1.0 (+ 1.0 (exp b))))
double code(double a, double b) {
return 1.0 / (1.0 + exp(b));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (1.0d0 + exp(b))
end function
public static double code(double a, double b) {
return 1.0 / (1.0 + Math.exp(b));
}
def code(a, b): return 1.0 / (1.0 + math.exp(b))
function code(a, b) return Float64(1.0 / Float64(1.0 + exp(b))) end
function tmp = code(a, b) tmp = 1.0 / (1.0 + exp(b)); end
code[a_, b_] := N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + e^{b}}
\end{array}
Initial program 98.0%
*-lft-identity98.0%
associate-*l/98.0%
associate-/r/98.0%
remove-double-neg98.0%
unsub-neg98.0%
div-sub69.1%
*-lft-identity69.1%
associate-*l/69.1%
lft-mult-inverse98.8%
sub-neg98.8%
distribute-frac-neg98.8%
remove-double-neg98.8%
div-exp100.0%
Simplified100.0%
Taylor expanded in a around 0 78.7%
Final simplification78.7%
(FPCore (a b) :precision binary64 (if (<= b 6e-23) (+ 0.5 (* a 0.25)) (/ 1.0 (+ 2.0 (* b (+ 1.0 (* b (+ 0.5 (* b 0.16666666666666666)))))))))
double code(double a, double b) {
double tmp;
if (b <= 6e-23) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * (0.5 + (b * 0.16666666666666666))))));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 6d-23) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 1.0d0 / (2.0d0 + (b * (1.0d0 + (b * (0.5d0 + (b * 0.16666666666666666d0))))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 6e-23) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * (0.5 + (b * 0.16666666666666666))))));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 6e-23: tmp = 0.5 + (a * 0.25) else: tmp = 1.0 / (2.0 + (b * (1.0 + (b * (0.5 + (b * 0.16666666666666666)))))) return tmp
function code(a, b) tmp = 0.0 if (b <= 6e-23) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(1.0 / Float64(2.0 + Float64(b * Float64(1.0 + Float64(b * Float64(0.5 + Float64(b * 0.16666666666666666))))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 6e-23) tmp = 0.5 + (a * 0.25); else tmp = 1.0 / (2.0 + (b * (1.0 + (b * (0.5 + (b * 0.16666666666666666)))))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 6e-23], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(2.0 + N[(b * N[(1.0 + N[(b * N[(0.5 + N[(b * 0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6 \cdot 10^{-23}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + b \cdot \left(1 + b \cdot \left(0.5 + b \cdot 0.16666666666666666\right)\right)}\\
\end{array}
\end{array}
if b < 6.00000000000000006e-23Initial program 97.8%
*-lft-identity97.8%
associate-*l/97.8%
associate-/r/97.8%
remove-double-neg97.8%
unsub-neg97.8%
div-sub71.4%
*-lft-identity71.4%
associate-*l/71.4%
lft-mult-inverse98.9%
sub-neg98.9%
distribute-frac-neg98.9%
remove-double-neg98.9%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 79.8%
Taylor expanded in a around 0 52.5%
*-commutative52.5%
Simplified52.5%
if 6.00000000000000006e-23 < b Initial program 98.6%
*-lft-identity98.6%
associate-*l/98.6%
associate-/r/98.6%
remove-double-neg98.6%
unsub-neg98.6%
div-sub63.4%
*-lft-identity63.4%
associate-*l/63.4%
lft-mult-inverse98.6%
sub-neg98.6%
distribute-frac-neg98.6%
remove-double-neg98.6%
div-exp100.0%
Simplified100.0%
Taylor expanded in a around 0 95.9%
Taylor expanded in b around 0 68.3%
*-commutative68.3%
Simplified68.3%
Final simplification56.9%
(FPCore (a b) :precision binary64 (if (<= b 3.1e-21) (+ 0.5 (* a 0.25)) (/ 1.0 (+ 2.0 (* b (+ 1.0 (* b 0.5)))))))
double code(double a, double b) {
double tmp;
if (b <= 3.1e-21) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * 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 <= 3.1d-21) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 1.0d0 / (2.0d0 + (b * (1.0d0 + (b * 0.5d0))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 3.1e-21) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5))));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 3.1e-21: tmp = 0.5 + (a * 0.25) else: tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5)))) return tmp
function code(a, b) tmp = 0.0 if (b <= 3.1e-21) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(1.0 / Float64(2.0 + Float64(b * Float64(1.0 + Float64(b * 0.5))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 3.1e-21) tmp = 0.5 + (a * 0.25); else tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 3.1e-21], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(2.0 + N[(b * N[(1.0 + N[(b * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.1 \cdot 10^{-21}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + b \cdot \left(1 + b \cdot 0.5\right)}\\
\end{array}
\end{array}
if b < 3.0999999999999998e-21Initial program 97.8%
*-lft-identity97.8%
associate-*l/97.8%
associate-/r/97.8%
remove-double-neg97.8%
unsub-neg97.8%
div-sub71.4%
*-lft-identity71.4%
associate-*l/71.4%
lft-mult-inverse98.9%
sub-neg98.9%
distribute-frac-neg98.9%
remove-double-neg98.9%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 79.8%
Taylor expanded in a around 0 52.5%
*-commutative52.5%
Simplified52.5%
if 3.0999999999999998e-21 < b Initial program 98.6%
*-lft-identity98.6%
associate-*l/98.6%
associate-/r/98.6%
remove-double-neg98.6%
unsub-neg98.6%
div-sub63.4%
*-lft-identity63.4%
associate-*l/63.4%
lft-mult-inverse98.6%
sub-neg98.6%
distribute-frac-neg98.6%
remove-double-neg98.6%
div-exp100.0%
Simplified100.0%
Taylor expanded in a around 0 95.9%
Taylor expanded in b around 0 49.7%
*-commutative49.7%
Simplified49.7%
Final simplification51.7%
(FPCore (a b) :precision binary64 (if (<= b 660.0) (+ 0.5 (* a 0.25)) (/ (/ -2.0 b) b)))
double code(double a, double b) {
double tmp;
if (b <= 660.0) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = (-2.0 / b) / b;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 660.0d0) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = ((-2.0d0) / b) / b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 660.0) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = (-2.0 / b) / b;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 660.0: tmp = 0.5 + (a * 0.25) else: tmp = (-2.0 / b) / b return tmp
function code(a, b) tmp = 0.0 if (b <= 660.0) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(Float64(-2.0 / b) / b); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 660.0) tmp = 0.5 + (a * 0.25); else tmp = (-2.0 / b) / b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 660.0], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 / b), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 660:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{-2}{b}}{b}\\
\end{array}
\end{array}
if b < 660Initial program 97.9%
*-lft-identity97.9%
associate-*l/97.9%
associate-/r/97.9%
remove-double-neg97.9%
unsub-neg97.9%
div-sub71.0%
*-lft-identity71.0%
associate-*l/71.0%
lft-mult-inverse99.0%
sub-neg99.0%
distribute-frac-neg99.0%
remove-double-neg99.0%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 80.0%
Taylor expanded in a around 0 52.2%
*-commutative52.2%
Simplified52.2%
if 660 < b Initial program 98.4%
*-lft-identity98.4%
associate-*l/98.4%
associate-/r/98.4%
remove-double-neg98.4%
unsub-neg98.4%
div-sub63.5%
*-lft-identity63.5%
associate-*l/63.5%
lft-mult-inverse98.4%
sub-neg98.4%
distribute-frac-neg98.4%
remove-double-neg98.4%
div-exp100.0%
Simplified100.0%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 5.5%
+-commutative5.5%
Simplified5.5%
Taylor expanded in b around inf 5.5%
associate-*r/5.5%
metadata-eval5.5%
Simplified5.5%
Taylor expanded in b around 0 48.4%
Final simplification51.3%
(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.0%
*-lft-identity98.0%
associate-*l/98.0%
associate-/r/98.0%
remove-double-neg98.0%
unsub-neg98.0%
div-sub69.1%
*-lft-identity69.1%
associate-*l/69.1%
lft-mult-inverse98.8%
sub-neg98.8%
distribute-frac-neg98.8%
remove-double-neg98.8%
div-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 69.4%
Taylor expanded in a around 0 40.0%
*-commutative40.0%
Simplified40.0%
Final simplification40.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.0%
*-lft-identity98.0%
associate-*l/98.0%
associate-/r/98.0%
remove-double-neg98.0%
unsub-neg98.0%
div-sub69.1%
*-lft-identity69.1%
associate-*l/69.1%
lft-mult-inverse98.8%
sub-neg98.8%
distribute-frac-neg98.8%
remove-double-neg98.8%
div-exp100.0%
Simplified100.0%
Taylor expanded in a around 0 78.7%
Taylor expanded in b around 0 40.0%
Final simplification40.0%
(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 2024060
(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))))