
(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 11 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 (if (<= (exp a) 0.9999999999999991) (+ (/ b (+ (exp a) 1.0)) (log1p (exp a))) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (exp(a) <= 0.9999999999999991) {
tmp = (b / (exp(a) + 1.0)) + log1p(exp(a));
} else {
tmp = log1p(exp(b));
}
return tmp;
}
assert a < b;
public static double code(double a, double b) {
double tmp;
if (Math.exp(a) <= 0.9999999999999991) {
tmp = (b / (Math.exp(a) + 1.0)) + 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 math.exp(a) <= 0.9999999999999991: tmp = (b / (math.exp(a) + 1.0)) + 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 (exp(a) <= 0.9999999999999991) tmp = Float64(Float64(b / Float64(exp(a) + 1.0)) + 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[N[Exp[a], $MachinePrecision], 0.9999999999999991], N[(N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] + N[Log[1 + N[Exp[a], $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 0.9999999999999991:\\
\;\;\;\;\frac{b}{e^{a} + 1} + \mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\
\end{array}
\end{array}
if (exp.f64 a) < 0.99999999999999911Initial program 9.0%
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.f6498.8
Applied rewrites98.8%
if 0.99999999999999911 < (exp.f64 a) Initial program 69.9%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6466.3
Applied rewrites66.3%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= (+ (exp a) (exp b)) 1.9999999999999991) (+ (* 0.5 b) (log1p (exp a))) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
double tmp;
if ((exp(a) + exp(b)) <= 1.9999999999999991) {
tmp = (0.5 * b) + log1p(exp(a));
} else {
tmp = log1p(exp(b));
}
return tmp;
}
assert a < b;
public static double code(double a, double b) {
double tmp;
if ((Math.exp(a) + Math.exp(b)) <= 1.9999999999999991) {
tmp = (0.5 * b) + 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 (math.exp(a) + math.exp(b)) <= 1.9999999999999991: tmp = (0.5 * b) + 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 (Float64(exp(a) + exp(b)) <= 1.9999999999999991) tmp = Float64(Float64(0.5 * b) + 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[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision], 1.9999999999999991], N[(N[(0.5 * b), $MachinePrecision] + N[Log[1 + N[Exp[a], $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} + e^{b} \leq 1.9999999999999991:\\
\;\;\;\;0.5 \cdot b + \mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\
\end{array}
\end{array}
if (+.f64 (exp.f64 a) (exp.f64 b)) < 1.9999999999999991Initial program 9.5%
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.f6459.6
Applied rewrites59.6%
Taylor expanded in a around 0
Applied rewrites13.7%
if 1.9999999999999991 < (+.f64 (exp.f64 a) (exp.f64 b)) Initial program 98.3%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6493.4
Applied rewrites93.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= (+ (exp a) (exp b)) 1.9999999999999991) (log (+ (exp a) (+ 1.0 b))) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
double tmp;
if ((exp(a) + exp(b)) <= 1.9999999999999991) {
tmp = log((exp(a) + (1.0 + 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) + Math.exp(b)) <= 1.9999999999999991) {
tmp = Math.log((Math.exp(a) + (1.0 + 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) + math.exp(b)) <= 1.9999999999999991: tmp = math.log((math.exp(a) + (1.0 + b))) else: tmp = math.log1p(math.exp(b)) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (Float64(exp(a) + exp(b)) <= 1.9999999999999991) tmp = log(Float64(exp(a) + Float64(1.0 + 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[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision], 1.9999999999999991], N[Log[N[(N[Exp[a], $MachinePrecision] + N[(1.0 + b), $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} + e^{b} \leq 1.9999999999999991:\\
\;\;\;\;\log \left(e^{a} + \left(1 + b\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\
\end{array}
\end{array}
if (+.f64 (exp.f64 a) (exp.f64 b)) < 1.9999999999999991Initial program 9.5%
Taylor expanded in b around 0
lower-+.f645.5
Applied rewrites5.5%
if 1.9999999999999991 < (+.f64 (exp.f64 a) (exp.f64 b)) Initial program 98.3%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6493.4
Applied rewrites93.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= (exp b) 1.00000000000002) (+ (* 0.5 b) (log1p (exp a))) (log (+ (exp a) (exp b)))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (exp(b) <= 1.00000000000002) {
tmp = (0.5 * b) + log1p(exp(a));
} else {
tmp = log((exp(a) + exp(b)));
}
return tmp;
}
assert a < b;
public static double code(double a, double b) {
double tmp;
if (Math.exp(b) <= 1.00000000000002) {
tmp = (0.5 * b) + Math.log1p(Math.exp(a));
} else {
tmp = Math.log((Math.exp(a) + Math.exp(b)));
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if math.exp(b) <= 1.00000000000002: tmp = (0.5 * b) + math.log1p(math.exp(a)) else: tmp = math.log((math.exp(a) + math.exp(b))) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (exp(b) <= 1.00000000000002) tmp = Float64(Float64(0.5 * b) + log1p(exp(a))); else tmp = log(Float64(exp(a) + 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[b], $MachinePrecision], 1.00000000000002], N[(N[(0.5 * b), $MachinePrecision] + N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;e^{b} \leq 1.00000000000002:\\
\;\;\;\;0.5 \cdot b + \mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(e^{a} + e^{b}\right)\\
\end{array}
\end{array}
if (exp.f64 b) < 1.00000000000002Initial program 49.5%
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.f6476.9
Applied rewrites76.9%
Taylor expanded in a around 0
Applied rewrites51.8%
if 1.00000000000002 < (exp.f64 b) Initial program 72.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= b 2.9e-94) (log1p (exp a)) (log1p (exp b))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (b <= 2.9e-94) {
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.9e-94) {
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.9e-94: 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.9e-94) 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.9e-94], 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.9 \cdot 10^{-94}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{b}\right)\\
\end{array}
\end{array}
if b < 2.89999999999999995e-94Initial program 48.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6446.6
Applied rewrites46.6%
if 2.89999999999999995e-94 < b Initial program 64.6%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6460.5
Applied rewrites60.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= b 2.9e-94) (log1p (exp a)) (log (+ 1.0 (fma (fma (fma 0.16666666666666666 b 0.5) b 1.0) b 1.0)))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (b <= 2.9e-94) {
tmp = log1p(exp(a));
} else {
tmp = log((1.0 + fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 1.0)));
}
return tmp;
}
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (b <= 2.9e-94) tmp = log1p(exp(a)); else tmp = log(Float64(1.0 + fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 1.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.9e-94], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision], N[Log[N[(1.0 + N[(N[(N[(0.16666666666666666 * b + 0.5), $MachinePrecision] * b + 1.0), $MachinePrecision] * b + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.9 \cdot 10^{-94}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(1 + \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, b, 0.5\right), b, 1\right), b, 1\right)\right)\\
\end{array}
\end{array}
if b < 2.89999999999999995e-94Initial program 48.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6446.6
Applied rewrites46.6%
if 2.89999999999999995e-94 < b Initial program 64.6%
Taylor expanded in a around 0
Applied rewrites60.4%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6456.1
Applied rewrites56.1%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (log (+ 1.0 (fma (fma (fma 0.16666666666666666 b 0.5) b 1.0) b 1.0))))
assert(a < b);
double code(double a, double b) {
return log((1.0 + fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 1.0)));
}
a, b = sort([a, b]) function code(a, b) return log(Float64(1.0 + fma(fma(fma(0.16666666666666666, b, 0.5), b, 1.0), b, 1.0))) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[Log[N[(1.0 + N[(N[(N[(0.16666666666666666 * b + 0.5), $MachinePrecision] * b + 1.0), $MachinePrecision] * b + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log \left(1 + \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, b, 0.5\right), b, 1\right), b, 1\right)\right)
\end{array}
Initial program 50.4%
Taylor expanded in a around 0
Applied rewrites47.0%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6444.8
Applied rewrites44.8%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (log (+ 1.0 (fma (fma 0.5 b 1.0) b 1.0))))
assert(a < b);
double code(double a, double b) {
return log((1.0 + fma(fma(0.5, b, 1.0), b, 1.0)));
}
a, b = sort([a, b]) function code(a, b) return log(Float64(1.0 + fma(fma(0.5, b, 1.0), b, 1.0))) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[Log[N[(1.0 + N[(N[(0.5 * b + 1.0), $MachinePrecision] * b + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\log \left(1 + \mathsf{fma}\left(\mathsf{fma}\left(0.5, b, 1\right), b, 1\right)\right)
\end{array}
Initial program 50.4%
Taylor expanded in b around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6448.0
Applied rewrites48.0%
Taylor expanded in a around 0
Applied rewrites45.3%
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 50.4%
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.0
Applied rewrites77.0%
Taylor expanded in a around 0
Applied rewrites45.1%
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 50.4%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6447.2
Applied rewrites47.2%
Taylor expanded in a around 0
Applied rewrites44.7%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* (* a a) 0.125))
assert(a < b);
double code(double a, double b) {
return (a * 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 = (a * a) * 0.125d0
end function
assert a < b;
public static double code(double a, double b) {
return (a * a) * 0.125;
}
[a, b] = sort([a, b]) def code(a, b): return (a * a) * 0.125
a, b = sort([a, b]) function code(a, b) return Float64(Float64(a * a) * 0.125) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = (a * a) * 0.125;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(a * a), $MachinePrecision] * 0.125), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\left(a \cdot a\right) \cdot 0.125
\end{array}
Initial program 50.4%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6447.2
Applied rewrites47.2%
Taylor expanded in a around 0
Applied rewrites45.1%
Taylor expanded in a around inf
Applied rewrites4.3%
herbie shell --seed 2024323
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))