
(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 9 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 (let* ((t_0 (+ 1.0 (exp b))) (t_1 (/ 1.0 t_0))) (if (<= a -8e+21) (exp a) (+ t_1 (* a (- t_1 (/ 1.0 (pow t_0 2.0))))))))
double code(double a, double b) {
double t_0 = 1.0 + exp(b);
double t_1 = 1.0 / t_0;
double tmp;
if (a <= -8e+21) {
tmp = exp(a);
} else {
tmp = t_1 + (a * (t_1 - (1.0 / pow(t_0, 2.0))));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = 1.0d0 + exp(b)
t_1 = 1.0d0 / t_0
if (a <= (-8d+21)) then
tmp = exp(a)
else
tmp = t_1 + (a * (t_1 - (1.0d0 / (t_0 ** 2.0d0))))
end if
code = tmp
end function
public static double code(double a, double b) {
double t_0 = 1.0 + Math.exp(b);
double t_1 = 1.0 / t_0;
double tmp;
if (a <= -8e+21) {
tmp = Math.exp(a);
} else {
tmp = t_1 + (a * (t_1 - (1.0 / Math.pow(t_0, 2.0))));
}
return tmp;
}
def code(a, b): t_0 = 1.0 + math.exp(b) t_1 = 1.0 / t_0 tmp = 0 if a <= -8e+21: tmp = math.exp(a) else: tmp = t_1 + (a * (t_1 - (1.0 / math.pow(t_0, 2.0)))) return tmp
function code(a, b) t_0 = Float64(1.0 + exp(b)) t_1 = Float64(1.0 / t_0) tmp = 0.0 if (a <= -8e+21) tmp = exp(a); else tmp = Float64(t_1 + Float64(a * Float64(t_1 - Float64(1.0 / (t_0 ^ 2.0))))); end return tmp end
function tmp_2 = code(a, b) t_0 = 1.0 + exp(b); t_1 = 1.0 / t_0; tmp = 0.0; if (a <= -8e+21) tmp = exp(a); else tmp = t_1 + (a * (t_1 - (1.0 / (t_0 ^ 2.0)))); end tmp_2 = tmp; end
code[a_, b_] := Block[{t$95$0 = N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 / t$95$0), $MachinePrecision]}, If[LessEqual[a, -8e+21], N[Exp[a], $MachinePrecision], N[(t$95$1 + N[(a * N[(t$95$1 - N[(1.0 / N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + e^{b}\\
t_1 := \frac{1}{t_0}\\
\mathbf{if}\;a \leq -8 \cdot 10^{+21}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;t_1 + a \cdot \left(t_1 - \frac{1}{{t_0}^{2}}\right)\\
\end{array}
\end{array}
if a < -8e21Initial 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 100.0%
if -8e21 < a Initial program 98.4%
Taylor expanded in a around 0 100.0%
Final simplification100.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.4%
Final simplification98.4%
(FPCore (a b) :precision binary64 (if (<= a -8e+21) (exp a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -8e+21) {
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 <= (-8d+21)) 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 <= -8e+21) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -8e+21: tmp = math.exp(a) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -8e+21) 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 <= -8e+21) tmp = exp(a); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -8e+21], 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 -8 \cdot 10^{+21}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -8e21Initial 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 100.0%
if -8e21 < a Initial program 98.4%
Taylor expanded in a around 0 99.4%
Final simplification99.5%
(FPCore (a b) :precision binary64 (if (<= a -2.1e-8) (exp a) (/ 1.0 (+ 2.0 (+ b (* 0.5 (* b b)))))))
double code(double a, double b) {
double tmp;
if (a <= -2.1e-8) {
tmp = exp(a);
} else {
tmp = 1.0 / (2.0 + (b + (0.5 * (b * b))));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-2.1d-8)) then
tmp = exp(a)
else
tmp = 1.0d0 / (2.0d0 + (b + (0.5d0 * (b * b))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -2.1e-8) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (2.0 + (b + (0.5 * (b * b))));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -2.1e-8: tmp = math.exp(a) else: tmp = 1.0 / (2.0 + (b + (0.5 * (b * b)))) return tmp
function code(a, b) tmp = 0.0 if (a <= -2.1e-8) tmp = exp(a); else tmp = Float64(1.0 / Float64(2.0 + Float64(b + Float64(0.5 * Float64(b * b))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -2.1e-8) tmp = exp(a); else tmp = 1.0 / (2.0 + (b + (0.5 * (b * b)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -2.1e-8], N[Exp[a], $MachinePrecision], N[(1.0 / N[(2.0 + N[(b + N[(0.5 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2.1 \cdot 10^{-8}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + \left(b + 0.5 \cdot \left(b \cdot b\right)\right)}\\
\end{array}
\end{array}
if a < -2.09999999999999994e-8Initial program 95.9%
add-cbrt-cube95.9%
pow1/395.9%
pow-to-exp95.9%
pow395.9%
log-pow95.9%
log-div95.9%
add-log-exp96.0%
Applied egg-rr96.0%
Taylor expanded in a around inf 95.6%
if -2.09999999999999994e-8 < a Initial program 99.4%
Taylor expanded in a around 0 99.4%
Taylor expanded in b around 0 62.2%
unpow262.2%
Simplified62.2%
Final simplification71.7%
(FPCore (a b) :precision binary64 (if (<= b -0.16) 0.5 (/ 1.0 (+ 2.0 (+ b (* 0.5 (* b b)))))))
double code(double a, double b) {
double tmp;
if (b <= -0.16) {
tmp = 0.5;
} else {
tmp = 1.0 / (2.0 + (b + (0.5 * (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 <= (-0.16d0)) then
tmp = 0.5d0
else
tmp = 1.0d0 / (2.0d0 + (b + (0.5d0 * (b * b))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -0.16) {
tmp = 0.5;
} else {
tmp = 1.0 / (2.0 + (b + (0.5 * (b * b))));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.16: tmp = 0.5 else: tmp = 1.0 / (2.0 + (b + (0.5 * (b * b)))) return tmp
function code(a, b) tmp = 0.0 if (b <= -0.16) tmp = 0.5; else tmp = Float64(1.0 / Float64(2.0 + Float64(b + Float64(0.5 * Float64(b * b))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.16) tmp = 0.5; else tmp = 1.0 / (2.0 + (b + (0.5 * (b * b)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.16], 0.5, N[(1.0 / N[(2.0 + N[(b + N[(0.5 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.16:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + \left(b + 0.5 \cdot \left(b \cdot b\right)\right)}\\
\end{array}
\end{array}
if b < -0.160000000000000003Initial program 91.7%
Taylor expanded in a around 0 96.0%
Taylor expanded in b around 0 18.1%
if -0.160000000000000003 < b Initial program 100.0%
Taylor expanded in a around 0 77.6%
Taylor expanded in b around 0 60.1%
unpow260.1%
Simplified60.1%
Final simplification52.2%
(FPCore (a b) :precision binary64 (if (<= b 5.2e-45) (+ 0.5 (* a 0.25)) (/ 1.0 (+ 2.0 (* 0.5 (* b b))))))
double code(double a, double b) {
double tmp;
if (b <= 5.2e-45) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (0.5 * (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 <= 5.2d-45) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 1.0d0 / (2.0d0 + (0.5d0 * (b * b)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 5.2e-45) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (0.5 * (b * b)));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 5.2e-45: tmp = 0.5 + (a * 0.25) else: tmp = 1.0 / (2.0 + (0.5 * (b * b))) return tmp
function code(a, b) tmp = 0.0 if (b <= 5.2e-45) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(1.0 / Float64(2.0 + Float64(0.5 * Float64(b * b)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 5.2e-45) tmp = 0.5 + (a * 0.25); else tmp = 1.0 / (2.0 + (0.5 * (b * b))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 5.2e-45], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(2.0 + N[(0.5 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 5.2 \cdot 10^{-45}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + 0.5 \cdot \left(b \cdot b\right)}\\
\end{array}
\end{array}
if b < 5.19999999999999973e-45Initial program 97.7%
Taylor expanded in b around 0 77.7%
Taylor expanded in a around 0 51.0%
*-commutative51.0%
Simplified51.0%
if 5.19999999999999973e-45 < b Initial program 99.9%
Taylor expanded in a around 0 97.6%
Taylor expanded in b around 0 54.3%
unpow254.3%
Simplified54.3%
Taylor expanded in b around inf 52.9%
unpow252.9%
Simplified52.9%
Final simplification51.6%
(FPCore (a b) :precision binary64 (if (<= b 1.6) (+ 0.5 (* a 0.25)) (/ 2.0 (* b b))))
double code(double a, double b) {
double tmp;
if (b <= 1.6) {
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 <= 1.6d0) 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 <= 1.6) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 2.0 / (b * b);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 1.6: tmp = 0.5 + (a * 0.25) else: tmp = 2.0 / (b * b) return tmp
function code(a, b) tmp = 0.0 if (b <= 1.6) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(2.0 / Float64(b * b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 1.6) tmp = 0.5 + (a * 0.25); else tmp = 2.0 / (b * b); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 1.6], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.6:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{b \cdot b}\\
\end{array}
\end{array}
if b < 1.6000000000000001Initial program 97.8%
Taylor expanded in b around 0 77.9%
Taylor expanded in a around 0 51.8%
*-commutative51.8%
Simplified51.8%
if 1.6000000000000001 < b Initial program 100.0%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 51.2%
unpow251.2%
Simplified51.2%
Taylor expanded in b around inf 51.2%
unpow251.2%
Simplified51.2%
Final simplification51.6%
(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.4%
Taylor expanded in b around 0 64.2%
Taylor expanded in a around 0 37.9%
*-commutative37.9%
Simplified37.9%
Final simplification37.9%
(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.4%
Taylor expanded in a around 0 81.0%
Taylor expanded in b around 0 37.9%
Final simplification37.9%
(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 2023218
(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))))