
(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:
Herbie found 12 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
NOTE: a and b should be sorted in increasing order before calling this function.
(FPCore (a b)
:precision binary64
(let* ((t_0 (+ (exp a) 1.0)))
(fma
(fma (fma 0.5 b 1.0) (pow t_0 -1.0) (* (/ -0.5 (pow t_0 2.0)) b))
b
(log1p (exp a)))))assert(a < b);
double code(double a, double b) {
double t_0 = exp(a) + 1.0;
return fma(fma(fma(0.5, b, 1.0), pow(t_0, -1.0), ((-0.5 / pow(t_0, 2.0)) * b)), b, log1p(exp(a)));
}
a, b = sort([a, b]) function code(a, b) t_0 = Float64(exp(a) + 1.0) return fma(fma(fma(0.5, b, 1.0), (t_0 ^ -1.0), Float64(Float64(-0.5 / (t_0 ^ 2.0)) * b)), b, log1p(exp(a))) end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := Block[{t$95$0 = N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]}, N[(N[(N[(0.5 * b + 1.0), $MachinePrecision] * N[Power[t$95$0, -1.0], $MachinePrecision] + N[(N[(-0.5 / N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision] * b + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
t_0 := e^{a} + 1\\
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, b, 1\right), {t\_0}^{-1}, \frac{-0.5}{{t\_0}^{2}} \cdot b\right), b, \mathsf{log1p}\left(e^{a}\right)\right)
\end{array}
\end{array}
Initial program 57.1%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites78.0%
Final simplification78.0%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (+ (/ b (+ (exp a) 1.0)) (log1p (exp a))))
assert(a < b);
double code(double a, double b) {
return (b / (exp(a) + 1.0)) + log1p(exp(a));
}
assert a < b;
public static double code(double a, double b) {
return (b / (Math.exp(a) + 1.0)) + Math.log1p(Math.exp(a));
}
[a, b] = sort([a, b]) def code(a, b): return (b / (math.exp(a) + 1.0)) + math.log1p(math.exp(a))
a, b = sort([a, b]) function code(a, b) return Float64(Float64(b / Float64(exp(a) + 1.0)) + log1p(exp(a))) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\frac{b}{e^{a} + 1} + \mathsf{log1p}\left(e^{a}\right)
\end{array}
Initial program 57.1%
Taylor expanded in b around 0
+-commutativeN/A
*-rgt-identityN/A
associate-*r/N/A
lower-+.f64N/A
associate-*r/N/A
*-rgt-identityN/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-exp.f64N/A
lower-log1p.f64N/A
lower-exp.f6477.7
Applied rewrites77.7%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (fma (fma 0.125 b 0.5) b (log1p (exp a))))
assert(a < b);
double code(double a, double b) {
return fma(fma(0.125, b, 0.5), b, log1p(exp(a)));
}
a, b = sort([a, b]) function code(a, b) return fma(fma(0.125, b, 0.5), b, log1p(exp(a))) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(0.125 * b + 0.5), $MachinePrecision] * b + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{fma}\left(\mathsf{fma}\left(0.125, b, 0.5\right), b, \mathsf{log1p}\left(e^{a}\right)\right)
\end{array}
Initial program 57.1%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites78.0%
Taylor expanded in a around 0
Applied rewrites57.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= b 2.2e-156) (log1p (exp a)) (log (+ 1.0 (exp b)))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (b <= 2.2e-156) {
tmp = log1p(exp(a));
} else {
tmp = log((1.0 + exp(b)));
}
return tmp;
}
assert a < b;
public static double code(double a, double b) {
double tmp;
if (b <= 2.2e-156) {
tmp = Math.log1p(Math.exp(a));
} else {
tmp = Math.log((1.0 + Math.exp(b)));
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if b <= 2.2e-156: tmp = math.log1p(math.exp(a)) else: tmp = math.log((1.0 + math.exp(b))) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (b <= 2.2e-156) tmp = log1p(exp(a)); else tmp = log(Float64(1.0 + 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, 2.2e-156], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[Log[N[(1.0 + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.2 \cdot 10^{-156}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(1 + e^{b}\right)\\
\end{array}
\end{array}
if b < 2.1999999999999999e-156Initial program 54.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6451.9
Applied rewrites51.9%
if 2.1999999999999999e-156 < b Initial program 68.2%
Taylor expanded in a around 0
Applied rewrites67.8%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= b 2.2e-156) (log1p (exp a)) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (b <= 2.2e-156) {
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 <= 2.2e-156) {
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 <= 2.2e-156: 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 <= 2.2e-156) 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, 2.2e-156], 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 2.2 \cdot 10^{-156}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\
\end{array}
\end{array}
if b < 2.1999999999999999e-156Initial program 54.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6451.9
Applied rewrites51.9%
if 2.1999999999999999e-156 < b Initial program 68.2%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6467.8
Applied rewrites67.8%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= b 2.2e-156) (log1p (exp a)) (fma (fma 0.125 b 0.5) b (log 2.0))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (b <= 2.2e-156) {
tmp = log1p(exp(a));
} else {
tmp = fma(fma(0.125, b, 0.5), b, log(2.0));
}
return tmp;
}
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (b <= 2.2e-156) tmp = log1p(exp(a)); else tmp = fma(fma(0.125, b, 0.5), b, log(2.0)); end return tmp end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[LessEqual[b, 2.2e-156], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[(N[(0.125 * b + 0.5), $MachinePrecision] * b + N[Log[2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.2 \cdot 10^{-156}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.125, b, 0.5\right), b, \log 2\right)\\
\end{array}
\end{array}
if b < 2.1999999999999999e-156Initial program 54.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6451.9
Applied rewrites51.9%
if 2.1999999999999999e-156 < b Initial program 68.2%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites95.4%
Taylor expanded in a around 0
Applied rewrites65.2%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (fma 0.5 b (log1p (exp a))))
assert(a < b);
double code(double a, double b) {
return fma(0.5, b, log1p(exp(a)));
}
a, b = sort([a, b]) function code(a, b) return fma(0.5, b, log1p(exp(a))) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(0.5 * b + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{fma}\left(0.5, b, \mathsf{log1p}\left(e^{a}\right)\right)
\end{array}
Initial program 57.1%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites78.0%
Taylor expanded in b around 0
Applied rewrites77.7%
Taylor expanded in a around 0
Applied rewrites57.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (fma (fma (fma (* b b) -0.005208333333333333 0.125) b 0.5) b (log 2.0)))
assert(a < b);
double code(double a, double b) {
return fma(fma(fma((b * b), -0.005208333333333333, 0.125), b, 0.5), b, log(2.0));
}
a, b = sort([a, b]) function code(a, b) return fma(fma(fma(Float64(b * b), -0.005208333333333333, 0.125), b, 0.5), b, log(2.0)) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(N[(N[(b * b), $MachinePrecision] * -0.005208333333333333 + 0.125), $MachinePrecision] * b + 0.5), $MachinePrecision] * b + N[Log[2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(b \cdot b, -0.005208333333333333, 0.125\right), b, 0.5\right), b, \log 2\right)
\end{array}
Initial program 57.1%
lift-log.f64N/A
lift-+.f64N/A
flip-+N/A
clear-numN/A
log-recN/A
lower-neg.f64N/A
lower-log.f64N/A
clear-numN/A
flip-+N/A
lift-+.f64N/A
lower-/.f6457.1
lift-+.f64N/A
+-commutativeN/A
lower-+.f6457.1
Applied rewrites57.1%
Taylor expanded in a around 0
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-log1p.f64N/A
lower-exp.f6453.5
Applied rewrites53.5%
Taylor expanded in b around 0
Applied rewrites52.5%
Final simplification52.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (fma (fma 0.125 b 0.5) b (log 2.0)))
assert(a < b);
double code(double a, double b) {
return fma(fma(0.125, b, 0.5), b, log(2.0));
}
a, b = sort([a, b]) function code(a, b) return fma(fma(0.125, b, 0.5), b, log(2.0)) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(0.125 * b + 0.5), $MachinePrecision] * b + N[Log[2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{fma}\left(\mathsf{fma}\left(0.125, b, 0.5\right), b, \log 2\right)
\end{array}
Initial program 57.1%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
Applied rewrites78.0%
Taylor expanded in a around 0
Applied rewrites52.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (fma 0.5 b (log 2.0)))
assert(a < b);
double code(double a, double b) {
return fma(0.5, b, log(2.0));
}
a, b = sort([a, b]) function code(a, b) return fma(0.5, b, log(2.0)) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(0.5 * b + N[Log[2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{fma}\left(0.5, b, \log 2\right)
\end{array}
Initial program 57.1%
lift-log.f64N/A
lift-+.f64N/A
flip-+N/A
clear-numN/A
log-recN/A
lower-neg.f64N/A
lower-log.f64N/A
clear-numN/A
flip-+N/A
lift-+.f64N/A
lower-/.f6457.1
lift-+.f64N/A
+-commutativeN/A
lower-+.f6457.1
Applied rewrites57.1%
Taylor expanded in a around 0
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-log1p.f64N/A
lower-exp.f6453.5
Applied rewrites53.5%
Taylor expanded in b around 0
Applied rewrites52.3%
Final simplification52.3%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (log1p (+ 1.0 b)))
assert(a < b);
double code(double a, double b) {
return log1p((1.0 + b));
}
assert a < b;
public static double code(double a, double b) {
return Math.log1p((1.0 + b));
}
[a, b] = sort([a, b]) def code(a, b): return math.log1p((1.0 + b))
a, b = sort([a, b]) function code(a, b) return log1p(Float64(1.0 + b)) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[Log[1 + N[(1.0 + b), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{log1p}\left(1 + b\right)
\end{array}
Initial program 57.1%
lift-log.f64N/A
lift-+.f64N/A
flip-+N/A
clear-numN/A
log-recN/A
lower-neg.f64N/A
lower-log.f64N/A
clear-numN/A
flip-+N/A
lift-+.f64N/A
lower-/.f6457.1
lift-+.f64N/A
+-commutativeN/A
lower-+.f6457.1
Applied rewrites57.1%
Taylor expanded in a around 0
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-log1p.f64N/A
lower-exp.f6453.5
Applied rewrites53.5%
Taylor expanded in b around 0
Applied rewrites51.5%
Final simplification51.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (log1p 1.0))
assert(a < b);
double code(double a, double b) {
return log1p(1.0);
}
assert a < b;
public static double code(double a, double b) {
return Math.log1p(1.0);
}
[a, b] = sort([a, b]) def code(a, b): return math.log1p(1.0)
a, b = sort([a, b]) function code(a, b) return log1p(1.0) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[Log[1 + 1.0], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{log1p}\left(1\right)
\end{array}
Initial program 57.1%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6453.8
Applied rewrites53.8%
Taylor expanded in a around 0
Applied rewrites51.8%
herbie shell --seed 2024318
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))