
(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 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.6%
Final simplification99.6%
(FPCore (a b) :precision binary64 (if (<= (exp a) 1e-206) (exp a) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 1e-206) {
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 (exp(a) <= 1d-206) 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 (Math.exp(a) <= 1e-206) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 1e-206: tmp = math.exp(a) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 1e-206) 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 (exp(a) <= 1e-206) tmp = exp(a); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 1e-206], N[Exp[a], $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 10^{-206}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 1.00000000000000003e-206Initial program 100.0%
add-cbrt-cube98.7%
pow1/398.7%
pow-to-exp98.7%
pow398.7%
log-pow100.0%
log-div100.0%
add-log-exp100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
if 1.00000000000000003e-206 < (exp.f64 a) Initial program 99.4%
Taylor expanded in a around 0 98.2%
Final simplification98.7%
(FPCore (a b)
:precision binary64
(if (<= b -1.1)
(exp a)
(if (or (<= b 2500.0) (and (not (<= b 1.45e+118)) (<= b 3e+202)))
(/ (exp a) 2.0)
(* -0.020833333333333332 (pow a 3.0)))))
double code(double a, double b) {
double tmp;
if (b <= -1.1) {
tmp = exp(a);
} else if ((b <= 2500.0) || (!(b <= 1.45e+118) && (b <= 3e+202))) {
tmp = exp(a) / 2.0;
} else {
tmp = -0.020833333333333332 * pow(a, 3.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-1.1d0)) then
tmp = exp(a)
else if ((b <= 2500.0d0) .or. (.not. (b <= 1.45d+118)) .and. (b <= 3d+202)) then
tmp = exp(a) / 2.0d0
else
tmp = (-0.020833333333333332d0) * (a ** 3.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.1) {
tmp = Math.exp(a);
} else if ((b <= 2500.0) || (!(b <= 1.45e+118) && (b <= 3e+202))) {
tmp = Math.exp(a) / 2.0;
} else {
tmp = -0.020833333333333332 * Math.pow(a, 3.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.1: tmp = math.exp(a) elif (b <= 2500.0) or (not (b <= 1.45e+118) and (b <= 3e+202)): tmp = math.exp(a) / 2.0 else: tmp = -0.020833333333333332 * math.pow(a, 3.0) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.1) tmp = exp(a); elseif ((b <= 2500.0) || (!(b <= 1.45e+118) && (b <= 3e+202))) tmp = Float64(exp(a) / 2.0); else tmp = Float64(-0.020833333333333332 * (a ^ 3.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.1) tmp = exp(a); elseif ((b <= 2500.0) || (~((b <= 1.45e+118)) && (b <= 3e+202))) tmp = exp(a) / 2.0; else tmp = -0.020833333333333332 * (a ^ 3.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.1], N[Exp[a], $MachinePrecision], If[Or[LessEqual[b, 2500.0], And[N[Not[LessEqual[b, 1.45e+118]], $MachinePrecision], LessEqual[b, 3e+202]]], N[(N[Exp[a], $MachinePrecision] / 2.0), $MachinePrecision], N[(-0.020833333333333332 * N[Power[a, 3.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.1:\\
\;\;\;\;e^{a}\\
\mathbf{elif}\;b \leq 2500 \lor \neg \left(b \leq 1.45 \cdot 10^{+118}\right) \land b \leq 3 \cdot 10^{+202}:\\
\;\;\;\;\frac{e^{a}}{2}\\
\mathbf{else}:\\
\;\;\;\;-0.020833333333333332 \cdot {a}^{3}\\
\end{array}
\end{array}
if b < -1.1000000000000001Initial program 100.0%
add-cbrt-cube100.0%
pow1/3100.0%
pow-to-exp100.0%
pow3100.0%
log-pow100.0%
log-div100.0%
add-log-exp100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 95.9%
if -1.1000000000000001 < b < 2500 or 1.45000000000000008e118 < b < 3.0000000000000001e202Initial program 99.3%
Taylor expanded in b around 0 91.9%
Taylor expanded in a around 0 89.7%
if 2500 < b < 1.45000000000000008e118 or 3.0000000000000001e202 < b Initial program 100.0%
Taylor expanded in b around 0 25.7%
Taylor expanded in a around 0 2.9%
Taylor expanded in a around inf 55.4%
Final simplification85.4%
(FPCore (a b) :precision binary64 (if (or (<= a -5.2e-12) (and (not (<= a -2.45e-30)) (<= a -6.5e-81))) (exp a) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if ((a <= -5.2e-12) || (!(a <= -2.45e-30) && (a <= -6.5e-81))) {
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 ((a <= (-5.2d-12)) .or. (.not. (a <= (-2.45d-30))) .and. (a <= (-6.5d-81))) 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 ((a <= -5.2e-12) || (!(a <= -2.45e-30) && (a <= -6.5e-81))) {
tmp = Math.exp(a);
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if (a <= -5.2e-12) or (not (a <= -2.45e-30) and (a <= -6.5e-81)): tmp = math.exp(a) else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if ((a <= -5.2e-12) || (!(a <= -2.45e-30) && (a <= -6.5e-81))) 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 ((a <= -5.2e-12) || (~((a <= -2.45e-30)) && (a <= -6.5e-81))) tmp = exp(a); else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[a, -5.2e-12], And[N[Not[LessEqual[a, -2.45e-30]], $MachinePrecision], LessEqual[a, -6.5e-81]]], N[Exp[a], $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -5.2 \cdot 10^{-12} \lor \neg \left(a \leq -2.45 \cdot 10^{-30}\right) \land a \leq -6.5 \cdot 10^{-81}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if a < -5.19999999999999965e-12 or -2.44999999999999985e-30 < a < -6.5000000000000002e-81Initial program 100.0%
add-cbrt-cube98.9%
pow1/399.0%
pow-to-exp98.9%
pow398.9%
log-pow99.9%
log-div99.9%
add-log-exp99.9%
Applied egg-rr99.9%
Taylor expanded in a around inf 93.9%
if -5.19999999999999965e-12 < a < -2.44999999999999985e-30 or -6.5000000000000002e-81 < a Initial program 99.3%
Taylor expanded in b around 0 52.0%
Taylor expanded in a around 0 51.0%
*-commutative51.0%
Simplified51.0%
Final simplification66.9%
(FPCore (a b) :precision binary64 (if (<= b -1.1) (exp a) (/ (exp a) 2.0)))
double code(double a, double b) {
double tmp;
if (b <= -1.1) {
tmp = exp(a);
} else {
tmp = exp(a) / 2.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-1.1d0)) then
tmp = exp(a)
else
tmp = exp(a) / 2.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.1) {
tmp = Math.exp(a);
} else {
tmp = Math.exp(a) / 2.0;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.1: tmp = math.exp(a) else: tmp = math.exp(a) / 2.0 return tmp
function code(a, b) tmp = 0.0 if (b <= -1.1) tmp = exp(a); else tmp = Float64(exp(a) / 2.0); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.1) tmp = exp(a); else tmp = exp(a) / 2.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.1], N[Exp[a], $MachinePrecision], N[(N[Exp[a], $MachinePrecision] / 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.1:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{a}}{2}\\
\end{array}
\end{array}
if b < -1.1000000000000001Initial program 100.0%
add-cbrt-cube100.0%
pow1/3100.0%
pow-to-exp100.0%
pow3100.0%
log-pow100.0%
log-div100.0%
add-log-exp100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 95.9%
if -1.1000000000000001 < b Initial program 99.5%
Taylor expanded in b around 0 77.3%
Taylor expanded in a around 0 75.6%
Final simplification80.4%
(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.6%
Taylor expanded in b around 0 63.7%
Taylor expanded in a around 0 35.5%
*-commutative35.5%
Simplified35.5%
Final simplification35.5%
(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.6%
Taylor expanded in b around 0 63.7%
Taylor expanded in a around 0 35.2%
Final simplification35.2%
(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 2023189
(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))))