
(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 8 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 -2.05e+18) (/ (exp a) a) (/ 1.0 (+ (exp b) 1.0))))
double code(double a, double b) {
double tmp;
if (a <= -2.05e+18) {
tmp = exp(a) / 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 <= (-2.05d+18)) then
tmp = exp(a) / 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 <= -2.05e+18) {
tmp = Math.exp(a) / a;
} else {
tmp = 1.0 / (Math.exp(b) + 1.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -2.05e+18: tmp = math.exp(a) / a else: tmp = 1.0 / (math.exp(b) + 1.0) return tmp
function code(a, b) tmp = 0.0 if (a <= -2.05e+18) tmp = Float64(exp(a) / 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 <= -2.05e+18) tmp = exp(a) / a; else tmp = 1.0 / (exp(b) + 1.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -2.05e+18], N[(N[Exp[a], $MachinePrecision] / a), $MachinePrecision], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2.05 \cdot 10^{+18}:\\
\;\;\;\;\frac{e^{a}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\end{array}
\end{array}
if a < -2.05e18Initial program 98.6%
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 -2.05e18 < a Initial program 98.9%
Taylor expanded in a around 0 99.5%
Final simplification99.7%
(FPCore (a b) :precision binary64 (log (exp (/ (exp a) (+ (exp a) (exp b))))))
double code(double a, double b) {
return log(exp((exp(a) / (exp(a) + exp(b)))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = log(exp((exp(a) / (exp(a) + exp(b)))))
end function
public static double code(double a, double b) {
return Math.log(Math.exp((Math.exp(a) / (Math.exp(a) + Math.exp(b)))));
}
def code(a, b): return math.log(math.exp((math.exp(a) / (math.exp(a) + math.exp(b)))))
function code(a, b) return log(exp(Float64(exp(a) / Float64(exp(a) + exp(b))))) end
function tmp = code(a, b) tmp = log(exp((exp(a) / (exp(a) + exp(b))))); end
code[a_, b_] := N[Log[N[Exp[N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\log \left(e^{\frac{e^{a}}{e^{a} + e^{b}}}\right)
\end{array}
Initial program 98.8%
add-log-exp98.8%
Applied egg-rr98.8%
Final simplification98.8%
(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 98.8%
Final simplification98.8%
(FPCore (a b) :precision binary64 (if (<= a -540000000000.0) (/ (exp a) a) (/ 1.0 (+ (+ b 2.0) (* 0.5 (* b b))))))
double code(double a, double b) {
double tmp;
if (a <= -540000000000.0) {
tmp = exp(a) / a;
} else {
tmp = 1.0 / ((b + 2.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 (a <= (-540000000000.0d0)) then
tmp = exp(a) / a
else
tmp = 1.0d0 / ((b + 2.0d0) + (0.5d0 * (b * b)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= -540000000000.0) {
tmp = Math.exp(a) / a;
} else {
tmp = 1.0 / ((b + 2.0) + (0.5 * (b * b)));
}
return tmp;
}
def code(a, b): tmp = 0 if a <= -540000000000.0: tmp = math.exp(a) / a else: tmp = 1.0 / ((b + 2.0) + (0.5 * (b * b))) return tmp
function code(a, b) tmp = 0.0 if (a <= -540000000000.0) tmp = Float64(exp(a) / a); else tmp = Float64(1.0 / Float64(Float64(b + 2.0) + Float64(0.5 * Float64(b * b)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= -540000000000.0) tmp = exp(a) / a; else tmp = 1.0 / ((b + 2.0) + (0.5 * (b * b))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, -540000000000.0], N[(N[Exp[a], $MachinePrecision] / a), $MachinePrecision], N[(1.0 / N[(N[(b + 2.0), $MachinePrecision] + N[(0.5 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -540000000000:\\
\;\;\;\;\frac{e^{a}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(b + 2\right) + 0.5 \cdot \left(b \cdot b\right)}\\
\end{array}
\end{array}
if a < -5.4e11Initial program 98.6%
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.4e11 < a Initial program 98.9%
Taylor expanded in a around 0 99.5%
Taylor expanded in b around 0 64.4%
associate-+r+64.4%
+-commutative64.4%
unpow264.4%
Simplified64.4%
Final simplification74.7%
(FPCore (a b) :precision binary64 (if (<= b -3100.0) 0.5 (/ 1.0 (+ (+ b 2.0) (* 0.5 (* b b))))))
double code(double a, double b) {
double tmp;
if (b <= -3100.0) {
tmp = 0.5;
} else {
tmp = 1.0 / ((b + 2.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 (b <= (-3100.0d0)) then
tmp = 0.5d0
else
tmp = 1.0d0 / ((b + 2.0d0) + (0.5d0 * (b * b)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= -3100.0) {
tmp = 0.5;
} else {
tmp = 1.0 / ((b + 2.0) + (0.5 * (b * b)));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= -3100.0: tmp = 0.5 else: tmp = 1.0 / ((b + 2.0) + (0.5 * (b * b))) return tmp
function code(a, b) tmp = 0.0 if (b <= -3100.0) tmp = 0.5; else tmp = Float64(1.0 / Float64(Float64(b + 2.0) + Float64(0.5 * Float64(b * b)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= -3100.0) tmp = 0.5; else tmp = 1.0 / ((b + 2.0) + (0.5 * (b * b))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, -3100.0], 0.5, N[(1.0 / N[(N[(b + 2.0), $MachinePrecision] + N[(0.5 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3100:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\left(b + 2\right) + 0.5 \cdot \left(b \cdot b\right)}\\
\end{array}
\end{array}
if b < -3100Initial program 97.7%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 18.8%
if -3100 < b Initial program 99.0%
Taylor expanded in a around 0 74.5%
Taylor expanded in b around 0 60.7%
associate-+r+60.7%
+-commutative60.7%
unpow260.7%
Simplified60.7%
Final simplification53.5%
(FPCore (a b) :precision binary64 (if (<= b 1.25) 0.5 (/ 1.0 (+ b (* 0.5 (* b b))))))
double code(double a, double b) {
double tmp;
if (b <= 1.25) {
tmp = 0.5;
} else {
tmp = 1.0 / (b + (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 (b <= 1.25d0) then
tmp = 0.5d0
else
tmp = 1.0d0 / (b + (0.5d0 * (b * b)))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 1.25) {
tmp = 0.5;
} else {
tmp = 1.0 / (b + (0.5 * (b * b)));
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 1.25: tmp = 0.5 else: tmp = 1.0 / (b + (0.5 * (b * b))) return tmp
function code(a, b) tmp = 0.0 if (b <= 1.25) tmp = 0.5; else tmp = Float64(1.0 / Float64(b + Float64(0.5 * Float64(b * b)))); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 1.25) tmp = 0.5; else tmp = 1.0 / (b + (0.5 * (b * b))); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 1.25], 0.5, N[(1.0 / N[(b + N[(0.5 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.25:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{b + 0.5 \cdot \left(b \cdot b\right)}\\
\end{array}
\end{array}
if b < 1.25Initial program 98.9%
Taylor expanded in a around 0 70.6%
Taylor expanded in b around 0 49.8%
if 1.25 < b Initial program 98.6%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 60.7%
associate-+r+60.7%
+-commutative60.7%
unpow260.7%
Simplified60.7%
Taylor expanded in b around inf 60.7%
unpow260.7%
Simplified60.7%
Final simplification52.8%
(FPCore (a b) :precision binary64 (if (<= b 2.0) 0.5 (/ 2.0 (* b b))))
double code(double a, double b) {
double tmp;
if (b <= 2.0) {
tmp = 0.5;
} 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 <= 2.0d0) then
tmp = 0.5d0
else
tmp = 2.0d0 / (b * b)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 2.0) {
tmp = 0.5;
} else {
tmp = 2.0 / (b * b);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 2.0: tmp = 0.5 else: tmp = 2.0 / (b * b) return tmp
function code(a, b) tmp = 0.0 if (b <= 2.0) tmp = 0.5; else tmp = Float64(2.0 / Float64(b * b)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 2.0) tmp = 0.5; else tmp = 2.0 / (b * b); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 2.0], 0.5, N[(2.0 / N[(b * b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2:\\
\;\;\;\;0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{b \cdot b}\\
\end{array}
\end{array}
if b < 2Initial program 98.9%
Taylor expanded in a around 0 70.6%
Taylor expanded in b around 0 49.8%
if 2 < b Initial program 98.6%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 60.7%
associate-+r+60.7%
+-commutative60.7%
unpow260.7%
Simplified60.7%
Taylor expanded in b around inf 60.7%
unpow260.7%
Simplified60.7%
Final simplification52.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 98.8%
Taylor expanded in a around 0 78.9%
Taylor expanded in b around 0 36.7%
Final simplification36.7%
(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 2023224
(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))))