?

Average Error: 29.1 → 0.9
Time: 16.2s
Precision: binary64
Cost: 25924

?

\[ \begin{array}{c}[a, b] = \mathsf{sort}([a, b])\\ \end{array} \]
\[\log \left(e^{a} + e^{b}\right) \]
\[\begin{array}{l} \mathbf{if}\;e^{b} \leq 0.02:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\ \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
(FPCore (a b)
 :precision binary64
 (if (<= (exp b) 0.02) b (log1p (+ (exp a) (expm1 b)))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
double code(double a, double b) {
	double tmp;
	if (exp(b) <= 0.02) {
		tmp = b;
	} else {
		tmp = log1p((exp(a) + 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 tmp;
	if (Math.exp(b) <= 0.02) {
		tmp = b;
	} else {
		tmp = Math.log1p((Math.exp(a) + Math.expm1(b)));
	}
	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.02:
		tmp = b
	else:
		tmp = math.log1p((math.exp(a) + math.expm1(b)))
	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.02)
		tmp = b;
	else
		tmp = log1p(Float64(exp(a) + 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_] := If[LessEqual[N[Exp[b], $MachinePrecision], 0.02], b, N[Log[1 + N[(N[Exp[a], $MachinePrecision] + N[(Exp[b] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\log \left(e^{a} + e^{b}\right)
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 0.02:\\
\;\;\;\;b\\

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

    1. Initial program 52.8

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

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

      \[\leadsto \log \color{blue}{\left(e^{a} + \left(1 + b\right)\right)} \]
      Proof

      [Start]64.0

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

      associate-+r+ [=>]64.0

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

      +-commutative [=>]64.0

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

      associate-+l+ [=>]64.0

      \[ \log \color{blue}{\left(e^{a} + \left(1 + b\right)\right)} \]
    4. Applied egg-rr64.0

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a} + b\right)} \]
    5. Taylor expanded in b around inf 64.0

      \[\leadsto \mathsf{log1p}\left(\color{blue}{b}\right) \]
    6. Taylor expanded in b around 0 0

      \[\leadsto \color{blue}{b} \]

    if 0.0200000000000000004 < (exp.f64 b)

    1. Initial program 29.0

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

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

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

      [Start]29.5

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

      log-prod [<=]29.8

      \[ \color{blue}{\log \left(\sqrt{e^{a} + e^{b}} \cdot \sqrt{e^{a} + e^{b}}\right)} \]

      rem-square-sqrt [=>]29.0

      \[ \log \color{blue}{\left(e^{a} + e^{b}\right)} \]

      log1p-expm1 [<=]29.0

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

      expm1-def [<=]29.0

      \[ \mathsf{log1p}\left(\color{blue}{e^{\log \left(e^{a} + e^{b}\right)} - 1}\right) \]

      rem-exp-log [=>]29.0

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

      associate--l+ [=>]28.9

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

      expm1-def [=>]0.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{b} \leq 0.02:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + \mathsf{expm1}\left(b\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Error29.7
Cost26056
\[\begin{array}{l} \mathbf{if}\;e^{b} \leq 1:\\ \;\;\;\;b\\ \mathbf{elif}\;e^{b} \leq 1.001:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + b\right)\\ \mathbf{else}:\\ \;\;\;\;b\\ \end{array} \]
Alternative 2
Error1.1
Cost19648
\[\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1} \]
Alternative 3
Error1.5
Cost19396
\[\begin{array}{l} \mathbf{if}\;e^{a} \leq 0:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \end{array} \]
Alternative 4
Error2.0
Cost6852
\[\begin{array}{l} \mathbf{if}\;a \leq -70:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;b \cdot 0.5 + \log 2\\ \end{array} \]
Alternative 5
Error2.1
Cost6724
\[\begin{array}{l} \mathbf{if}\;a \leq -42:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;\log \left(b + 2\right)\\ \end{array} \]
Alternative 6
Error2.1
Cost6724
\[\begin{array}{l} \mathbf{if}\;a \leq -42:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(b + 1\right)\\ \end{array} \]
Alternative 7
Error2.4
Cost6596
\[\begin{array}{l} \mathbf{if}\;a \leq -44:\\ \;\;\;\;b\\ \mathbf{else}:\\ \;\;\;\;\log 2\\ \end{array} \]
Alternative 8
Error30.3
Cost64
\[b \]

Error

Reproduce?

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