| Alternative 1 | |
|---|---|
| Error | 0.9 |
| Cost | 25924 |
\[\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{b}{e^{a} + 1}\\
\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + e^{b}\right)\\
\end{array}
\]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b) :precision binary64 (if (<= a -370.0) (/ b (+ (exp a) 1.0)) (if (<= a -1e-27) (log1p (exp a)) (log (+ (expm1 b) 2.0)))))
double code(double a, double b) {
return log((exp(a) + exp(b)));
}
double code(double a, double b) {
double tmp;
if (a <= -370.0) {
tmp = b / (exp(a) + 1.0);
} else if (a <= -1e-27) {
tmp = log1p(exp(a));
} else {
tmp = log((expm1(b) + 2.0));
}
return tmp;
}
public static double code(double a, double b) {
return Math.log((Math.exp(a) + Math.exp(b)));
}
public static double code(double a, double b) {
double tmp;
if (a <= -370.0) {
tmp = b / (Math.exp(a) + 1.0);
} else if (a <= -1e-27) {
tmp = Math.log1p(Math.exp(a));
} else {
tmp = Math.log((Math.expm1(b) + 2.0));
}
return tmp;
}
def code(a, b): return math.log((math.exp(a) + math.exp(b)))
def code(a, b): tmp = 0 if a <= -370.0: tmp = b / (math.exp(a) + 1.0) elif a <= -1e-27: tmp = math.log1p(math.exp(a)) else: tmp = math.log((math.expm1(b) + 2.0)) return tmp
function code(a, b) return log(Float64(exp(a) + exp(b))) end
function code(a, b) tmp = 0.0 if (a <= -370.0) tmp = Float64(b / Float64(exp(a) + 1.0)); elseif (a <= -1e-27) tmp = log1p(exp(a)); else tmp = log(Float64(expm1(b) + 2.0)); end return tmp end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := If[LessEqual[a, -370.0], N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[a, -1e-27], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[Log[N[(N[(Exp[b] - 1), $MachinePrecision] + 2.0), $MachinePrecision]], $MachinePrecision]]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
\mathbf{if}\;a \leq -370:\\
\;\;\;\;\frac{b}{e^{a} + 1}\\
\mathbf{elif}\;a \leq -1 \cdot 10^{-27}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(\mathsf{expm1}\left(b\right) + 2\right)\\
\end{array}
Results
if a < -370Initial program 58.3
Taylor expanded in b around 0 0
Simplified0
Taylor expanded in b around inf 0
if -370 < a < -1e-27Initial program 4.9
Taylor expanded in b around 0 6.1
Simplified2.6
if -1e-27 < a Initial program 1.5
Taylor expanded in a around 0 1.5
Applied egg-rr1.5
Simplified1.5
Final simplification0.8
| Alternative 1 | |
|---|---|
| Error | 0.9 |
| Cost | 25924 |
| Alternative 2 | |
|---|---|
| Error | 1.4 |
| Cost | 19652 |
| Alternative 3 | |
|---|---|
| Error | 1.1 |
| Cost | 19652 |
| Alternative 4 | |
|---|---|
| Error | 1.5 |
| Cost | 19396 |
| Alternative 5 | |
|---|---|
| Error | 1.5 |
| Cost | 19392 |
| Alternative 6 | |
|---|---|
| Error | 0.8 |
| Cost | 13256 |
| Alternative 7 | |
|---|---|
| Error | 2.1 |
| Cost | 13252 |
| Alternative 8 | |
|---|---|
| Error | 0.8 |
| Cost | 13128 |
| Alternative 9 | |
|---|---|
| Error | 27.9 |
| Cost | 6852 |
| Alternative 10 | |
|---|---|
| Error | 27.8 |
| Cost | 6724 |
| Alternative 11 | |
|---|---|
| Error | 28.2 |
| Cost | 6596 |
| Alternative 12 | |
|---|---|
| Error | 62.3 |
| Cost | 192 |
| Alternative 13 | |
|---|---|
| Error | 56.3 |
| Cost | 192 |
herbie shell --seed 2022340
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))