
(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 (/ 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%
add-exp-log99.2%
div-exp99.2%
Applied egg-rr99.2%
exp-diff99.2%
add-exp-log99.2%
+-commutative99.2%
clear-num99.2%
+-commutative99.2%
frac-2neg99.2%
metadata-eval99.2%
Applied egg-rr99.2%
rem-exp-log99.2%
exp-diff99.2%
sub-neg99.2%
+-commutative99.2%
prod-exp99.2%
rem-exp-log99.2%
distribute-lft-in68.7%
rec-exp68.7%
lft-mult-inverse99.2%
prod-exp100.0%
Simplified100.0%
Taylor expanded in a around inf 100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.99999996) (exp a) (/ 1.0 (+ 2.0 (* b (+ 1.0 (* b 0.5)))))))
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.99999996) {
tmp = exp(a);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5))));
}
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.99999996d0) then
tmp = exp(a)
else
tmp = 1.0d0 / (2.0d0 + (b * (1.0d0 + (b * 0.5d0))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 0.99999996) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5))));
}
return tmp;
}
def code(a, b): tmp = 0 if math.exp(a) <= 0.99999996: tmp = math.exp(a) else: tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5)))) return tmp
function code(a, b) tmp = 0.0 if (exp(a) <= 0.99999996) tmp = exp(a); else tmp = Float64(1.0 / Float64(2.0 + Float64(b * Float64(1.0 + Float64(b * 0.5))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (exp(a) <= 0.99999996) tmp = exp(a); else tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 0.99999996], N[Exp[a], $MachinePrecision], N[(1.0 / N[(2.0 + N[(b * N[(1.0 + N[(b * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0.99999996:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + b \cdot \left(1 + b \cdot 0.5\right)}\\
\end{array}
\end{array}
if (exp.f64 a) < 0.99999996000000002Initial program 97.6%
add-exp-log97.6%
div-exp97.7%
Applied egg-rr97.7%
Taylor expanded in a around inf 95.6%
if 0.99999996000000002 < (exp.f64 a) Initial program 100.0%
Taylor expanded in a around 0 98.9%
Taylor expanded in b around 0 65.1%
unpow265.1%
Simplified65.1%
associate-*r*65.1%
distribute-rgt1-in65.1%
Applied egg-rr65.1%
Final simplification75.1%
(FPCore (a b) :precision binary64 (if (<= b -1.1) (exp a) (if (<= b 1.05e+144) (/ (exp a) 2.0) (/ 1.0 (+ 2.0 (* b (* b 0.5)))))))
double code(double a, double b) {
double tmp;
if (b <= -1.1) {
tmp = exp(a);
} else if (b <= 1.05e+144) {
tmp = exp(a) / 2.0;
} else {
tmp = 1.0 / (2.0 + (b * (b * 0.5)));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= (-1.1d0)) then
tmp = exp(a)
else if (b <= 1.05d+144) then
tmp = exp(a) / 2.0d0
else
tmp = 1.0d0 / (2.0d0 + (b * (b * 0.5d0)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -1.1) {
tmp = Math.exp(a);
} else if (b <= 1.05e+144) {
tmp = Math.exp(a) / 2.0;
} else {
tmp = 1.0 / (2.0 + (b * (b * 0.5)));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -1.1: tmp = math.exp(a) elif b <= 1.05e+144: tmp = math.exp(a) / 2.0 else: tmp = 1.0 / (2.0 + (b * (b * 0.5))) return tmp
function code(a, b) tmp = 0.0 if (b <= -1.1) tmp = exp(a); elseif (b <= 1.05e+144) tmp = Float64(exp(a) / 2.0); else tmp = Float64(1.0 / Float64(2.0 + Float64(b * Float64(b * 0.5)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -1.1) tmp = exp(a); elseif (b <= 1.05e+144) tmp = exp(a) / 2.0; else tmp = 1.0 / (2.0 + (b * (b * 0.5))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -1.1], N[Exp[a], $MachinePrecision], If[LessEqual[b, 1.05e+144], N[(N[Exp[a], $MachinePrecision] / 2.0), $MachinePrecision], N[(1.0 / N[(2.0 + N[(b * N[(b * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.1:\\
\;\;\;\;e^{a}\\
\mathbf{elif}\;b \leq 1.05 \cdot 10^{+144}:\\
\;\;\;\;\frac{e^{a}}{2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + b \cdot \left(b \cdot 0.5\right)}\\
\end{array}
\end{array}
if b < -1.1000000000000001Initial program 95.1%
add-exp-log95.1%
div-exp95.2%
Applied egg-rr95.2%
Taylor expanded in a around inf 92.4%
if -1.1000000000000001 < b < 1.04999999999999998e144Initial program 100.0%
Taylor expanded in a around 0 98.3%
Taylor expanded in b around 0 86.1%
if 1.04999999999999998e144 < b Initial program 100.0%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 93.2%
unpow293.2%
Simplified93.2%
Taylor expanded in b around inf 93.2%
unpow293.2%
*-commutative93.2%
associate-*r*93.2%
Simplified93.2%
Final simplification88.2%
(FPCore (a b) :precision binary64 (if (<= a -28000000.0) (exp a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -28000000.0) {
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 <= (-28000000.0d0)) 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 <= -28000000.0) {
tmp = Math.exp(a);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -28000000.0: tmp = math.exp(a) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -28000000.0) 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 <= -28000000.0) tmp = exp(a); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -28000000.0], 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 -28000000:\\
\;\;\;\;e^{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -2.8e7Initial program 98.7%
add-exp-log98.7%
div-exp98.8%
Applied egg-rr98.8%
Taylor expanded in a around inf 100.0%
if -2.8e7 < a Initial program 99.4%
Taylor expanded in a around 0 98.3%
Final simplification98.8%
(FPCore (a b) :precision binary64 (if (<= b 1.65e-91) (+ 0.5 (* a 0.25)) (/ 1.0 (+ 2.0 (* b (+ 1.0 (* b 0.5)))))))
double code(double a, double b) {
double tmp;
if (b <= 1.65e-91) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5))));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 1.65d-91) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 1.0d0 / (2.0d0 + (b * (1.0d0 + (b * 0.5d0))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 1.65e-91) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5))));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 1.65e-91: tmp = 0.5 + (a * 0.25) else: tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5)))) return tmp
function code(a, b) tmp = 0.0 if (b <= 1.65e-91) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(1.0 / Float64(2.0 + Float64(b * Float64(1.0 + Float64(b * 0.5))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 1.65e-91) tmp = 0.5 + (a * 0.25); else tmp = 1.0 / (2.0 + (b * (1.0 + (b * 0.5)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 1.65e-91], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(2.0 + N[(b * N[(1.0 + N[(b * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.65 \cdot 10^{-91}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + b \cdot \left(1 + b \cdot 0.5\right)}\\
\end{array}
\end{array}
if b < 1.65000000000000006e-91Initial program 98.7%
Taylor expanded in b around 0 79.0%
Taylor expanded in a around 0 51.7%
*-commutative51.7%
Simplified51.7%
if 1.65000000000000006e-91 < b Initial program 100.0%
Taylor expanded in a around 0 92.9%
Taylor expanded in b around 0 53.8%
unpow253.8%
Simplified53.8%
associate-*r*53.8%
distribute-rgt1-in53.8%
Applied egg-rr53.8%
Final simplification52.5%
(FPCore (a b) :precision binary64 (if (<= b 9.8e-92) (+ 0.5 (* a 0.25)) (/ 1.0 (+ 2.0 (* b (* b 0.5))))))
double code(double a, double b) {
double tmp;
if (b <= 9.8e-92) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (b * 0.5)));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 9.8d-92) then
tmp = 0.5d0 + (a * 0.25d0)
else
tmp = 1.0d0 / (2.0d0 + (b * (b * 0.5d0)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 9.8e-92) {
tmp = 0.5 + (a * 0.25);
} else {
tmp = 1.0 / (2.0 + (b * (b * 0.5)));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 9.8e-92: tmp = 0.5 + (a * 0.25) else: tmp = 1.0 / (2.0 + (b * (b * 0.5))) return tmp
function code(a, b) tmp = 0.0 if (b <= 9.8e-92) tmp = Float64(0.5 + Float64(a * 0.25)); else tmp = Float64(1.0 / Float64(2.0 + Float64(b * Float64(b * 0.5)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 9.8e-92) tmp = 0.5 + (a * 0.25); else tmp = 1.0 / (2.0 + (b * (b * 0.5))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 9.8e-92], N[(0.5 + N[(a * 0.25), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(2.0 + N[(b * N[(b * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 9.8 \cdot 10^{-92}:\\
\;\;\;\;0.5 + a \cdot 0.25\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + b \cdot \left(b \cdot 0.5\right)}\\
\end{array}
\end{array}
if b < 9.8e-92Initial program 98.7%
Taylor expanded in b around 0 79.0%
Taylor expanded in a around 0 51.7%
*-commutative51.7%
Simplified51.7%
if 9.8e-92 < b Initial program 100.0%
Taylor expanded in a around 0 92.9%
Taylor expanded in b around 0 53.8%
unpow253.8%
Simplified53.8%
Taylor expanded in b around inf 53.3%
unpow253.3%
*-commutative53.3%
associate-*r*53.3%
Simplified53.3%
Final simplification52.3%
(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 98.9%
Taylor expanded in b around 0 80.9%
Taylor expanded in a around 0 52.8%
*-commutative52.8%
Simplified52.8%
if 1.6000000000000001 < b Initial program 100.0%
Taylor expanded in a around 0 99.8%
Taylor expanded in b around 0 51.0%
unpow251.0%
Simplified51.0%
Taylor expanded in b around inf 51.0%
unpow251.0%
Simplified51.0%
Final simplification52.3%
(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 68.1%
Taylor expanded in a around 0 37.8%
*-commutative37.8%
Simplified37.8%
Final simplification37.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 79.5%
Taylor expanded in b around 0 37.3%
Final simplification37.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 2023213
(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))))