
(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 (if (<= a -32500000.0) (/ (exp a) (+ 1.0 1.0)) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -32500000.0) {
tmp = exp(a) / (1.0 + 1.0);
} 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 <= (-32500000.0d0)) then
tmp = exp(a) / (1.0d0 + 1.0d0)
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 <= -32500000.0) {
tmp = Math.exp(a) / (1.0 + 1.0);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -32500000.0: tmp = math.exp(a) / (1.0 + 1.0) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -32500000.0) tmp = Float64(exp(a) / Float64(1.0 + 1.0)); 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 <= -32500000.0) tmp = exp(a) / (1.0 + 1.0); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -32500000.0], N[(N[Exp[a], $MachinePrecision] / N[(1.0 + 1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -32500000:\\
\;\;\;\;\frac{e^{a}}{1 + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -3.25e7Initial program 100.0%
Taylor expanded in b around 0
Applied rewrites100.0%
Taylor expanded in a around 0
Applied rewrites100.0%
if -3.25e7 < a Initial program 98.9%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6499.6
Applied rewrites99.6%
Final simplification99.7%
(FPCore (a b) :precision binary64 (if (<= (/ (exp a) (+ (exp b) (exp a))) 0.6) (/ 1.0 (fma (fma (fma 0.16666666666666666 b 0.5) b 1.0) b 2.0)) 0.5))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(b) + exp(a))) <= 0.6) {
tmp = 1.0 / fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 2.0);
} else {
tmp = 0.5;
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(b) + exp(a))) <= 0.6) tmp = Float64(1.0 / fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 2.0)); else tmp = 0.5; end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[b], $MachinePrecision] + N[Exp[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.6], N[(1.0 / N[(N[(N[(0.16666666666666666 * b + 0.5), $MachinePrecision] * b + 1.0), $MachinePrecision] * b + 2.0), $MachinePrecision]), $MachinePrecision], 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{b} + e^{a}} \leq 0.6:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, b, 0.5\right), b, 1\right), b, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.599999999999999978Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6478.6
Applied rewrites78.6%
Taylor expanded in b around 0
Applied rewrites64.1%
if 0.599999999999999978 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 95.5%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Taylor expanded in b around 0
Applied rewrites18.8%
Final simplification56.1%
(FPCore (a b) :precision binary64 (if (<= (/ (exp a) (+ (exp b) (exp a))) 0.6) (/ 1.0 (fma (fma 0.5 b 1.0) b 2.0)) 0.5))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(b) + exp(a))) <= 0.6) {
tmp = 1.0 / fma(fma(0.5, b, 1.0), b, 2.0);
} else {
tmp = 0.5;
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(b) + exp(a))) <= 0.6) tmp = Float64(1.0 / fma(fma(0.5, b, 1.0), b, 2.0)); else tmp = 0.5; end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[b], $MachinePrecision] + N[Exp[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.6], N[(1.0 / N[(N[(0.5 * b + 1.0), $MachinePrecision] * b + 2.0), $MachinePrecision]), $MachinePrecision], 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{b} + e^{a}} \leq 0.6:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, b, 1\right), b, 2\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.599999999999999978Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6478.6
Applied rewrites78.6%
Taylor expanded in b around 0
Applied rewrites59.8%
if 0.599999999999999978 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 95.5%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Taylor expanded in b around 0
Applied rewrites18.8%
Final simplification52.6%
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp b) (exp a))))
double code(double a, double b) {
return exp(a) / (exp(b) + exp(a));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = exp(a) / (exp(b) + exp(a))
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(b) + Math.exp(a));
}
def code(a, b): return math.exp(a) / (math.exp(b) + math.exp(a))
function code(a, b) return Float64(exp(a) / Float64(exp(b) + exp(a))) end
function tmp = code(a, b) tmp = exp(a) / (exp(b) + exp(a)); end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[b], $MachinePrecision] + N[Exp[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{a}}{e^{b} + e^{a}}
\end{array}
Initial program 99.2%
Final simplification99.2%
(FPCore (a b) :precision binary64 (if (<= (exp b) 2.0) 0.5 (/ 1.0 (* (fma 0.5 b 1.0) b))))
double code(double a, double b) {
double tmp;
if (exp(b) <= 2.0) {
tmp = 0.5;
} else {
tmp = 1.0 / (fma(0.5, b, 1.0) * b);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (exp(b) <= 2.0) tmp = 0.5; else tmp = Float64(1.0 / Float64(fma(0.5, b, 1.0) * b)); end return tmp end
code[a_, b_] := If[LessEqual[N[Exp[b], $MachinePrecision], 2.0], 0.5, N[(1.0 / N[(N[(0.5 * b + 1.0), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(0.5, b, 1\right) \cdot b}\\
\end{array}
\end{array}
if (exp.f64 b) < 2Initial program 98.8%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6473.6
Applied rewrites73.6%
Taylor expanded in b around 0
Applied rewrites50.5%
if 2 < (exp.f64 b) Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6498.9
Applied rewrites98.9%
Taylor expanded in b around 0
Applied rewrites54.7%
Taylor expanded in b around inf
Applied rewrites54.7%
(FPCore (a b) :precision binary64 (if (<= (exp b) 2.0) 0.5 (/ 1.0 (* (* 0.5 b) b))))
double code(double a, double b) {
double tmp;
if (exp(b) <= 2.0) {
tmp = 0.5;
} else {
tmp = 1.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 (exp(b) <= 2.0d0) then
tmp = 0.5d0
else
tmp = 1.0d0 / ((0.5d0 * b) * b)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(b) <= 2.0) {
tmp = 0.5;
} else {
tmp = 1.0 / ((0.5 * b) * b);
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(b) <= 2.0: tmp = 0.5 else: tmp = 1.0 / ((0.5 * b) * b) return tmp
function code(a, b) tmp = 0.0 if (exp(b) <= 2.0) tmp = 0.5; else tmp = Float64(1.0 / Float64(Float64(0.5 * b) * b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(b) <= 2.0) tmp = 0.5; else tmp = 1.0 / ((0.5 * b) * b); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[b], $MachinePrecision], 2.0], 0.5, N[(1.0 / N[(N[(0.5 * b), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(0.5 \cdot b\right) \cdot b}\\
\end{array}
\end{array}
if (exp.f64 b) < 2Initial program 98.8%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6473.6
Applied rewrites73.6%
Taylor expanded in b around 0
Applied rewrites50.5%
if 2 < (exp.f64 b) Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6498.9
Applied rewrites98.9%
Taylor expanded in b around 0
Applied rewrites54.7%
Taylor expanded in b around inf
Applied rewrites54.7%
(FPCore (a b) :precision binary64 (if (<= a -8.6e+34) (/ 1.0 (* (fma (+ (/ 2.0 (* b b)) 0.5) b 1.0) b)) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -8.6e+34) {
tmp = 1.0 / (fma(((2.0 / (b * b)) + 0.5), b, 1.0) * b);
} else {
tmp = 1.0 / (1.0 + exp(b));
}
return tmp;
}
function code(a, b) tmp = 0.0 if (a <= -8.6e+34) tmp = Float64(1.0 / Float64(fma(Float64(Float64(2.0 / Float64(b * b)) + 0.5), b, 1.0) * b)); else tmp = Float64(1.0 / Float64(1.0 + exp(b))); end return tmp end
code[a_, b_] := If[LessEqual[a, -8.6e+34], N[(1.0 / N[(N[(N[(N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] * b + 1.0), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -8.6 \cdot 10^{+34}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{2}{b \cdot b} + 0.5, b, 1\right) \cdot b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -8.59999999999999988e34Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6434.9
Applied rewrites34.9%
Taylor expanded in b around 0
Applied rewrites21.4%
Taylor expanded in b around inf
Applied rewrites20.6%
Taylor expanded in b around inf
Applied rewrites56.3%
if -8.59999999999999988e34 < a Initial program 98.9%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6498.1
Applied rewrites98.1%
Final simplification87.7%
(FPCore (a b) :precision binary64 (if (<= a -5.6e+34) (/ 1.0 (* (fma (+ (/ 2.0 (* b b)) 0.5) b 1.0) b)) (/ 1.0 (fma (fma (fma 0.16666666666666666 b 0.5) b 1.0) b 2.0))))
double code(double a, double b) {
double tmp;
if (a <= -5.6e+34) {
tmp = 1.0 / (fma(((2.0 / (b * b)) + 0.5), b, 1.0) * b);
} else {
tmp = 1.0 / fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 2.0);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (a <= -5.6e+34) tmp = Float64(1.0 / Float64(fma(Float64(Float64(2.0 / Float64(b * b)) + 0.5), b, 1.0) * b)); else tmp = Float64(1.0 / fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 2.0)); end return tmp end
code[a_, b_] := If[LessEqual[a, -5.6e+34], N[(1.0 / N[(N[(N[(N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] * b + 1.0), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(N[(0.16666666666666666 * b + 0.5), $MachinePrecision] * b + 1.0), $MachinePrecision] * b + 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -5.6 \cdot 10^{+34}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{2}{b \cdot b} + 0.5, b, 1\right) \cdot b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, b, 0.5\right), b, 1\right), b, 2\right)}\\
\end{array}
\end{array}
if a < -5.60000000000000016e34Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6434.9
Applied rewrites34.9%
Taylor expanded in b around 0
Applied rewrites21.4%
Taylor expanded in b around inf
Applied rewrites20.6%
Taylor expanded in b around inf
Applied rewrites56.3%
if -5.60000000000000016e34 < a Initial program 98.9%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6498.1
Applied rewrites98.1%
Taylor expanded in b around 0
Applied rewrites63.9%
Final simplification62.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 99.2%
Taylor expanded in a around 0
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f6482.3
Applied rewrites82.3%
Taylor expanded in b around 0
Applied rewrites34.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 2024270
(FPCore (a b)
:name "Quotient of sum of exps"
:precision binary64
:alt
(! :herbie-platform default (/ 1 (+ 1 (exp (- b a)))))
(/ (exp a) (+ (exp a) (exp b))))