\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\log \left(e^{a} + e^{b}\right)
\]
↓
\[\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}
\]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
↓
(FPCore (a b) :precision binary64 (+ (log1p (exp a)) (/ b (+ (exp a) 1.0))))
double code(double a, double b) {
return log((exp(a) + exp(b)));
}
↓
double code(double a, double b) {
return log1p(exp(a)) + (b / (exp(a) + 1.0));
}
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) {
return Math.log1p(Math.exp(a)) + (b / (Math.exp(a) + 1.0));
}
def code(a, b):
return math.log((math.exp(a) + math.exp(b)))
↓
def code(a, b):
return math.log1p(math.exp(a)) + (b / (math.exp(a) + 1.0))
function code(a, b)
return log(Float64(exp(a) + exp(b)))
end
↓
function code(a, b)
return Float64(log1p(exp(a)) + Float64(b / Float64(exp(a) + 1.0)))
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
↓
code[a_, b_] := N[(N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision] + N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\log \left(e^{a} + e^{b}\right)
↓
\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}
Alternatives
| Alternative 1 |
|---|
| Error | 1.73% |
|---|
| Cost | 20036 |
|---|
\[\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;\log \left(1 + \left(e^{a} + \left(b + 0.5 \cdot \left(b \cdot b\right)\right)\right)\right)\\
\end{array}
\]
| Alternative 2 |
|---|
| Error | 2.29% |
|---|
| Cost | 19396 |
|---|
\[\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\end{array}
\]
| Alternative 3 |
|---|
| Error | 2.1% |
|---|
| Cost | 19392 |
|---|
\[\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)
\]
| Alternative 4 |
|---|
| Error | 1.87% |
|---|
| Cost | 13252 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -37:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + \left(b + 1\right)\right)\\
\end{array}
\]
| Alternative 5 |
|---|
| Error | 42.82% |
|---|
| Cost | 6852 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -118:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;b \cdot 0.5 + \log 2\\
\end{array}
\]
| Alternative 6 |
|---|
| Error | 2.92% |
|---|
| Cost | 6852 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -37:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\
\mathbf{else}:\\
\;\;\;\;b \cdot 0.5 + \log 2\\
\end{array}
\]
| Alternative 7 |
|---|
| Error | 42.95% |
|---|
| Cost | 6724 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -1:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log \left(a + 2\right)\\
\end{array}
\]
| Alternative 8 |
|---|
| Error | 42.91% |
|---|
| Cost | 6724 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -145:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log \left(b + 2\right)\\
\end{array}
\]
| Alternative 9 |
|---|
| Error | 43.45% |
|---|
| Cost | 6596 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -160:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log 2\\
\end{array}
\]
| Alternative 10 |
|---|
| Error | 97.38% |
|---|
| Cost | 192 |
|---|
\[a \cdot 0.5
\]
| Alternative 11 |
|---|
| Error | 88.09% |
|---|
| Cost | 192 |
|---|
\[b \cdot 0.5
\]