| Alternative 1 | |
|---|---|
| Error | 0.8 |
| Cost | 26185 |
\[\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.995 \lor \neg \left(e^{b} \leq 2\right):\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{a}}{e^{a} + 1}\\
\end{array}
\]
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
(FPCore (a b) :precision binary64 (if (or (<= (exp b) 0.995) (not (<= (exp b) 2.0))) (/ 1.0 (+ (exp b) 1.0)) (/ (exp a) (+ (exp a) (+ b 1.0)))))
double code(double a, double b) {
return exp(a) / (exp(a) + exp(b));
}
double code(double a, double b) {
double tmp;
if ((exp(b) <= 0.995) || !(exp(b) <= 2.0)) {
tmp = 1.0 / (exp(b) + 1.0);
} else {
tmp = exp(a) / (exp(a) + (b + 1.0));
}
return tmp;
}
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
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if ((exp(b) <= 0.995d0) .or. (.not. (exp(b) <= 2.0d0))) then
tmp = 1.0d0 / (exp(b) + 1.0d0)
else
tmp = exp(a) / (exp(a) + (b + 1.0d0))
end if
code = tmp
end function
public static double code(double a, double b) {
return Math.exp(a) / (Math.exp(a) + Math.exp(b));
}
public static double code(double a, double b) {
double tmp;
if ((Math.exp(b) <= 0.995) || !(Math.exp(b) <= 2.0)) {
tmp = 1.0 / (Math.exp(b) + 1.0);
} else {
tmp = Math.exp(a) / (Math.exp(a) + (b + 1.0));
}
return tmp;
}
def code(a, b): return math.exp(a) / (math.exp(a) + math.exp(b))
def code(a, b): tmp = 0 if (math.exp(b) <= 0.995) or not (math.exp(b) <= 2.0): tmp = 1.0 / (math.exp(b) + 1.0) else: tmp = math.exp(a) / (math.exp(a) + (b + 1.0)) return tmp
function code(a, b) return Float64(exp(a) / Float64(exp(a) + exp(b))) end
function code(a, b) tmp = 0.0 if ((exp(b) <= 0.995) || !(exp(b) <= 2.0)) tmp = Float64(1.0 / Float64(exp(b) + 1.0)); else tmp = Float64(exp(a) / Float64(exp(a) + Float64(b + 1.0))); end return tmp end
function tmp = code(a, b) tmp = exp(a) / (exp(a) + exp(b)); end
function tmp_2 = code(a, b) tmp = 0.0; if ((exp(b) <= 0.995) || ~((exp(b) <= 2.0))) tmp = 1.0 / (exp(b) + 1.0); else tmp = exp(a) / (exp(a) + (b + 1.0)); end tmp_2 = tmp; end
code[a_, b_] := N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, b_] := If[Or[LessEqual[N[Exp[b], $MachinePrecision], 0.995], N[Not[LessEqual[N[Exp[b], $MachinePrecision], 2.0]], $MachinePrecision]], N[(1.0 / N[(N[Exp[b], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[Exp[a], $MachinePrecision] / N[(N[Exp[a], $MachinePrecision] + N[(b + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{e^{a}}{e^{a} + e^{b}}
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.995 \lor \neg \left(e^{b} \leq 2\right):\\
\;\;\;\;\frac{1}{e^{b} + 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{e^{a}}{e^{a} + \left(b + 1\right)}\\
\end{array}
Results
| Original | 0.6 |
|---|---|
| Target | 0.0 |
| Herbie | 0.5 |
if (exp.f64 b) < 0.994999999999999996 or 2 < (exp.f64 b) Initial program 1.0
Taylor expanded in a around 0 0.4
if 0.994999999999999996 < (exp.f64 b) < 2Initial program 0.3
Taylor expanded in b around 0 0.6
Simplified0.6
[Start]0.6 | \[ \frac{e^{a}}{1 + \left(e^{a} + b\right)}
\] |
|---|---|
associate-+r+ [=>]0.6 | \[ \frac{e^{a}}{\color{blue}{\left(1 + e^{a}\right) + b}}
\] |
+-commutative [=>]0.6 | \[ \frac{e^{a}}{\color{blue}{\left(e^{a} + 1\right)} + b}
\] |
associate-+l+ [=>]0.6 | \[ \frac{e^{a}}{\color{blue}{e^{a} + \left(1 + b\right)}}
\] |
Final simplification0.5
| Alternative 1 | |
|---|---|
| Error | 0.8 |
| Cost | 26185 |
| Alternative 2 | |
|---|---|
| Error | 0.6 |
| Cost | 19520 |
| Alternative 3 | |
|---|---|
| Error | 0.9 |
| Cost | 13252 |
| Alternative 4 | |
|---|---|
| Error | 11.2 |
| Cost | 6724 |
| Alternative 5 | |
|---|---|
| Error | 13.3 |
| Cost | 708 |
| Alternative 6 | |
|---|---|
| Error | 22.2 |
| Cost | 452 |
| Alternative 7 | |
|---|---|
| Error | 22.3 |
| Cost | 196 |
| Alternative 8 | |
|---|---|
| Error | 38.7 |
| Cost | 64 |
herbie shell --seed 2023027
(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))))