
(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 (/ 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.8%
add-cbrt-cube98.3%
pow1/398.8%
pow398.8%
clear-num98.8%
inv-pow98.8%
pow-pow98.8%
metadata-eval98.8%
Applied egg-rr98.8%
unpow1/398.3%
+-commutative98.3%
Simplified98.3%
Taylor expanded in b around inf 98.8%
+-commutative98.8%
rem-exp-log98.8%
div-exp98.8%
sub-neg98.8%
+-commutative98.8%
neg-sub098.8%
associate-+l-98.8%
metadata-eval98.8%
rem-log-exp98.8%
log-div98.8%
log-div98.8%
unpow-198.8%
rem-exp-log98.8%
unpow-198.8%
rem-exp-log98.8%
exp-diff98.8%
Simplified100.0%
Final simplification100.0%
(FPCore (a b)
:precision binary64
(if (<= (exp b) 0.0001)
(+ 1.0 (exp b))
(if (<= (exp b) 2e+19)
(/ 1.0 (- 2.0 a))
(* -0.020833333333333332 (pow a 3.0)))))
double code(double a, double b) {
double tmp;
if (exp(b) <= 0.0001) {
tmp = 1.0 + exp(b);
} else if (exp(b) <= 2e+19) {
tmp = 1.0 / (2.0 - a);
} 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 (exp(b) <= 0.0001d0) then
tmp = 1.0d0 + exp(b)
else if (exp(b) <= 2d+19) then
tmp = 1.0d0 / (2.0d0 - a)
else
tmp = (-0.020833333333333332d0) * (a ** 3.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(b) <= 0.0001) {
tmp = 1.0 + Math.exp(b);
} else if (Math.exp(b) <= 2e+19) {
tmp = 1.0 / (2.0 - a);
} else {
tmp = -0.020833333333333332 * Math.pow(a, 3.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(b) <= 0.0001: tmp = 1.0 + math.exp(b) elif math.exp(b) <= 2e+19: tmp = 1.0 / (2.0 - a) else: tmp = -0.020833333333333332 * math.pow(a, 3.0) return tmp
function code(a, b) tmp = 0.0 if (exp(b) <= 0.0001) tmp = Float64(1.0 + exp(b)); elseif (exp(b) <= 2e+19) tmp = Float64(1.0 / Float64(2.0 - a)); else tmp = Float64(-0.020833333333333332 * (a ^ 3.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(b) <= 0.0001) tmp = 1.0 + exp(b); elseif (exp(b) <= 2e+19) tmp = 1.0 / (2.0 - a); else tmp = -0.020833333333333332 * (a ^ 3.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[b], $MachinePrecision], 0.0001], N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Exp[b], $MachinePrecision], 2e+19], N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision], N[(-0.020833333333333332 * N[Power[a, 3.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.0001:\\
\;\;\;\;1 + e^{b}\\
\mathbf{elif}\;e^{b} \leq 2 \cdot 10^{+19}:\\
\;\;\;\;\frac{1}{2 - a}\\
\mathbf{else}:\\
\;\;\;\;-0.020833333333333332 \cdot {a}^{3}\\
\end{array}
\end{array}
if (exp.f64 b) < 1.00000000000000005e-4Initial program 96.5%
add-cbrt-cube96.5%
pow1/396.5%
pow-to-exp96.5%
pow396.5%
log-pow96.5%
log-div96.5%
add-log-exp96.6%
Applied egg-rr96.6%
Taylor expanded in a around 0 100.0%
neg-mul-1100.0%
log1p-def100.0%
Simplified100.0%
add-sqr-sqrt98.3%
sqrt-unprod98.9%
sqr-neg98.9%
sqrt-unprod98.9%
add-sqr-sqrt98.9%
log1p-udef98.9%
rem-exp-log98.9%
+-commutative98.9%
Applied egg-rr98.9%
if 1.00000000000000005e-4 < (exp.f64 b) < 2e19Initial program 99.3%
add-cbrt-cube98.3%
pow1/399.2%
pow399.2%
clear-num99.2%
inv-pow99.2%
pow-pow99.2%
metadata-eval99.2%
Applied egg-rr99.2%
unpow1/398.3%
+-commutative98.3%
Simplified98.3%
Taylor expanded in b around inf 99.3%
+-commutative99.3%
rem-exp-log99.3%
div-exp99.3%
sub-neg99.3%
+-commutative99.3%
neg-sub099.3%
associate-+l-99.3%
metadata-eval99.3%
rem-log-exp99.3%
log-div99.3%
log-div99.3%
unpow-199.3%
rem-exp-log99.3%
unpow-199.3%
rem-exp-log99.3%
exp-diff99.3%
Simplified100.0%
Taylor expanded in b around 0 98.8%
Taylor expanded in a around 0 63.6%
mul-1-neg63.6%
unsub-neg63.6%
Simplified63.6%
if 2e19 < (exp.f64 b) Initial program 100.0%
Taylor expanded in b around 0 26.9%
Taylor expanded in a around 0 2.8%
Taylor expanded in a around inf 40.9%
Final simplification66.6%
(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-cbrt-cube98.5%
pow1/398.5%
pow-to-exp98.5%
pow398.5%
log-pow98.5%
log-div98.5%
add-log-exp98.5%
Applied egg-rr98.5%
Taylor expanded in a around inf 98.5%
if 0.0 < (exp.f64 a) Initial program 98.9%
Taylor expanded in b around 0 52.4%
Taylor expanded in a around 0 52.2%
*-commutative52.2%
Simplified52.2%
Final simplification63.9%
(FPCore (a b) :precision binary64 (if (<= a -800.0) (exp a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -800.0) {
tmp = 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 <= (-800.0d0)) then
tmp = 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 <= -800.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -800.0: tmp = math.exp(a) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -800.0) tmp = exp(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 <= -800.0) tmp = exp(a); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -800.0], N[Exp[a], $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -800:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -800Initial 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 100.0%
if -800 < a Initial program 98.4%
Taylor expanded in a around 0 98.4%
Final simplification98.8%
(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.8%
Taylor expanded in b around 0 64.1%
Taylor expanded in a around 0 39.5%
*-commutative39.5%
Simplified39.5%
Final simplification39.5%
(FPCore (a b) :precision binary64 (/ 1.0 (- 2.0 a)))
double code(double a, double b) {
return 1.0 / (2.0 - a);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (2.0d0 - a)
end function
public static double code(double a, double b) {
return 1.0 / (2.0 - a);
}
def code(a, b): return 1.0 / (2.0 - a)
function code(a, b) return Float64(1.0 / Float64(2.0 - a)) end
function tmp = code(a, b) tmp = 1.0 / (2.0 - a); end
code[a_, b_] := N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 - a}
\end{array}
Initial program 98.8%
add-cbrt-cube98.3%
pow1/398.8%
pow398.8%
clear-num98.8%
inv-pow98.8%
pow-pow98.8%
metadata-eval98.8%
Applied egg-rr98.8%
unpow1/398.3%
+-commutative98.3%
Simplified98.3%
Taylor expanded in b around inf 98.8%
+-commutative98.8%
rem-exp-log98.8%
div-exp98.8%
sub-neg98.8%
+-commutative98.8%
neg-sub098.8%
associate-+l-98.8%
metadata-eval98.8%
rem-log-exp98.8%
log-div98.8%
log-div98.8%
unpow-198.8%
rem-exp-log98.8%
unpow-198.8%
rem-exp-log98.8%
exp-diff98.8%
Simplified100.0%
Taylor expanded in b around 0 64.9%
Taylor expanded in a around 0 40.0%
mul-1-neg40.0%
unsub-neg40.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.8%
Taylor expanded in a around 0 79.9%
Taylor expanded in b around 0 39.3%
Final simplification39.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 2024023
(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))))