symmetry log of sum of exp

Percentage Accurate: 54.0% → 98.2%
Time: 16.9s
Alternatives: 9
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \log \left(e^{a} + e^{b}\right) \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log((exp(a) + exp(b)))
end function
public static double code(double a, double b) {
	return Math.log((Math.exp(a) + Math.exp(b)));
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function tmp = code(a, b)
	tmp = log((exp(a) + exp(b)));
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(e^{a} + e^{b}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 54.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(e^{a} + e^{b}\right) \end{array} \]
(FPCore (a b) :precision binary64 (log (+ (exp a) (exp b))))
double code(double a, double b) {
	return log((exp(a) + exp(b)));
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log((exp(a) + exp(b)))
end function
public static double code(double a, double b) {
	return Math.log((Math.exp(a) + Math.exp(b)));
}
def code(a, b):
	return math.log((math.exp(a) + math.exp(b)))
function code(a, b)
	return log(Float64(exp(a) + exp(b)))
end
function tmp = code(a, b)
	tmp = log((exp(a) + exp(b)));
end
code[a_, b_] := N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(e^{a} + e^{b}\right)
\end{array}

Alternative 1: 98.2% accurate, 0.7× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \begin{array}{l} \mathbf{if}\;e^{a} \leq 1:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b)
 :precision binary64
 (if (<= (exp a) 1.0)
   (+ (log1p (exp a)) (/ b (+ (exp a) 1.0)))
   (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
	double tmp;
	if (exp(a) <= 1.0) {
		tmp = log1p(exp(a)) + (b / (exp(a) + 1.0));
	} else {
		tmp = log1p(exp(b));
	}
	return tmp;
}
assert a < b;
public static double code(double a, double b) {
	double tmp;
	if (Math.exp(a) <= 1.0) {
		tmp = Math.log1p(Math.exp(a)) + (b / (Math.exp(a) + 1.0));
	} else {
		tmp = Math.log1p(Math.exp(b));
	}
	return tmp;
}
[a, b] = sort([a, b])
def code(a, b):
	tmp = 0
	if math.exp(a) <= 1.0:
		tmp = math.log1p(math.exp(a)) + (b / (math.exp(a) + 1.0))
	else:
		tmp = math.log1p(math.exp(b))
	return tmp
a, b = sort([a, b])
function code(a, b)
	tmp = 0.0
	if (exp(a) <= 1.0)
		tmp = Float64(log1p(exp(a)) + Float64(b / Float64(exp(a) + 1.0)));
	else
		tmp = log1p(exp(b));
	end
	return tmp
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 1.0], N[(N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision] + N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 1:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 a) < 1

    1. Initial program 55.5%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right) + \frac{b}{1 + e^{a}}} \]
    4. Step-by-step derivation
      1. log1p-define78.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} + \frac{b}{1 + e^{a}} \]
    5. Simplified78.0%

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

    if 1 < (exp.f64 a)

    1. Initial program 69.8%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
    4. Step-by-step derivation
      1. log1p-define20.8%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
    5. Simplified20.8%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{a} \leq 1:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right) + \frac{b}{e^{a} + 1}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 95.6% accurate, 1.0× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \begin{array}{l} \mathbf{if}\;e^{a} \leq 1:\\ \;\;\;\;\mathsf{log1p}\left(e^{a} + b\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b)
 :precision binary64
 (if (<= (exp a) 1.0) (log1p (+ (exp a) b)) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
	double tmp;
	if (exp(a) <= 1.0) {
		tmp = log1p((exp(a) + b));
	} else {
		tmp = log1p(exp(b));
	}
	return tmp;
}
assert a < b;
public static double code(double a, double b) {
	double tmp;
	if (Math.exp(a) <= 1.0) {
		tmp = Math.log1p((Math.exp(a) + b));
	} else {
		tmp = Math.log1p(Math.exp(b));
	}
	return tmp;
}
[a, b] = sort([a, b])
def code(a, b):
	tmp = 0
	if math.exp(a) <= 1.0:
		tmp = math.log1p((math.exp(a) + b))
	else:
		tmp = math.log1p(math.exp(b))
	return tmp
