
(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 (exp (- a (log (+ (exp a) (exp b))))))
double code(double a, double b) {
return exp((a - log((exp(a) + exp(b)))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp((a - log((exp(a) + exp(b)))))
end function
public static double code(double a, double b) {
return Math.exp((a - Math.log((Math.exp(a) + Math.exp(b)))));
}
def code(a, b): return math.exp((a - math.log((math.exp(a) + math.exp(b)))))
function code(a, b) return exp(Float64(a - log(Float64(exp(a) + exp(b))))) end
function tmp = code(a, b) tmp = exp((a - log((exp(a) + exp(b))))); end
code[a_, b_] := N[Exp[N[(a - N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
e^{a - \log \left(e^{a} + e^{b}\right)}
\end{array}
Initial program 99.2%
add-exp-log99.2%
div-exp99.2%
Applied egg-rr99.2%
Final simplification99.2%
(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.2%
Final simplification99.2%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.99999995) (/ (exp a) b) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.99999995) {
tmp = exp(a) / b;
} 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.99999995d0) then
tmp = exp(a) / b
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.99999995) {
tmp = Math.exp(a) / b;
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.99999995: tmp = math.exp(a) / b else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.99999995) tmp = Float64(exp(a) / b); 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.99999995) tmp = exp(a) / b; else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.99999995], N[(N[Exp[a], $MachinePrecision] / b), $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.99999995:\\
\;\;\;\;\frac{e^{a}}{b}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if (exp.f64 a) < 0.999999949999999971Initial program 98.6%
Taylor expanded in a around 0 98.6%
associate-+r+98.6%
Simplified98.6%
Taylor expanded in b around 0 95.9%
+-commutative95.9%
+-commutative95.9%
associate-+l+95.9%
Simplified95.9%
Taylor expanded in b around inf 95.9%
if 0.999999949999999971 < (exp.f64 a) Initial program 99.4%
Taylor expanded in b around 0 49.0%
Taylor expanded in a around 0 48.4%
*-commutative48.4%
Simplified48.4%
Final simplification61.3%
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp b) (+ a 1.0))))
double code(double a, double b) {
return exp(a) / (exp(b) + (a + 1.0));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / (exp(b) + (a + 1.0d0))
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(b) + (a + 1.0));
}
def code(a, b): return math.exp(a) / (math.exp(b) + (a + 1.0))
function code(a, b) return Float64(exp(a) / Float64(exp(b) + Float64(a + 1.0))) end
function tmp = code(a, b) tmp = exp(a) / (exp(b) + (a + 1.0)); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[b], $MachinePrecision] + N[(a + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{e^{b} + \left(a + 1\right)}
\end{array}
Initial program 99.2%
Taylor expanded in a around 0 98.4%
associate-+r+98.4%
Simplified98.4%
Final simplification98.4%
(FPCore (a b) :precision binary64 (if (<= (exp a) 5e-13) (exp a) (+ 0.5 (* a 0.25))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 5e-13) {
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) <= 5d-13) 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) <= 5e-13) {
tmp = Math.exp(a);
} else {
tmp = 0.5 + (a * 0.25);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 5e-13: tmp = math.exp(a) else: tmp = 0.5 + (a * 0.25) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 5e-13) 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) <= 5e-13) tmp = exp(a); else tmp = 0.5 + (a * 0.25); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 5e-13], N[Exp[a], $MachinePrecision], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 5 \cdot 10^{-13}:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\end{array}
\end{array}
if (exp.f64 a) < 4.9999999999999999e-13Initial program 98.6%
add-exp-log98.6%
div-exp98.6%
Applied egg-rr98.6%
Taylor expanded in a around inf 97.2%
if 4.9999999999999999e-13 < (exp.f64 a) Initial program 99.4%
Taylor expanded in b around 0 48.8%
Taylor expanded in a around 0 48.1%
*-commutative48.1%
Simplified48.1%
Final simplification61.3%
(FPCore (a b)
:precision binary64
(if (<= b -0.155)
(exp a)
(if (<= b 4.3e-253)
(+ 0.5 (* b -0.25))
(if (<= b 4.8e-233)
(exp a)
(if (<= b 7.5e-32)
(+ 0.5 (* a 0.25))
(if (<= b 3.15e+63)
(exp a)
(if (<= b 1.15e+276)
(* -0.020833333333333332 (pow a 3.0))
(/ (exp a) b))))))))
double code(double a, double b) {
double tmp;
if (b <= -0.155) {
tmp = exp(a);
} else if (b <= 4.3e-253) {
tmp = 0.5 + (b * -0.25);
} else if (b <= 4.8e-233) {
tmp = exp(a);
} else if (b <= 7.5e-32) {
tmp = 0.5 + (a * 0.25);
} else if (b <= 3.15e+63) {
tmp = exp(a);
} else if (b <= 1.15e+276) {
tmp = -0.020833333333333332 * pow(a, 3.0);
} else {
tmp = exp(a) / 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.155d0)) then
tmp = exp(a)
else if (b <= 4.3d-253) then
tmp = 0.5d0 + (b * (-0.25d0))
else if (b <= 4.8d-233) then
tmp = exp(a)
else if (b <= 7.5d-32) then
tmp = 0.5d0 + (a * 0.25d0)
else if (b <= 3.15d+63) then
tmp = exp(a)
else if (b <= 1.15d+276) then
tmp = (-0.020833333333333332d0) * (a ** 3.0d0)
else
tmp = exp(a) / b
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -0.155) {
tmp = Math.exp(a);
} else if (b <= 4.3e-253) {
tmp = 0.5 + (b * -0.25);
} else if (b <= 4.8e-233) {
tmp = Math.exp(a);
} else if (b <= 7.5e-32) {
tmp = 0.5 + (a * 0.25);
} else if (b <= 3.15e+63) {
tmp = Math.exp(a);
} else if (b <= 1.15e+276) {
tmp = -0.020833333333333332 * Math.pow(a, 3.0);
} else {
tmp = Math.exp(a) / b;
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -0.155: tmp = math.exp(a) elif b <= 4.3e-253: tmp = 0.5 + (b * -0.25) elif b <= 4.8e-233: tmp = math.exp(a) elif b <= 7.5e-32: tmp = 0.5 + (a * 0.25) elif b <= 3.15e+63: tmp = math.exp(a) elif b <= 1.15e+276: tmp = -0.020833333333333332 * math.pow(a, 3.0) else: tmp = math.exp(a) / b return tmp
function code(a, b) tmp = 0.0 if (b <= -0.155) tmp = exp(a); elseif (b <= 4.3e-253) tmp = Float64(0.5 + Float64(b * -0.25)); elseif (b <= 4.8e-233) tmp = exp(a); elseif (b <= 7.5e-32) tmp = Float64(0.5 + Float64(a * 0.25)); elseif (b <= 3.15e+63) tmp = exp(a); elseif (b <= 1.15e+276) tmp = Float64(-0.020833333333333332 * (a ^ 3.0)); else tmp = Float64(exp(a) / b); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -0.155) tmp = exp(a); elseif (b <= 4.3e-253) tmp = 0.5 + (b * -0.25); elseif (b <= 4.8e-233) tmp = exp(a); elseif (b <= 7.5e-32) tmp = 0.5 + (a * 0.25); elseif (b <= 3.15e+63) tmp = exp(a); elseif (b <= 1.15e+276) tmp = -0.020833333333333332 * (a ^ 3.0); else tmp = exp(a) / b; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -0.155], N[Exp[a], $MachinePrecision], If[LessEqual[b, 4.3e-253], N[(0.5 + N[(b * -0.25), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 4.8e-233], N[Exp[a], $MachinePrecision], If[LessEqual[b, 7.5e-32], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 3.15e+63], N[Exp[a], $MachinePrecision], If[LessEqual[b, 1.15e+276], N[(-0.020833333333333332 * N[Power[a, 3.0], $MachinePrecision]), $MachinePrecision], N[(N[Exp[a], $MachinePrecision] / b), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.155:\\
\;\;\;\;e^{a}\\
\mathbf{elif}\;b \leq 4.3 \cdot 10^{-253}:\\
\;\;\;\;0.5 + b \cdot -0.25\\
\mathbf{elif}\;b \leq 4.8 \cdot 10^{-233}:\\
\;\;\;\;e^{a}\\
\mathbf{elif}\;b \leq 7.5 \cdot 10^{-32}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{elif}\;b \leq 3.15 \cdot 10^{+63}:\\
\;\;\;\;e^{a}\\
\mathbf{elif}\;b \leq 1.15 \cdot 10^{+276}:\\
\;\;\;\;-0.020833333333333332 \cdot {a}^{3}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{a}}{b}\\
\end{array}
\end{array}
if b < -0.154999999999999999 or 4.3000000000000002e-253 < b < 4.7999999999999998e-233 or 7.49999999999999953e-32 < b < 3.1499999999999999e63Initial program 98.5%
add-exp-log98.5%
div-exp98.5%
Applied egg-rr98.5%
Taylor expanded in a around inf 83.9%
if -0.154999999999999999 < b < 4.3000000000000002e-253Initial program 98.4%
Taylor expanded in a around 0 70.8%
Taylor expanded in b around 0 69.7%
*-commutative69.7%
Simplified69.7%
if 4.7999999999999998e-233 < b < 7.49999999999999953e-32Initial program 100.0%
Taylor expanded in b around 0 100.0%
Taylor expanded in a around 0 70.6%
*-commutative70.6%
Simplified70.6%
if 3.1499999999999999e63 < b < 1.15000000000000002e276Initial program 100.0%
Taylor expanded in b around 0 21.9%
Taylor expanded in a around 0 2.9%
Taylor expanded in a around inf 52.7%
if 1.15000000000000002e276 < b Initial program 100.0%
Taylor expanded in a around 0 100.0%
associate-+r+100.0%
Simplified100.0%
Taylor expanded in b around 0 67.3%
+-commutative67.3%
+-commutative67.3%
associate-+l+67.3%
Simplified67.3%
Taylor expanded in b around inf 67.3%
Final simplification69.6%
(FPCore (a b) :precision binary64 (if (<= a -1600000.0) (exp a) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (a <= -1600000.0) {
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 (a <= (-1600000.0d0)) 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 (a <= -1600000.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -1600000.0: tmp = math.exp(a) else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (a <= -1600000.0) 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 (a <= -1600000.0) tmp = exp(a); else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -1600000.0], N[Exp[a], $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1600000:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if a < -1.6e6Initial program 100.0%
add-exp-log100.0%
div-exp100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
if -1.6e6 < a Initial program 98.9%
Taylor expanded in a around 0 98.3%
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 99.2%
Taylor expanded in b around 0 61.8%
Taylor expanded in a around 0 35.8%
*-commutative35.8%
Simplified35.8%
Final simplification35.8%
(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.2%
Taylor expanded in a around 0 82.9%
Taylor expanded in b around 0 35.6%
Final simplification35.6%
(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 2024046
(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))))