Average Error: 29.8 → 0.8
Time: 17.3s
Precision: binary64
Cost: 13256
\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\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} \]
(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}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 3 regimes
  2. if a < -370

    1. Initial program 58.3

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

      \[\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)))): 0 points increase in error, 0 points decrease in error
    4. Taylor expanded in b around inf 0

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

    if -370 < a < -1e-27

    1. Initial program 4.9

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

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
      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)))): 0 points increase in error, 0 points decrease in error

    if -1e-27 < a

    1. Initial program 1.5

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

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

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

      \[\leadsto \log \color{blue}{\left(2 + \mathsf{expm1}\left(b\right)\right)} \]
      Proof
      (log.f64 (+.f64 2 (expm1.f64 b))): 0 points increase in error, 0 points decrease in error
      (log.f64 (+.f64 2 (Rewrite<= expm1-def_binary64 (-.f64 (exp.f64 b) 1)))): 5 points increase in error, 0 points decrease in error
      (log.f64 (Rewrite<= associate--l+_binary64 (-.f64 (+.f64 2 (exp.f64 b)) 1))): 0 points increase in error, 5 points decrease in error
      (log.f64 (-.f64 (+.f64 (Rewrite<= metadata-eval (+.f64 1 1)) (exp.f64 b)) 1)): 5 points increase in error, 0 points decrease in error
      (log.f64 (-.f64 (Rewrite<= associate-+r+_binary64 (+.f64 1 (+.f64 1 (exp.f64 b)))) 1)): 0 points increase in error, 5 points decrease in error
  3. Recombined 3 regimes into one program.
  4. Final simplification0.8

    \[\leadsto \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} \]

Alternatives

Alternative 1
Error0.9
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.4
Cost19652
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + \left(b + 1\right)\right)\\ \end{array} \]
Alternative 3
Error1.1
Cost19652
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{2}\\ \end{array} \]
Alternative 4
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 5
Error1.5
Cost19392
\[\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right) \]
Alternative 6
Error0.8
Cost13256
\[\begin{array}{l} \mathbf{if}\;a \leq -410:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq -1 \cdot 10^{-27}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + e^{b}\right)\\ \end{array} \]
Alternative 7
Error2.1
Cost13252
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 10^{-11}:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;a \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 8
Error0.8
Cost13128
\[\begin{array}{l} \mathbf{if}\;a \leq -390:\\ \;\;\;\;\frac{b}{e^{a} + 1}\\ \mathbf{elif}\;a \leq -1 \cdot 10^{-27}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
Alternative 9
Error27.9
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -1.4:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;a \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 10
Error27.8
Cost6724
\[\begin{array}{l} \mathbf{if}\;a \leq -110:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log \left(b + 2\right)\\ \end{array} \]
Alternative 11
Error28.2
Cost6596
\[\begin{array}{l} \mathbf{if}\;a \leq -185:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log 2\\ \end{array} \]
Alternative 12
Error62.3
Cost192
\[a \cdot 0.5 \]
Alternative 13
Error56.3
Cost192
\[\frac{b}{2} \]

Error

Reproduce

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