Average Error: 29.9 → 0.8
Time: 20.3s
Precision: binary64
Cost: 26052
\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\log \left(e^{a} + e^{b}\right) \]
\[\begin{array}{l} t_0 := e^{a} + 1\\ \mathbf{if}\;e^{a} \leq 2 \cdot 10^{-19}:\\ \;\;\;\;\frac{b}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\log \left(t_0 + \mathsf{expm1}\left(b\right)\right)\\ \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (let* ((t_0 (+ (exp a) 1.0)))
   (if (<= (exp a) 2e-19) (/ b t_0) (log (+ t_0 (expm1 b))))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	double t_0 = exp(a) + 1.0;
	double tmp;
	if (exp(a) <= 2e-19) {
		tmp = b / t_0;
	} else {
		tmp = log((t_0 + expm1(b)));
	}
	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 t_0 = Math.exp(a) + 1.0;
	double tmp;
	if (Math.exp(a) <= 2e-19) {
		tmp = b / t_0;
	} else {
		tmp = Math.log((t_0 + Math.expm1(b)));
	}
	return tmp;
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
def code(a, b):
	t_0 = math.exp(a) + 1.0
	tmp = 0
	if math.exp(a) <= 2e-19:
		tmp = b / t_0
	else:
		tmp = math.log((t_0 + math.expm1(b)))
	return tmp
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function code(a, b)
	t_0 = Float64(exp(a) + 1.0)
	tmp = 0.0
	if (exp(a) <= 2e-19)
		tmp = Float64(b / t_0);
	else
		tmp = log(Float64(t_0 + expm1(b)));
	end
	return tmp
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[a_, b_] := Block[{t$95$0 = N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]}, If[LessEqual[N[Exp[a], $MachinePrecision], 2e-19], N[(b / t$95$0), $MachinePrecision], N[Log[N[(t$95$0 + N[(Exp[b] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
t_0 := e^{a} + 1\\
\mathbf{if}\;e^{a} \leq 2 \cdot 10^{-19}:\\
\;\;\;\;\frac{b}{t_0}\\

\mathbf{else}:\\
\;\;\;\;\log \left(t_0 + \mathsf{expm1}\left(b\right)\right)\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 2 regimes
  2. if (exp.f64 a) < 2e-19

    1. Initial program 58.3

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Taylor expanded in b around 0 0.2

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}}} \]
    3. Simplified0

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right) + \frac{b}{1 + e^{a}}} \]
      Proof
      (+.f64 (log1p.f64 (exp.f64 a)) (/.f64 b (+.f64 1 (exp.f64 a)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= log1p-def_binary64 (log.f64 (+.f64 1 (exp.f64 a)))) (/.f64 b (+.f64 1 (exp.f64 a)))): 4 points increase in error, 1 points decrease in error
    4. Taylor expanded in b around inf 0.2

      \[\leadsto \color{blue}{\frac{b}{1 + e^{a}}} \]

    if 2e-19 < (exp.f64 a)

    1. Initial program 1.5

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Applied egg-rr1.5

      \[\leadsto \log \color{blue}{\left(\left(1 + \left(e^{a} + e^{b}\right)\right) - 1\right)} \]
    3. Applied egg-rr1.5

      \[\leadsto \log \color{blue}{\left(\left(1 + e^{a}\right) + \mathsf{expm1}\left(b\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{a} \leq 2 \cdot 10^{-19}:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(\left(e^{a} + 1\right) + \mathsf{expm1}\left(b\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Error1.1
Cost83712
\[\begin{array}{l} t_0 := \mathsf{log1p}\left(e^{a}\right)\\ t_1 := \sqrt[3]{t_0}\\ t_0 + \frac{b}{{\left(e^{{\left({\left(\sqrt[3]{t_1}\right)}^{3}\right)}^{2}}\right)}^{t_1}} \end{array} \]
Alternative 2
Error0.8
Cost25924
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 2 \cdot 10^{-19}:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + e^{b}\right)\\ \end{array} \]
Alternative 3
Error1.1
Cost19648
\[\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1} \]
Alternative 4
Error1.6
Cost19396
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 5
Error1.5
Cost19396
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \end{array} \]
Alternative 6
Error2.0
Cost13252
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0.05:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;a \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 7
Error32.5
Cost6720
\[\log 2 + b \cdot 0.5 \]
Alternative 8
Error32.7
Cost6592
\[\log \left(b + 2\right) \]
Alternative 9
Error33.0
Cost6464
\[\log 2 \]
Alternative 10
Error62.3
Cost192
\[a \cdot 0.5 \]

Error

Reproduce

herbie shell --seed 2022311 
(FPCore (a b)
  :name "symmetry log of sum of exp"
  :precision binary64
  (log (+ (exp a) (exp b))))