a, b = sort([a, b])
function code(a, b)
	tmp = 0.0
	if (exp(a) <= 1.0)
		tmp = log1p(Float64(exp(a) + b));
	else
		tmp = log1p(exp(b));
	end
	return tmp
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := If[LessEqual[N[Exp[a], $MachinePrecision], 1.0], N[Log[1 + N[(N[Exp[a], $MachinePrecision] + b), $MachinePrecision]], $MachinePrecision], N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;e^{a} \leq 1:\\
\;\;\;\;\mathsf{log1p}\left(e^{a} + b\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (exp.f64 a) < 1

    1. Initial program 55.5%

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

      \[\leadsto \log \color{blue}{\left(1 + \left(e^{a} + b \cdot \left(1 + 0.5 \cdot b\right)\right)\right)} \]
    4. Step-by-step derivation
      1. associate-+r+54.2%

        \[\leadsto \log \color{blue}{\left(\left(1 + e^{a}\right) + b \cdot \left(1 + 0.5 \cdot b\right)\right)} \]
      2. *-commutative54.2%

        \[\leadsto \log \left(\left(1 + e^{a}\right) + b \cdot \left(1 + \color{blue}{b \cdot 0.5}\right)\right) \]
    5. Simplified54.2%

      \[\leadsto \log \color{blue}{\left(\left(1 + e^{a}\right) + b \cdot \left(1 + b \cdot 0.5\right)\right)} \]
    6. Taylor expanded in b around 0 53.4%

      \[\leadsto \log \left(\left(1 + e^{a}\right) + \color{blue}{b}\right) \]
    7. Step-by-step derivation
      1. associate-+l+53.4%

        \[\leadsto \log \color{blue}{\left(1 + \left(e^{a} + b\right)\right)} \]
      2. log1p-define76.9%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a} + b\right)} \]
    8. Applied egg-rr76.9%

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

    if 1 < (exp.f64 a)

    1. Initial program 69.8%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
    4. Step-by-step derivation
      1. log1p-define20.8%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
    5. Simplified20.8%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 52.3% accurate, 1.5× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \begin{array}{l} \mathbf{if}\;b \leq 3.5 \cdot 10^{-84}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\ \end{array} \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b)
 :precision binary64
 (if (<= b 3.5e-84) (log1p (exp a)) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
	double tmp;
	if (b <= 3.5e-84) {
		tmp = log1p(exp(a));
	} else {
		tmp = log1p(exp(b));
	}
	return tmp;
}
assert a < b;
public static double code(double a, double b) {
	double tmp;
	if (b <= 3.5e-84) {
		tmp = Math.log1p(Math.exp(a));
	} else {
		tmp = Math.log1p(Math.exp(b));
	}
	return tmp;
}
[a, b] = sort([a, b])
def code(a, b):
	tmp = 0
	if b <= 3.5e-84:
		tmp = math.log1p(math.exp(a))
	else:
		tmp = math.log1p(math.exp(b))
	return tmp
a, b = sort([a, b])
function code(a, b)
	tmp = 0.0
	if (b <= 3.5e-84)
		tmp = log1p(exp(a));
	else
		tmp = log1p(exp(b));
	end
	return tmp
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := If[LessEqual[b, 3.5e-84], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[Log[1 + N[Exp[b], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.5 \cdot 10^{-84}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 3.5000000000000001e-84

    1. Initial program 54.5%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right)} \]
    4. Step-by-step derivation
      1. log1p-define52.3%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
    5. Simplified52.3%

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

    if 3.5000000000000001e-84 < b

    1. Initial program 73.1%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
    4. Step-by-step derivation
      1. log1p-define68.6%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
    5. Simplified68.6%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{b}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 51.7% accurate, 1.5× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \begin{array}{l} \mathbf{if}\;b \leq 10^{-83}:\\ \;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + b \cdot \left(0.5 + b \cdot 0.125\right)\\ \end{array} \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b)
 :precision binary64
 (if (<= b 1e-83) (log1p (exp a)) (+ (log 2.0) (* b (+ 0.5 (* b 0.125))))))
assert(a < b);
double code(double a, double b) {
	double tmp;
	if (b <= 1e-83) {
		tmp = log1p(exp(a));
	} else {
		tmp = log(2.0) + (b * (0.5 + (b * 0.125)));
	}
	return tmp;
}
assert a < b;
public static double code(double a, double b) {
	double tmp;
	if (b <= 1e-83) {
		tmp = Math.log1p(Math.exp(a));
	} else {
		tmp = Math.log(2.0) + (b * (0.5 + (b * 0.125)));
	}
	return tmp;
}
[a, b] = sort([a, b])
def code(a, b):
	tmp = 0
	if b <= 1e-83:
		tmp = math.log1p(math.exp(a))
	else:
		tmp = math.log(2.0) + (b * (0.5 + (b * 0.125)))
	return tmp
