Average Error: 29.7 → 0.6
Time: 20.8s
Precision: binary64
Cost: 45768
\[ \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} + e^{b}\\ t_1 := \mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+166}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (let* ((t_0 (+ (exp a) (exp b)))
        (t_1 (+ (log1p (exp a)) (/ b (+ (exp a) 1.0)))))
   (if (<= t_0 2.0) t_1 (if (<= t_0 5e+166) (log1p (exp b)) t_1))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	double t_0 = exp(a) + exp(b);
	double t_1 = log1p(exp(a)) + (b / (exp(a) + 1.0));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+166) {
		tmp = log1p(exp(b));
	} else {
		tmp = t_1;
	}
	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) + Math.exp(b);
	double t_1 = Math.log1p(Math.exp(a)) + (b / (Math.exp(a) + 1.0));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_1;
	} else if (t_0 <= 5e+166) {
		tmp = Math.log1p(Math.exp(b));
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
def code(a, b):
	t_0 = math.exp(a) + math.exp(b)
	t_1 = math.log1p(math.exp(a)) + (b / (math.exp(a) + 1.0))
	tmp = 0
	if t_0 <= 2.0:
		tmp = t_1
	elif t_0 <= 5e+166:
		tmp = math.log1p(math.exp(b))
	else:
		tmp = t_1
	return tmp
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function code(a, b)
	t_0 = Float64(exp(a) + exp(b))
	t_1 = Float64(log1p(exp(a)) + Float64(b / Float64(exp(a) + 1.0)))
	tmp = 0.0
	if (t_0 <= 2.0)
		tmp = t_1;
	elseif (t_0 <= 5e+166)
		tmp = log1p(exp(b));
	else
		tmp = t_1;
	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] + N[Exp[b], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision] + N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$1, If[LessEqual[t$95$0, 5e+166], N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision], t$95$1]]]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
t_0 := e^{a} + e^{b}\\
t_1 := \mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+166}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\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 (+.f64 (exp.f64 a) (exp.f64 b)) < 2 or 5.0000000000000002e166 < (+.f64 (exp.f64 a) (exp.f64 b))

    1. Initial program 30.7

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

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

      \[\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)))): 1 points increase in error, 1 points decrease in error

    if 2 < (+.f64 (exp.f64 a) (exp.f64 b)) < 5.0000000000000002e166

    1. Initial program 0.5

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

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
      Proof
      (log1p.f64 (exp.f64 b)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= log1p-def_binary64 (log.f64 (+.f64 1 (exp.f64 b)))): 5 points increase in error, 1 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{a} + e^{b} \leq 2:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\ \mathbf{elif}\;e^{a} + e^{b} \leq 5 \cdot 10^{+166}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\ \end{array} \]

Alternatives

Alternative 1
Error0.8
Cost25924
\[\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} \]
Alternative 2
Error1.5
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 3
Error1.8
Cost13636
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;b \cdot 0.5 + \left(b \cdot \left(b \cdot 0.125\right) + \log 2\right)\\ \end{array} \]
Alternative 4
Error27.8
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -86526.87079063947:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 5
Error2.2
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -86526.87079063947:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 6
Error27.9
Cost6724
\[\begin{array}{l} \mathbf{if}\;a \leq -86526.87079063947:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log \left(a + 2\right)\\ \end{array} \]
Alternative 7
Error27.8
Cost6724
\[\begin{array}{l} \mathbf{if}\;a \leq -86526.87079063947:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log \left(b + 2\right)\\ \end{array} \]
Alternative 8
Error28.2
Cost6596
\[\begin{array}{l} \mathbf{if}\;a \leq -86526.87079063947:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log 2\\ \end{array} \]
Alternative 9
Error56.3
Cost192
\[\frac{b}{2} \]

Error

Reproduce

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