
(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 15 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 99.2%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
clear-numN/A
lower-/.f64N/A
div-invN/A
lower-*.f64N/A
lift-exp.f64N/A
rec-expN/A
lower-exp.f64N/A
lower-neg.f6499.2
Applied rewrites99.2%
Taylor expanded in a around inf
distribute-rgt-inN/A
exp-negN/A
rgt-mult-inverseN/A
lower-+.f64N/A
neg-mul-1N/A
prod-expN/A
lower-exp.f64N/A
neg-mul-1N/A
unsub-negN/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (a b)
:precision binary64
(if (<= (/ (exp a) (+ (exp a) (exp b))) 0.6437559377674459)
(/
1.0
(fma
b
(fma b (/ (fma (* b (* b b)) 0.004629629629629629 0.125) 0.25) 1.0)
2.0))
(+ 1.0 (exp b))))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.6437559377674459) {
tmp = 1.0 / fma(b, fma(b, (fma((b * (b * b)), 0.004629629629629629, 0.125) / 0.25), 1.0), 2.0);
} else {
tmp = 1.0 + exp(b);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.6437559377674459) tmp = Float64(1.0 / fma(b, fma(b, Float64(fma(Float64(b * Float64(b * b)), 0.004629629629629629, 0.125) / 0.25), 1.0), 2.0)); else tmp = Float64(1.0 + exp(b)); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.6437559377674459], N[(1.0 / N[(b * N[(b * N[(N[(N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision] * 0.004629629629629629 + 0.125), $MachinePrecision] / 0.25), $MachinePrecision] + 1.0), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0.6437559377674459:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(b, \mathsf{fma}\left(b, \frac{\mathsf{fma}\left(b \cdot \left(b \cdot b\right), 0.004629629629629629, 0.125\right)}{0.25}, 1\right), 2\right)}\\
\mathbf{else}:\\
\;\;\;\;1 + e^{b}\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.643755937767445885Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6481.4
Applied rewrites81.4%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6470.1
Applied rewrites70.1%
flip3-+N/A
lower-/.f64N/A
unpow-prod-downN/A
lower-fma.f64N/A
cube-multN/A
lower-*.f64N/A
lower-*.f64N/A
metadata-evalN/A
metadata-evalN/A
associate-+r-N/A
lower--.f64N/A
swap-sqrN/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-evalN/A
metadata-evalN/A
associate-*l*N/A
lower-*.f64N/A
metadata-eval51.2
Applied rewrites51.2%
Taylor expanded in b around 0
Applied rewrites75.5%
if 0.643755937767445885 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 96.3%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Applied rewrites100.0%
Final simplification80.7%
(FPCore (a b)
:precision binary64
(if (<= (/ (exp a) (+ (exp a) (exp b))) 0.495)
(/
1.0
(fma
b
(fma b (/ (fma (* b (* b b)) 0.004629629629629629 0.125) 0.25) 1.0)
2.0))
(fma
a
(fma
(* a a)
(fma (* a a) 0.0020833333333333333 -0.020833333333333332)
0.25)
0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.495) {
tmp = 1.0 / fma(b, fma(b, (fma((b * (b * b)), 0.004629629629629629, 0.125) / 0.25), 1.0), 2.0);
} else {
tmp = fma(a, fma((a * a), fma((a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.495) tmp = Float64(1.0 / fma(b, fma(b, Float64(fma(Float64(b * Float64(b * b)), 0.004629629629629629, 0.125) / 0.25), 1.0), 2.0)); else tmp = fma(a, fma(Float64(a * a), fma(Float64(a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.495], N[(1.0 / N[(b * N[(b * N[(N[(N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision] * 0.004629629629629629 + 0.125), $MachinePrecision] / 0.25), $MachinePrecision] + 1.0), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision], N[(a * N[(N[(a * a), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * 0.0020833333333333333 + -0.020833333333333332), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0.495:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(b, \mathsf{fma}\left(b, \frac{\mathsf{fma}\left(b \cdot \left(b \cdot b\right), 0.004629629629629629, 0.125\right)}{0.25}, 1\right), 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(a \cdot a, \mathsf{fma}\left(a \cdot a, 0.0020833333333333333, -0.020833333333333332\right), 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.495Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6467.8
Applied rewrites67.8%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6446.0
Applied rewrites46.0%
flip3-+N/A
lower-/.f64N/A
unpow-prod-downN/A
lower-fma.f64N/A
cube-multN/A
lower-*.f64N/A
lower-*.f64N/A
metadata-evalN/A
metadata-evalN/A
associate-+r-N/A
lower--.f64N/A
swap-sqrN/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-evalN/A
metadata-evalN/A
associate-*l*N/A
lower-*.f64N/A
metadata-eval9.8
Applied rewrites9.8%
Taylor expanded in b around 0
Applied rewrites56.5%
if 0.495 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.3%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6470.0
Applied rewrites70.0%
(FPCore (a b)
:precision binary64
(if (<= (/ (exp a) (+ (exp a) (exp b))) 0.495)
(/ 1.0 (fma b (fma b (fma b 0.16666666666666666 0.5) 1.0) 2.0))
(fma
a
(fma
(* a a)
(fma (* a a) 0.0020833333333333333 -0.020833333333333332)
0.25)
0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.495) {
tmp = 1.0 / fma(b, fma(b, fma(b, 0.16666666666666666, 0.5), 1.0), 2.0);
} else {
tmp = fma(a, fma((a * a), fma((a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.495) tmp = Float64(1.0 / fma(b, fma(b, fma(b, 0.16666666666666666, 0.5), 1.0), 2.0)); else tmp = fma(a, fma(Float64(a * a), fma(Float64(a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.495], N[(1.0 / N[(b * N[(b * N[(b * 0.16666666666666666 + 0.5), $MachinePrecision] + 1.0), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision], N[(a * N[(N[(a * a), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * 0.0020833333333333333 + -0.020833333333333332), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0.495:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(b, \mathsf{fma}\left(b, \mathsf{fma}\left(b, 0.16666666666666666, 0.5\right), 1\right), 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(a \cdot a, \mathsf{fma}\left(a \cdot a, 0.0020833333333333333, -0.020833333333333332\right), 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.495Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6467.8
Applied rewrites67.8%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6446.0
Applied rewrites46.0%
if 0.495 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.3%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6470.0
Applied rewrites70.0%
(FPCore (a b)
:precision binary64
(if (<= (/ (exp a) (+ (exp a) (exp b))) 0.0)
(/ 1.0 (fma b (* b (fma b 0.16666666666666666 0.5)) b))
(fma
a
(fma
(* a a)
(fma (* a a) 0.0020833333333333333 -0.020833333333333332)
0.25)
0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.0) {
tmp = 1.0 / fma(b, (b * fma(b, 0.16666666666666666, 0.5)), b);
} else {
tmp = fma(a, fma((a * a), fma((a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.0) tmp = Float64(1.0 / fma(b, Float64(b * fma(b, 0.16666666666666666, 0.5)), b)); else tmp = fma(a, fma(Float64(a * a), fma(Float64(a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(1.0 / N[(b * N[(b * N[(b * 0.16666666666666666 + 0.5), $MachinePrecision]), $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision], N[(a * N[(N[(a * a), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * 0.0020833333333333333 + -0.020833333333333332), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(b, b \cdot \mathsf{fma}\left(b, 0.16666666666666666, 0.5\right), b\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(a \cdot a, \mathsf{fma}\left(a \cdot a, 0.0020833333333333333, -0.020833333333333332\right), 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.0Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6468.0
Applied rewrites68.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6446.1
Applied rewrites46.1%
Taylor expanded in b around inf
cube-multN/A
unpow2N/A
associate-*l*N/A
associate-+r+N/A
distribute-lft-inN/A
unpow2N/A
associate-*l*N/A
+-commutativeN/A
distribute-rgt-inN/A
associate-*l*N/A
lft-mult-inverseN/A
metadata-evalN/A
rgt-mult-inverseN/A
distribute-lft-inN/A
Applied rewrites45.9%
if 0.0 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.2%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6469.4
Applied rewrites69.4%
(FPCore (a b) :precision binary64 (if (<= (/ (exp a) (+ (exp a) (exp b))) 0.0) (/ 1.0 (fma b (* b (fma b 0.16666666666666666 0.5)) b)) (fma a (fma -0.020833333333333332 (* a a) 0.25) 0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.0) {
tmp = 1.0 / fma(b, (b * fma(b, 0.16666666666666666, 0.5)), b);
} else {
tmp = fma(a, fma(-0.020833333333333332, (a * a), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.0) tmp = Float64(1.0 / fma(b, Float64(b * fma(b, 0.16666666666666666, 0.5)), b)); else tmp = fma(a, fma(-0.020833333333333332, Float64(a * a), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(1.0 / N[(b * N[(b * N[(b * 0.16666666666666666 + 0.5), $MachinePrecision]), $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision], N[(a * N[(-0.020833333333333332 * N[(a * a), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(b, b \cdot \mathsf{fma}\left(b, 0.16666666666666666, 0.5\right), b\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(-0.020833333333333332, a \cdot a, 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.0Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6468.0
Applied rewrites68.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6446.1
Applied rewrites46.1%
Taylor expanded in b around inf
cube-multN/A
unpow2N/A
associate-*l*N/A
associate-+r+N/A
distribute-lft-inN/A
unpow2N/A
associate-*l*N/A
+-commutativeN/A
distribute-rgt-inN/A
associate-*l*N/A
lft-mult-inverseN/A
metadata-evalN/A
rgt-mult-inverseN/A
distribute-lft-inN/A
Applied rewrites45.9%
if 0.0 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.2%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6469.4
Applied rewrites69.4%
(FPCore (a b) :precision binary64 (if (<= (/ (exp a) (+ (exp a) (exp b))) 0.0) (/ 1.0 (* b (* b (fma b 0.16666666666666666 0.5)))) (fma a (fma -0.020833333333333332 (* a a) 0.25) 0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.0) {
tmp = 1.0 / (b * (b * fma(b, 0.16666666666666666, 0.5)));
} else {
tmp = fma(a, fma(-0.020833333333333332, (a * a), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.0) tmp = Float64(1.0 / Float64(b * Float64(b * fma(b, 0.16666666666666666, 0.5)))); else tmp = fma(a, fma(-0.020833333333333332, Float64(a * a), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(1.0 / N[(b * N[(b * N[(b * 0.16666666666666666 + 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a * N[(-0.020833333333333332 * N[(a * a), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\
\;\;\;\;\frac{1}{b \cdot \left(b \cdot \mathsf{fma}\left(b, 0.16666666666666666, 0.5\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(-0.020833333333333332, a \cdot a, 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.0Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6468.0
Applied rewrites68.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6446.1
Applied rewrites46.1%
Taylor expanded in b around inf
unpow3N/A
unpow2N/A
associate-*l*N/A
unpow2N/A
+-commutativeN/A
distribute-rgt-inN/A
associate-*l*N/A
lft-mult-inverseN/A
metadata-evalN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6445.8
Applied rewrites45.8%
if 0.0 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.2%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6469.4
Applied rewrites69.4%
(FPCore (a b) :precision binary64 (if (<= (/ (exp a) (+ (exp a) (exp b))) 0.0) (/ 6.0 (* b (* b b))) (fma a (fma -0.020833333333333332 (* a a) 0.25) 0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.0) {
tmp = 6.0 / (b * (b * b));
} else {
tmp = fma(a, fma(-0.020833333333333332, (a * a), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.0) tmp = Float64(6.0 / Float64(b * Float64(b * b))); else tmp = fma(a, fma(-0.020833333333333332, Float64(a * a), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(6.0 / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a * N[(-0.020833333333333332 * N[(a * a), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\
\;\;\;\;\frac{6}{b \cdot \left(b \cdot b\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(-0.020833333333333332, a \cdot a, 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.0Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6468.0
Applied rewrites68.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6446.1
Applied rewrites46.1%
Taylor expanded in b around inf
lower-/.f64N/A
cube-multN/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6445.7
Applied rewrites45.7%
if 0.0 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.2%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6469.4
Applied rewrites69.4%
(FPCore (a b) :precision binary64 (if (<= (/ (exp a) (+ (exp a) (exp b))) 0.0) (/ 2.0 (* b b)) (fma a (fma -0.020833333333333332 (* a a) 0.25) 0.5)))
double code(double a, double b) {
double tmp;
if ((exp(a) / (exp(a) + exp(b))) <= 0.0) {
tmp = 2.0 / (b * b);
} else {
tmp = fma(a, fma(-0.020833333333333332, (a * a), 0.25), 0.5);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(exp(a) / Float64(exp(a) + exp(b))) <= 0.0) tmp = Float64(2.0 / Float64(b * b)); else tmp = fma(a, fma(-0.020833333333333332, Float64(a * a), 0.25), 0.5); end return tmp end
code[a_, b_] := If[LessEqual[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision], N[(a * N[(-0.020833333333333332 * N[(a * a), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{e^{a}}{e^{a} + e^{b}} \leq 0:\\
\;\;\;\;\frac{2}{b \cdot b}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(-0.020833333333333332, a \cdot a, 0.25\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) < 0.0Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6468.0
Applied rewrites68.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6439.4
Applied rewrites39.4%
Taylor expanded in b around inf
lower-/.f64N/A
unpow2N/A
lower-*.f6439.1
Applied rewrites39.1%
if 0.0 < (/.f64 (exp.f64 a) (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 98.6%
Taylor expanded in b around 0
Applied rewrites70.2%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6469.4
Applied rewrites69.4%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.9999) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.9999) {
tmp = 1.0 / (1.0 + 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 (exp(a) <= 0.9999d0) then
tmp = 1.0d0 / (1.0d0 + 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 (Math.exp(a) <= 0.9999) {
tmp = 1.0 / (1.0 + Math.exp(-a));
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.9999: tmp = 1.0 / (1.0 + math.exp(-a)) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.9999) tmp = Float64(1.0 / Float64(1.0 + exp(Float64(-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 (exp(a) <= 0.9999) tmp = 1.0 / (1.0 + exp(-a)); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.9999], N[(1.0 / N[(1.0 + N[Exp[(-a)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.9999:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.99990000000000001Initial program 98.3%
lift-exp.f64N/A
lift-exp.f64N/A
lift-exp.f64N/A
lift-+.f64N/A
clear-numN/A
lower-/.f64N/A
div-invN/A
lower-*.f64N/A
lift-exp.f64N/A
rec-expN/A
lower-exp.f64N/A
lower-neg.f6498.2
Applied rewrites98.2%
Taylor expanded in b around 0
+-commutativeN/A
distribute-lft-inN/A
exp-negN/A
lft-mult-inverseN/A
*-rgt-identityN/A
lower-+.f64N/A
neg-mul-1N/A
lower-exp.f64N/A
neg-mul-1N/A
lower-neg.f6496.5
Applied rewrites96.5%
if 0.99990000000000001 < (exp.f64 a) Initial program 99.5%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6499.0
Applied rewrites99.0%
(FPCore (a b) :precision binary64 (if (<= a -250000.0) (/ (exp a) b) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -250000.0) {
tmp = exp(a) / b;
} 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 <= (-250000.0d0)) then
tmp = exp(a) / b
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 <= -250000.0) {
tmp = Math.exp(a) / b;
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -250000.0: tmp = math.exp(a) / b else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -250000.0) tmp = Float64(exp(a) / b); 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 <= -250000.0) tmp = exp(a) / b; else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -250000.0], N[(N[Exp[a], $MachinePrecision] / b), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -250000:\\
\;\;\;\;\frac{e^{a}}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -2.5e5Initial program 100.0%
Taylor expanded in b around 0
lower-+.f64N/A
lower-+.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Taylor expanded in b around inf
lower-/.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
if -2.5e5 < a Initial program 99.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6497.7
Applied rewrites97.7%
(FPCore (a b)
:precision binary64
(if (<= b 7.5e-5)
(fma
a
(fma
(* a a)
(fma (* a a) 0.0020833333333333333 -0.020833333333333332)
0.25)
0.5)
(if (<= b 2e+154)
(* (/ 1.0 (fma (* b b) (* (* b b) 0.25) -4.0)) (fma b (* b 0.5) -2.0))
(/ 2.0 (* b b)))))
double code(double a, double b) {
double tmp;
if (b <= 7.5e-5) {
tmp = fma(a, fma((a * a), fma((a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5);
} else if (b <= 2e+154) {
tmp = (1.0 / fma((b * b), ((b * b) * 0.25), -4.0)) * fma(b, (b * 0.5), -2.0);
} else {
tmp = 2.0 / (b * b);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (b <= 7.5e-5) tmp = fma(a, fma(Float64(a * a), fma(Float64(a * a), 0.0020833333333333333, -0.020833333333333332), 0.25), 0.5); elseif (b <= 2e+154) tmp = Float64(Float64(1.0 / fma(Float64(b * b), Float64(Float64(b * b) * 0.25), -4.0)) * fma(b, Float64(b * 0.5), -2.0)); else tmp = Float64(2.0 / Float64(b * b)); end return tmp end
code[a_, b_] := If[LessEqual[b, 7.5e-5], N[(a * N[(N[(a * a), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * 0.0020833333333333333 + -0.020833333333333332), $MachinePrecision] + 0.25), $MachinePrecision] + 0.5), $MachinePrecision], If[LessEqual[b, 2e+154], N[(N[(1.0 / N[(N[(b * b), $MachinePrecision] * N[(N[(b * b), $MachinePrecision] * 0.25), $MachinePrecision] + -4.0), $MachinePrecision]), $MachinePrecision] * N[(b * N[(b * 0.5), $MachinePrecision] + -2.0), $MachinePrecision]), $MachinePrecision], N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 7.5 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(a, \mathsf{fma}\left(a \cdot a, \mathsf{fma}\left(a \cdot a, 0.0020833333333333333, -0.020833333333333332\right), 0.25\right), 0.5\right)\\
\mathbf{elif}\;b \leq 2 \cdot 10^{+154}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(b \cdot b, \left(b \cdot b\right) \cdot 0.25, -4\right)} \cdot \mathsf{fma}\left(b, b \cdot 0.5, -2\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{b \cdot b}\\
\end{array}
\end{array}
if b < 7.49999999999999934e-5Initial program 98.9%
Taylor expanded in b around 0
Applied rewrites75.7%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6457.6
Applied rewrites57.6%
if 7.49999999999999934e-5 < b < 2.00000000000000007e154Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f6497.0
Applied rewrites97.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f646.2
Applied rewrites6.2%
Taylor expanded in b around inf
*-commutativeN/A
lower-*.f645.6
Applied rewrites5.6%
lift-*.f64N/A
flip-+N/A
associate-/r/N/A
lower-*.f64N/A
Applied rewrites54.0%
if 2.00000000000000007e154 < b Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64100.0
Applied rewrites100.0%
Taylor expanded in b around inf
lower-/.f64N/A
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
Final simplification63.4%
(FPCore (a b) :precision binary64 (if (<= b 1.95) (fma a 0.25 0.5) (/ 2.0 (* b b))))
double code(double a, double b) {
double tmp;
if (b <= 1.95) {
tmp = fma(a, 0.25, 0.5);
} else {
tmp = 2.0 / (b * b);
}
return tmp;
}
function code(a, b) tmp = 0.0 if (b <= 1.95) tmp = fma(a, 0.25, 0.5); else tmp = Float64(2.0 / Float64(b * b)); end return tmp end
code[a_, b_] := If[LessEqual[b, 1.95], N[(a * 0.25 + 0.5), $MachinePrecision], N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.95:\\
\;\;\;\;\mathsf{fma}\left(a, 0.25, 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{b \cdot b}\\
\end{array}
\end{array}
if b < 1.94999999999999996Initial program 98.9%
Taylor expanded in b around 0
Applied rewrites75.6%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6456.9
Applied rewrites56.9%
if 1.94999999999999996 < b Initial program 100.0%
Taylor expanded in a around 0
lower-/.f64N/A
lower-+.f64N/A
lower-exp.f64100.0
Applied rewrites100.0%
Taylor expanded in b around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6457.3
Applied rewrites57.3%
Taylor expanded in b around inf
lower-/.f64N/A
unpow2N/A
lower-*.f6457.3
Applied rewrites57.3%
(FPCore (a b) :precision binary64 (fma a 0.25 0.5))
double code(double a, double b) {
return fma(a, 0.25, 0.5);
}
function code(a, b) return fma(a, 0.25, 0.5) end
code[a_, b_] := N[(a * 0.25 + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(a, 0.25, 0.5\right)
\end{array}
Initial program 99.2%
Taylor expanded in b around 0
Applied rewrites63.6%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f6442.3
Applied rewrites42.3%
(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
lower-+.f64N/A
lower-exp.f6485.3
Applied rewrites85.3%
Taylor expanded in b around 0
Applied rewrites41.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 2024219
(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))))