
(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 (/ (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) 0.999999999995) (/ (exp a) (+ (exp a) 1.0)) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.999999999995) {
tmp = exp(a) / (exp(a) + 1.0);
} 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) <= 0.999999999995d0) then
tmp = exp(a) / (exp(a) + 1.0d0)
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) <= 0.999999999995) {
tmp = Math.exp(a) / (Math.exp(a) + 1.0);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.999999999995: tmp = math.exp(a) / (math.exp(a) + 1.0) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.999999999995) tmp = Float64(exp(a) / Float64(exp(a) + 1.0)); 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) <= 0.999999999995) tmp = exp(a) / (exp(a) + 1.0); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.999999999995], N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.999999999995:\\
\;\;\;\;\frac{e^{a}}{e^{a} + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.999999999995Initial program 98.6%
Taylor expanded in b around 0 97.4%
if 0.999999999995 < (exp.f64 a) Initial program 100.0%
Taylor expanded in a around 0 98.9%
Final simplification98.5%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.999999999995) (* (exp a) (+ 0.5 (* a -0.25))) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.999999999995) {
tmp = exp(a) * (0.5 + (a * -0.25));
} 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) <= 0.999999999995d0) then
tmp = exp(a) * (0.5d0 + (a * (-0.25d0)))
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) <= 0.999999999995) {
tmp = Math.exp(a) * (0.5 + (a * -0.25));
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.999999999995: tmp = math.exp(a) * (0.5 + (a * -0.25)) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.999999999995) tmp = Float64(exp(a) * Float64(0.5 + Float64(a * -0.25))); 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) <= 0.999999999995) tmp = exp(a) * (0.5 + (a * -0.25)); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.999999999995], N[(N[Exp[a], $MachinePrecision] * N[(0.5 + N[(a * -0.25), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.999999999995:\\
\;\;\;\;e^{a} \cdot \left(0.5 + a \cdot -0.25\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.999999999995Initial program 98.6%
clear-num98.6%
associate-/r/98.6%
Applied egg-rr98.6%
Taylor expanded in b around 0 97.4%
Taylor expanded in a around 0 94.3%
*-commutative94.3%
Simplified94.3%
if 0.999999999995 < (exp.f64 a) Initial program 100.0%
Taylor expanded in a around 0 98.9%
Final simplification97.5%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.1) (/ (exp a) 2.0) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.1) {
tmp = exp(a) / 2.0;
} 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) <= 0.1d0) then
tmp = exp(a) / 2.0d0
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) <= 0.1) {
tmp = Math.exp(a) / 2.0;
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.1: tmp = math.exp(a) / 2.0 else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.1) tmp = Float64(exp(a) / 2.0); 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) <= 0.1) tmp = exp(a) / 2.0; else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.1], N[(N[Exp[a], $MachinePrecision] / 2.0), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.1:\\
\;\;\;\;\frac{e^{a}}{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.10000000000000001Initial program 98.6%
Taylor expanded in b around 0 98.6%
Taylor expanded in a around 0 96.2%
if 0.10000000000000001 < (exp.f64 a) Initial program 100.0%
Taylor expanded in a around 0 97.5%
Final simplification97.1%
(FPCore (a b) :precision binary64 (if (<= b 1.7e+19) (/ (exp a) 2.0) (* -0.020833333333333332 (pow a 3.0))))
double code(double a, double b) {
double tmp;
if (b <= 1.7e+19) {
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.7d+19) 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.7e+19) {
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.7e+19: 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.7e+19) 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.7e+19) tmp = exp(a) / 2.0; else tmp = -0.020833333333333332 * (a ^ 3.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 1.7e+19], 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.7 \cdot 10^{+19}:\\
\;\;\;\;\frac{e^{a}}{2}\\
\mathbf{else}:\\
\;\;\;\;-0.020833333333333332 \cdot {a}^{3}\\
\end{array}
\end{array}
if b < 1.7e19Initial program 99.4%
Taylor expanded in b around 0 79.3%
Taylor expanded in a around 0 76.1%
if 1.7e19 < b Initial program 100.0%
Taylor expanded in b around 0 34.4%
Taylor expanded in a around 0 2.7%
Taylor expanded in a around inf 45.0%
Final simplification68.6%
(FPCore (a b) :precision binary64 (/ (exp a) 2.0))
double code(double a, double b) {
return exp(a) / 2.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / 2.0d0
end function
public static double code(double a, double b) {
return Math.exp(a) / 2.0;
}
def code(a, b): return math.exp(a) / 2.0
function code(a, b) return Float64(exp(a) / 2.0) end
function tmp = code(a, b) tmp = exp(a) / 2.0; end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{2}
\end{array}
Initial program 99.6%
Taylor expanded in b around 0 68.5%
Taylor expanded in a around 0 66.0%
Final simplification66.0%
(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 68.5%
Taylor expanded in a around 0 42.1%
*-commutative42.1%
Simplified42.1%
Final simplification42.1%
(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 a around 0 80.8%
Taylor expanded in b around 0 41.0%
Final simplification41.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 2023250
(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))))