
(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%
expm1-log1p-u99.2%
Applied egg-rr99.2%
Taylor expanded in a around inf 99.2%
*-lft-identity99.2%
associate-*l/99.2%
associate-/r/99.2%
rem-exp-log99.2%
exp-diff99.2%
sub-neg99.2%
exp-sum99.2%
rem-exp-log99.2%
*-commutative99.2%
distribute-rgt-in69.1%
rec-exp69.1%
rgt-mult-inverse100.0%
prod-exp100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (a b) :precision binary64 (if (<= a -2.5e-7) (/ 1.0 (+ 1.0 (exp (- a)))) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -2.5e-7) {
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 (a <= (-2.5d-7)) 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 (a <= -2.5e-7) {
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 a <= -2.5e-7: 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 (a <= -2.5e-7) 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 (a <= -2.5e-7) 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[a, -2.5e-7], 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}\;a \leq -2.5 \cdot 10^{-7}:\\
\;\;\;\;\frac{1}{1 + e^{-a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -2.49999999999999989e-7Initial program 100.0%
expm1-log1p-u100.0%
Applied egg-rr100.0%
Taylor expanded in a around inf 100.0%
*-lft-identity100.0%
associate-*l/100.0%
associate-/r/100.0%
rem-exp-log100.0%
exp-diff100.0%
sub-neg100.0%
exp-sum100.0%
rem-exp-log100.0%
*-commutative100.0%
distribute-rgt-in3.8%
rec-exp3.8%
rgt-mult-inverse100.0%
prod-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 100.0%
if -2.49999999999999989e-7 < a Initial program 98.8%
Taylor expanded in a around 0 99.2%
Final simplification99.4%
(FPCore (a b) :precision binary64 (if (<= a -580000000.0) (/ (exp a) a) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -580000000.0) {
tmp = exp(a) / 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 <= (-580000000.0d0)) then
tmp = exp(a) / 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 <= -580000000.0) {
tmp = Math.exp(a) / a;
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -580000000.0: tmp = math.exp(a) / a else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -580000000.0) tmp = Float64(exp(a) / 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 <= -580000000.0) tmp = exp(a) / a; else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -580000000.0], N[(N[Exp[a], $MachinePrecision] / a), $MachinePrecision], N[(1.0 / N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -580000000:\\
\;\;\;\;\frac{e^{a}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -5.8e8Initial program 100.0%
Taylor expanded in b around 0 100.0%
Taylor expanded in a around 0 100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in a around inf 100.0%
if -5.8e8 < a Initial program 98.8%
Taylor expanded in a around 0 98.0%
Final simplification98.6%
(FPCore (a b) :precision binary64 (if (<= a -2.65e-7) (/ (exp a) (+ a 2.0)) (/ 1.0 (+ 1.0 (exp b)))))
double code(double a, double b) {
double tmp;
if (a <= -2.65e-7) {
tmp = exp(a) / (a + 2.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 <= (-2.65d-7)) then
tmp = exp(a) / (a + 2.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 <= -2.65e-7) {
tmp = Math.exp(a) / (a + 2.0);
} else {
tmp = 1.0 / (1.0 + Math.exp(b));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -2.65e-7: tmp = math.exp(a) / (a + 2.0) else: tmp = 1.0 / (1.0 + math.exp(b)) return tmp
function code(a, b) tmp = 0.0 if (a <= -2.65e-7) tmp = Float64(exp(a) / Float64(a + 2.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 <= -2.65e-7) tmp = exp(a) / (a + 2.0); else tmp = 1.0 / (1.0 + exp(b)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -2.65e-7], N[(N[Exp[a], $MachinePrecision] / N[(a + 2.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 -2.65 \cdot 10^{-7}:\\
\;\;\;\;\frac{e^{a}}{a + 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{1 + e^{b}}\\
\end{array}
\end{array}
if a < -2.65e-7Initial program 100.0%
Taylor expanded in b around 0 100.0%
Taylor expanded in a around 0 97.6%
+-commutative97.6%
Simplified97.6%
if -2.65e-7 < a Initial program 98.8%
Taylor expanded in a around 0 99.2%
Final simplification98.7%
(FPCore (a b) :precision binary64 (if (<= a -460.0) (/ (exp a) a) (/ 1.0 (+ 2.0 (* a (+ -1.0 (* a 0.5)))))))
double code(double a, double b) {
double tmp;
if (a <= -460.0) {
tmp = exp(a) / a;
} else {
tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= (-460.0d0)) then
tmp = exp(a) / a
else
tmp = 1.0d0 / (2.0d0 + (a * ((-1.0d0) + (a * 0.5d0))))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -460.0) {
tmp = Math.exp(a) / a;
} else {
tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -460.0: tmp = math.exp(a) / a else: tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5)))) return tmp
function code(a, b) tmp = 0.0 if (a <= -460.0) tmp = Float64(exp(a) / a); else tmp = Float64(1.0 / Float64(2.0 + Float64(a * Float64(-1.0 + Float64(a * 0.5))))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -460.0) tmp = exp(a) / a; else tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5)))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -460.0], N[(N[Exp[a], $MachinePrecision] / a), $MachinePrecision], N[(1.0 / N[(2.0 + N[(a * N[(-1.0 + N[(a * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -460:\\
\;\;\;\;\frac{e^{a}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{2 + a \cdot \left(-1 + a \cdot 0.5\right)}\\
\end{array}
\end{array}
if a < -460Initial program 100.0%
Taylor expanded in b around 0 100.0%
Taylor expanded in a around 0 100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in a around inf 100.0%
if -460 < a Initial program 98.8%
expm1-log1p-u98.8%
Applied egg-rr98.8%
Taylor expanded in a around inf 98.8%
*-lft-identity98.8%
associate-*l/98.8%
associate-/r/98.8%
rem-exp-log98.8%
exp-diff98.9%
sub-neg98.9%
exp-sum98.8%
rem-exp-log98.8%
*-commutative98.8%
distribute-rgt-in98.8%
rec-exp98.8%
rgt-mult-inverse100.0%
prod-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 59.6%
Taylor expanded in a around 0 57.5%
*-commutative57.5%
*-commutative57.5%
unpow257.5%
associate-*l*57.5%
distribute-lft-out57.5%
Simplified57.5%
Final simplification70.3%
(FPCore (a b) :precision binary64 (/ 1.0 (+ 2.0 (* a (+ -1.0 (* a 0.5))))))
double code(double a, double b) {
return 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (2.0d0 + (a * ((-1.0d0) + (a * 0.5d0))))
end function
public static double code(double a, double b) {
return 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))));
}
def code(a, b): return 1.0 / (2.0 + (a * (-1.0 + (a * 0.5))))
function code(a, b) return Float64(1.0 / Float64(2.0 + Float64(a * Float64(-1.0 + Float64(a * 0.5))))) end
function tmp = code(a, b) tmp = 1.0 / (2.0 + (a * (-1.0 + (a * 0.5)))); end
code[a_, b_] := N[(1.0 / N[(2.0 + N[(a * N[(-1.0 + N[(a * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 + a \cdot \left(-1 + a \cdot 0.5\right)}
\end{array}
Initial program 99.2%
expm1-log1p-u99.2%
Applied egg-rr99.2%
Taylor expanded in a around inf 99.2%
*-lft-identity99.2%
associate-*l/99.2%
associate-/r/99.2%
rem-exp-log99.2%
exp-diff99.2%
sub-neg99.2%
exp-sum99.2%
rem-exp-log99.2%
*-commutative99.2%
distribute-rgt-in69.1%
rec-exp69.1%
rgt-mult-inverse100.0%
prod-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 71.8%
Taylor expanded in a around 0 53.4%
*-commutative53.4%
*-commutative53.4%
unpow253.4%
associate-*l*53.4%
distribute-lft-out53.4%
Simplified53.4%
Final simplification53.4%
(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 71.0%
Taylor expanded in a around 0 40.9%
*-commutative40.9%
Simplified40.9%
Final simplification40.9%
(FPCore (a b) :precision binary64 (/ 1.0 (- 2.0 a)))
double code(double a, double b) {
return 1.0 / (2.0 - a);
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = 1.0d0 / (2.0d0 - a)
end function
public static double code(double a, double b) {
return 1.0 / (2.0 - a);
}
def code(a, b): return 1.0 / (2.0 - a)
function code(a, b) return Float64(1.0 / Float64(2.0 - a)) end
function tmp = code(a, b) tmp = 1.0 / (2.0 - a); end
code[a_, b_] := N[(1.0 / N[(2.0 - a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 - a}
\end{array}
Initial program 99.2%
expm1-log1p-u99.2%
Applied egg-rr99.2%
Taylor expanded in a around inf 99.2%
*-lft-identity99.2%
associate-*l/99.2%
associate-/r/99.2%
rem-exp-log99.2%
exp-diff99.2%
sub-neg99.2%
exp-sum99.2%
rem-exp-log99.2%
*-commutative99.2%
distribute-rgt-in69.1%
rec-exp69.1%
rgt-mult-inverse100.0%
prod-exp100.0%
Simplified100.0%
Taylor expanded in b around 0 71.8%
Taylor expanded in a around 0 41.6%
mul-1-neg41.6%
unsub-neg41.6%
Simplified41.6%
Final simplification41.6%
(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 77.4%
Taylor expanded in b around 0 40.6%
Final simplification40.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 2024026
(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))))