\[\log \left(e^{a} + e^{b}\right)
\]
↓
\[\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.999995:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right) - \frac{a}{-1 - e^{b}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{1 + e^{a}} + \mathsf{log1p}\left(e^{a}\right)\\
\end{array}
\]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
↓
(FPCore (a b)
:precision binary64
(if (<= (exp b) 0.999995)
(- (log1p (exp b)) (/ a (- -1.0 (exp b))))
(+ (/ b (+ 1.0 (exp a))) (log1p (exp a)))))
double code(double a, double b) {
return log((exp(a) + exp(b)));
}
↓
double code(double a, double b) {
double tmp;
if (exp(b) <= 0.999995) {
tmp = log1p(exp(b)) - (a / (-1.0 - exp(b)));
} else {
tmp = (b / (1.0 + exp(a))) + log1p(exp(a));
}
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 (Math.exp(b) <= 0.999995) {
tmp = Math.log1p(Math.exp(b)) - (a / (-1.0 - Math.exp(b)));
} else {
tmp = (b / (1.0 + Math.exp(a))) + Math.log1p(Math.exp(a));
}
return tmp;
}
def code(a, b):
return math.log((math.exp(a) + math.exp(b)))
↓
def code(a, b):
tmp = 0
if math.exp(b) <= 0.999995:
tmp = math.log1p(math.exp(b)) - (a / (-1.0 - math.exp(b)))
else:
tmp = (b / (1.0 + math.exp(a))) + math.log1p(math.exp(a))
return tmp
function code(a, b)
return log(Float64(exp(a) + exp(b)))
end
↓
function code(a, b)
tmp = 0.0
if (exp(b) <= 0.999995)
tmp = Float64(log1p(exp(b)) - Float64(a / Float64(-1.0 - exp(b))));
else
tmp = Float64(Float64(b / Float64(1.0 + exp(a))) + log1p(exp(a)));
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[N[Exp[b], $MachinePrecision], 0.999995], N[(N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision] - N[(a / N[(-1.0 - N[Exp[b], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b / N[(1.0 + N[Exp[a], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\log \left(e^{a} + e^{b}\right)
↓
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.999995:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right) - \frac{a}{-1 - e^{b}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{1 + e^{a}} + \mathsf{log1p}\left(e^{a}\right)\\
\end{array}
Alternatives
| Alternative 1 |
|---|
| Error | 15.9 |
|---|
| Cost | 52036 |
|---|
\[\begin{array}{l}
t_0 := \mathsf{log1p}\left(e^{a}\right)\\
t_1 := 0.5 \cdot t_0\\
\mathbf{if}\;e^{b} \leq 1:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right) - \frac{a}{-1 - e^{b}}\\
\mathbf{else}:\\
\;\;\;\;\left(\frac{b}{e^{t_0}} + t_1\right) + t_1\\
\end{array}
\]
| Alternative 2 |
|---|
| Error | 1.2 |
|---|
| Cost | 26180 |
|---|
\[\begin{array}{l}
\mathbf{if}\;e^{b} \leq 2 \cdot 10^{-12}:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{1 + e^{a}} + \mathsf{log1p}\left(e^{a}\right)\\
\end{array}
\]
| Alternative 3 |
|---|
| Error | 1.8 |
|---|
| Cost | 19524 |
|---|
\[\begin{array}{l}
\mathbf{if}\;e^{a} \leq 0:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(a + e^{b}\right)\\
\end{array}
\]
| Alternative 4 |
|---|
| Error | 17.9 |
|---|
| Cost | 13260 |
|---|
\[\begin{array}{l}
t_0 := \mathsf{log1p}\left(e^{b}\right)\\
\mathbf{if}\;b \leq -260:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -5.2 \cdot 10^{-247}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 8.8 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\]
| Alternative 5 |
|---|
| Error | 17.2 |
|---|
| Cost | 13252 |
|---|
\[\begin{array}{l}
\mathbf{if}\;e^{b} \leq 2 \cdot 10^{-12}:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{else}:\\
\;\;\;\;\log \left(2 + \left(a + b\right)\right)\\
\end{array}
\]
| Alternative 6 |
|---|
| Error | 17.2 |
|---|
| Cost | 13128 |
|---|
\[\begin{array}{l}
\mathbf{if}\;b \leq -92:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -1.35 \cdot 10^{-278}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{elif}\;b \leq 8.8 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;\log \left(\left(2 + b\right) + \left(0.5 \cdot b\right) \cdot b\right)\\
\end{array}
\]
| Alternative 7 |
|---|
| Error | 18.1 |
|---|
| Cost | 8008 |
|---|
\[\begin{array}{l}
t_0 := \left(2 + b\right) + \left(0.5 \cdot b\right) \cdot b\\
t_1 := \log t_0\\
\mathbf{if}\;b \leq -50:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -3.6 \cdot 10^{-246}:\\
\;\;\;\;\frac{a}{t_0} + t_1\\
\mathbf{elif}\;b \leq 8.8 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\]
| Alternative 8 |
|---|
| Error | 18.1 |
|---|
| Cost | 7496 |
|---|
\[\begin{array}{l}
\mathbf{if}\;b \leq -1.4:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -5.2 \cdot 10^{-247}:\\
\;\;\;\;0.5 \cdot a + \left(\log 2 + \left(0.5 - 0.25 \cdot a\right) \cdot b\right)\\
\mathbf{elif}\;b \leq 8.8 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;\log \left(\left(2 + b\right) + \left(0.5 \cdot b\right) \cdot b\right)\\
\end{array}
\]
| Alternative 9 |
|---|
| Error | 18.3 |
|---|
| Cost | 7372 |
|---|
\[\begin{array}{l}
t_0 := \log \left(\left(2 + b\right) + \left(0.5 \cdot b\right) \cdot b\right)\\
\mathbf{if}\;b \leq -39:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -5.2 \cdot 10^{-247}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 8.8 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\]
| Alternative 10 |
|---|
| Error | 18.7 |
|---|
| Cost | 7244 |
|---|
\[\begin{array}{l}
t_0 := \log \left(2 + a\right) + 0.5 \cdot b\\
\mathbf{if}\;b \leq -1.4:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -5.2 \cdot 10^{-247}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 8.8 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\]
| Alternative 11 |
|---|
| Error | 18.7 |
|---|
| Cost | 7116 |
|---|
\[\begin{array}{l}
t_0 := \log \left(2 + \left(a + b\right)\right)\\
\mathbf{if}\;b \leq -1:\\
\;\;\;\;\frac{a}{1 + e^{b}}\\
\mathbf{elif}\;b \leq -5.2 \cdot 10^{-247}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 9.5 \cdot 10^{-300}:\\
\;\;\;\;\frac{b}{1 + e^{a}}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\]
| Alternative 12 |
|---|
| Error | 30.6 |
|---|
| Cost | 6852 |
|---|
\[\begin{array}{l}
\mathbf{if}\;b \leq -1:\\
\;\;\;\;\frac{a}{2}\\
\mathbf{else}:\\
\;\;\;\;\log \left(2 + \left(a + b\right)\right)\\
\end{array}
\]
| Alternative 13 |
|---|
| Error | 30.7 |
|---|
| Cost | 6596 |
|---|
\[\begin{array}{l}
\mathbf{if}\;b \leq -85:\\
\;\;\;\;\frac{a}{2}\\
\mathbf{else}:\\
\;\;\;\;\log 2\\
\end{array}
\]
| Alternative 14 |
|---|
| Error | 56.9 |
|---|
| Cost | 324 |
|---|
\[\begin{array}{l}
\mathbf{if}\;a \leq -2.46 \cdot 10^{-57}:\\
\;\;\;\;0.5 \cdot b\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{2}\\
\end{array}
\]
| Alternative 15 |
|---|
| Error | 59.4 |
|---|
| Cost | 192 |
|---|
\[0.5 \cdot b
\]