| Alternative 1 | |
|---|---|
| Error | 0.88% |
| Cost | 25920 |
\[e^{a - \log \left(e^{a} + e^{b}\right)}
\]
(FPCore (a b) :precision binary64 (/ (exp a) (+ (exp a) (exp b))))
(FPCore (a b) :precision binary64 (if (<= (exp a) 0.0) 0.0 (exp (- (log1p (exp b))))))
double code(double a, double b) {
return exp(a) / (exp(a) + exp(b));
}
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.0) {
tmp = 0.0;
} else {
tmp = exp(-log1p(exp(b)));
}
return tmp;
}
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(a) <= 0.0) {
tmp = 0.0;
} else {
tmp = Math.exp(-Math.log1p(Math.exp(b)));
}
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(a) <= 0.0: tmp = 0.0 else: tmp = math.exp(-math.log1p(math.exp(b))) 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(a) <= 0.0) tmp = 0.0; else tmp = exp(Float64(-log1p(exp(b)))); end return 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[LessEqual[N[Exp[a], $MachinePrecision], 0.0], 0.0, N[Exp[(-N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision])], $MachinePrecision]]
\frac{e^{a}}{e^{a} + e^{b}}
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;e^{-\mathsf{log1p}\left(e^{b}\right)}\\
\end{array}
Results
| Original | 1.03% |
|---|---|
| Target | 0.02% |
| Herbie | 1.5% |
if (exp.f64 a) < 0.0Initial program 1.41
Taylor expanded in a around 0 64.88
Taylor expanded in b around 0 96.13
Simplified96.13
[Start]96.13 | \[ \frac{1}{2 + b}
\] |
|---|---|
+-commutative [=>]96.13 | \[ \frac{1}{\color{blue}{b + 2}}
\] |
Applied egg-rr66.98
Taylor expanded in b around inf 0.59
if 0.0 < (exp.f64 a) Initial program 0.89
Applied egg-rr0.68
Taylor expanded in a around 0 1.83
Simplified1.83
[Start]1.83 | \[ e^{-1 \cdot \log \left(1 + e^{b}\right)}
\] |
|---|---|
mul-1-neg [=>]1.83 | \[ e^{\color{blue}{-\log \left(1 + e^{b}\right)}}
\] |
log1p-def [=>]1.83 | \[ e^{-\color{blue}{\mathsf{log1p}\left(e^{b}\right)}}
\] |
Final simplification1.5
| Alternative 1 | |
|---|---|
| Error | 0.88% |
| Cost | 25920 |
| Alternative 2 | |
|---|---|
| Error | 1.03% |
| Cost | 19520 |
| Alternative 3 | |
|---|---|
| Error | 1.5% |
| Cost | 13252 |
| Alternative 4 | |
|---|---|
| Error | 19.87% |
| Cost | 7108 |
| Alternative 5 | |
|---|---|
| Error | 35.23% |
| Cost | 1244 |
| Alternative 6 | |
|---|---|
| Error | 35.43% |
| Cost | 988 |
| Alternative 7 | |
|---|---|
| Error | 60.22% |
| Cost | 64 |
herbie shell --seed 2023115
(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))))