a, b = sort([a, b])
function code(a, b)
	tmp = 0.0
	if (b <= 1e-83)
		tmp = log1p(exp(a));
	else
		tmp = Float64(log(2.0) + Float64(b * Float64(0.5 + Float64(b * 0.125))));
	end
	return tmp
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := If[LessEqual[b, 1e-83], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + N[(b * N[(0.5 + N[(b * 0.125), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 10^{-83}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\

\mathbf{else}:\\
\;\;\;\;\log 2 + b \cdot \left(0.5 + b \cdot 0.125\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 1e-83

    1. Initial program 54.5%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{a}\right)} \]
    4. Step-by-step derivation
      1. log1p-define52.3%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
    5. Simplified52.3%

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

    if 1e-83 < b

    1. Initial program 73.1%

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

      \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
    4. Taylor expanded in b around 0 64.4%

      \[\leadsto \color{blue}{\log 2 + b \cdot \left(0.5 + 0.125 \cdot b\right)} \]
    5. Step-by-step derivation
      1. *-commutative64.4%

        \[\leadsto \log 2 + b \cdot \left(0.5 + \color{blue}{b \cdot 0.125}\right) \]
    6. Simplified64.4%

      \[\leadsto \color{blue}{\log 2 + b \cdot \left(0.5 + b \cdot 0.125\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 49.6% accurate, 2.8× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \log 2 + b \cdot \left(0.5 + b \cdot 0.125\right) \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (+ (log 2.0) (* b (+ 0.5 (* b 0.125)))))
assert(a < b);
double code(double a, double b) {
	return log(2.0) + (b * (0.5 + (b * 0.125)));
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log(2.0d0) + (b * (0.5d0 + (b * 0.125d0)))
end function
assert a < b;
public static double code(double a, double b) {
	return Math.log(2.0) + (b * (0.5 + (b * 0.125)));
}
[a, b] = sort([a, b])
def code(a, b):
	return math.log(2.0) + (b * (0.5 + (b * 0.125)))
a, b = sort([a, b])
function code(a, b)
	return Float64(log(2.0) + Float64(b * Float64(0.5 + Float64(b * 0.125))))
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = log(2.0) + (b * (0.5 + (b * 0.125)));
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[(N[Log[2.0], $MachinePrecision] + N[(b * N[(0.5 + N[(b * 0.125), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log 2 + b \cdot \left(0.5 + b \cdot 0.125\right)
\end{array}
Derivation
  1. Initial program 56.2%

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

    \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
  4. Taylor expanded in b around 0 51.4%

    \[\leadsto \color{blue}{\log 2 + b \cdot \left(0.5 + 0.125 \cdot b\right)} \]
  5. Step-by-step derivation
    1. *-commutative51.4%

      \[\leadsto \log 2 + b \cdot \left(0.5 + \color{blue}{b \cdot 0.125}\right) \]
  6. Simplified51.4%

    \[\leadsto \color{blue}{\log 2 + b \cdot \left(0.5 + b \cdot 0.125\right)} \]
  7. Add Preprocessing

Alternative 6: 48.6% accurate, 2.8× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \log 2 + a \cdot \left(0.5 + a \cdot 0.125\right) \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (+ (log 2.0) (* a (+ 0.5 (* a 0.125)))))
assert(a < b);
double code(double a, double b) {
	return log(2.0) + (a * (0.5 + (a * 0.125)));
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log(2.0d0) + (a * (0.5d0 + (a * 0.125d0)))
end function
assert a < b;
public static double code(double a, double b) {
	return Math.log(2.0) + (a * (0.5 + (a * 0.125)));
}
[a, b] = sort([a, b])
def code(a, b):
	return math.log(2.0) + (a * (0.5 + (a * 0.125)))
a, b = sort([a, b])
function code(a, b)
	return Float64(log(2.0) + Float64(a * Float64(0.5 + Float64(a * 0.125))))
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = log(2.0) + (a * (0.5 + (a * 0.125)));
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[(N[Log[2.0], $MachinePrecision] + N[(a * N[(0.5 + N[(a * 0.125), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log 2 + a \cdot \left(0.5 + a \cdot 0.125\right)
\end{array}
Derivation
  1. Initial program 56.2%

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

    \[\leadsto \color{blue}{\log \left(1 + e^{a}\right)} \]
  4. Step-by-step derivation
    1. log1p-define53.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
  5. Simplified53.2%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
  6. Taylor expanded in a around 0 52.0%

    \[\leadsto \color{blue}{\log 2 + a \cdot \left(0.5 + 0.125 \cdot a\right)} \]
  7. Step-by-step derivation
    1. *-commutative52.0%

      \[\leadsto \log 2 + a \cdot \left(0.5 + \color{blue}{a \cdot 0.125}\right) \]
  8. Simplified52.0%

    \[\leadsto \color{blue}{\log 2 + a \cdot \left(0.5 + a \cdot 0.125\right)} \]
  9. Add Preprocessing

Alternative 7: 49.5% accurate, 2.9× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \log 2 + b \cdot 0.5 \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (+ (log 2.0) (* b 0.5)))
assert(a < b);
double code(double a, double b) {
	return log(2.0) + (b * 0.5);
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log(2.0d0) + (b * 0.5d0)
end function
assert a < b;
public static double code(double a, double b) {
	return Math.log(2.0) + (b * 0.5);
}
[a, b] = sort([a, b])
def code(a, b):
	return math.log(2.0) + (b * 0.5)
a, b = sort([a, b])
function code(a, b)
	return Float64(log(2.0) + Float64(b * 0.5))
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = log(2.0) + (b * 0.5);
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[(N[Log[2.0], $MachinePrecision] + N[(b * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log 2 + b \cdot 0.5
\end{array}
Derivation
  1. Initial program 56.2%

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

    \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
  4. Taylor expanded in b around 0 51.1%

    \[\leadsto \color{blue}{\log 2 + 0.5 \cdot b} \]
  5. Step-by-step derivation
    1. +-commutative51.1%

      \[\leadsto \color{blue}{0.5 \cdot b + \log 2} \]
  6. Simplified51.1%

    \[\leadsto \color{blue}{0.5 \cdot b + \log 2} \]
  7. Final simplification51.1%

    \[\leadsto \log 2 + b \cdot 0.5 \]
  8. Add Preprocessing

Alternative 8: 49.3% accurate, 2.9× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \log \left(b + 2\right) \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (log (+ b 2.0)))
assert(a < b);
double code(double a, double b) {
	return log((b + 2.0));
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log((b + 2.0d0))
end function
assert a < b;
public static double code(double a, double b) {
	return Math.log((b + 2.0));
}
[a, b] = sort([a, b])
def code(a, b):
	return math.log((b + 2.0))
a, b = sort([a, b])
function code(a, b)
	return log(Float64(b + 2.0))
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = log((b + 2.0));
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[Log[N[(b + 2.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log \left(b + 2\right)
\end{array}
Derivation
  1. Initial program 56.2%

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

    \[\leadsto \color{blue}{\log \left(1 + e^{b}\right)} \]
  4. Taylor expanded in b around 0 50.5%

    \[\leadsto \log \color{blue}{\left(2 + b\right)} \]
  5. Step-by-step derivation
    1. +-commutative50.5%

      \[\leadsto \log \color{blue}{\left(b + 2\right)} \]
  6. Simplified50.5%

    \[\leadsto \log \color{blue}{\left(b + 2\right)} \]
  7. Add Preprocessing

Alternative 9: 48.7% accurate, 3.0× speedup?

\[\begin{array}{l} [a, b] = \mathsf{sort}([a, b])\\ \\ \log 2 \end{array} \]
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b) :precision binary64 (log 2.0))
assert(a < b);
double code(double a, double b) {
	return log(2.0);
}
NOTE: a and b should be sorted in increasing order before calling this function.
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = log(2.0d0)
end function
assert a < b;
public static double code(double a, double b) {
	return Math.log(2.0);
}
[a, b] = sort([a, b])
def code(a, b):
	return math.log(2.0)
a, b = sort([a, b])
function code(a, b)
	return log(2.0)
end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
	tmp = log(2.0);
end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := N[Log[2.0], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log 2
\end{array}
Derivation
  1. Initial program 56.2%

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

    \[\leadsto \color{blue}{\log \left(1 + e^{a}\right)} \]
  4. Step-by-step derivation
    1. log1p-define53.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
  5. Simplified53.2%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{a}\right)} \]
  6. Taylor expanded in a around 0 50.6%

    \[\leadsto \color{blue}{\log 2} \]
  7. Add Preprocessing

Reproduce

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