?

Average Error: 29.6 → 0.8
Time: 17.2s
Precision: binary64
Cost: 13128

?

\[ \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 -390:\\ \;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\ \mathbf{elif}\;a \leq -5 \cdot 10^{-45}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (if (<= a -390.0)
   (/ b (+ 2.0 (expm1 a)))
   (if (<= a -5e-45) (log1p (exp a)) (log1p (exp b)))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	double tmp;
	if (a <= -390.0) {
		tmp = b / (2.0 + expm1(a));
	} else if (a <= -5e-45) {
		tmp = log1p(exp(a));
	} else {
		tmp = log1p(exp(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 tmp;
	if (a <= -390.0) {
		tmp = b / (2.0 + Math.expm1(a));
	} else if (a <= -5e-45) {
		tmp = Math.log1p(Math.exp(a));
	} else {
		tmp = Math.log1p(Math.exp(b));
	}
	return tmp;
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
def code(a, b):
	tmp = 0
	if a <= -390.0:
		tmp = b / (2.0 + math.expm1(a))
	elif a <= -5e-45:
		tmp = math.log1p(math.exp(a))
	else:
		tmp = math.log1p(math.exp(b))
	return tmp
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function code(a, b)
	tmp = 0.0
	if (a <= -390.0)
		tmp = Float64(b / Float64(2.0 + expm1(a)));
	elseif (a <= -5e-45)
		tmp = log1p(exp(a));
	else
		tmp = log1p(exp(b));
	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, -390.0], N[(b / N[(2.0 + N[(Exp[a] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[a, -5e-45], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision]]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
\mathbf{if}\;a \leq -390:\\
\;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\

\mathbf{elif}\;a \leq -5 \cdot 10^{-45}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\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 < -390

    1. Initial program 58.3

      \[\log \left(e^{a} + e^{b}\right) \]
    2. Taylor expanded in b around 0 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

      [Start]0.0

      \[ \log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}} \]

      log1p-def [=>]0

      \[ \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + \frac{b}{1 + e^{a}} \]
    4. Taylor expanded in b around inf 0.0

      \[\leadsto \color{blue}{\frac{b}{1 + e^{a}}} \]
    5. Simplified0.0

      \[\leadsto \color{blue}{\frac{b}{2 + \mathsf{expm1}\left(a\right)}} \]
      Proof

      [Start]0.0

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

      +-commutative [=>]0.0

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

      metadata-eval [<=]0.0

      \[ \frac{b}{e^{a} + \color{blue}{\left(2 - 1\right)}} \]

      associate--l+ [<=]0.0

      \[ \frac{b}{\color{blue}{\left(e^{a} + 2\right) - 1}} \]

      +-commutative [<=]0.0

      \[ \frac{b}{\color{blue}{\left(2 + e^{a}\right)} - 1} \]

      associate--l+ [=>]0.0

      \[ \frac{b}{\color{blue}{2 + \left(e^{a} - 1\right)}} \]

      expm1-def [=>]0.0

      \[ \frac{b}{2 + \color{blue}{\mathsf{expm1}\left(a\right)}} \]

    if -390 < a < -4.99999999999999976e-45

    1. Initial program 2.4

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

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
      Proof

      [Start]3.5

      \[ \log \left(1 + e^{a}\right) \]

      log1p-def [=>]1.9

      \[ \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]

    if -4.99999999999999976e-45 < 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. Simplified1.5

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
      Proof

      [Start]1.5

      \[ \log \left(1 + e^{b}\right) \]

      log1p-def [=>]1.5

      \[ \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.8

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

Alternatives

Alternative 1
Error0.9
Cost25924
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + e^{b}\right)\\ \end{array} \]
Alternative 2
Error1.2
Cost20036
\[\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 3
Error1.3
Cost19652
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{a} + \left(b + 1\right)\right)\\ \end{array} \]
Alternative 4
Error1.5
Cost19396
\[\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 5
Error1.4
Cost19392
\[\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right) \]
Alternative 6
Error1.7
Cost7108
\[\begin{array}{l} \mathbf{if}\;a \leq -1.4:\\ \;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{b + 2} + \log \left(b + 2\right)\\ \end{array} \]
Alternative 7
Error27.4
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -1:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log \left(b + \left(a + 2\right)\right)\\ \end{array} \]
Alternative 8
Error1.7
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -1:\\ \;\;\;\;\frac{b}{2 + \mathsf{expm1}\left(a\right)}\\ \mathbf{else}:\\ \;\;\;\;\log \left(b + \left(a + 2\right)\right)\\ \end{array} \]
Alternative 9
Error27.7
Cost6724
\[\begin{array}{l} \mathbf{if}\;a \leq -1:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log \left(a + 2\right)\\ \end{array} \]
Alternative 10
Error28.0
Cost6596
\[\begin{array}{l} \mathbf{if}\;a \leq -170:\\ \;\;\;\;\frac{b}{2}\\ \mathbf{else}:\\ \;\;\;\;\log 2\\ \end{array} \]
Alternative 11
Error56.3
Cost192
\[\frac{b}{2} \]

Error

Reproduce?

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