
(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 8 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 (log1p (exp a))))
(if (or (<= b -4.2e-84) (not (<= b 8.2e-84)))
(+ t_0 (/ b (+ (exp a) 1.0)))
t_0)))assert(a < b);
double code(double a, double b) {
double t_0 = log1p(exp(a));
double tmp;
if ((b <= -4.2e-84) || !(b <= 8.2e-84)) {
tmp = t_0 + (b / (exp(a) + 1.0));
} else {
tmp = t_0;
}
return tmp;
}
assert a < b;
public static double code(double a, double b) {
double t_0 = Math.log1p(Math.exp(a));
double tmp;
if ((b <= -4.2e-84) || !(b <= 8.2e-84)) {
tmp = t_0 + (b / (Math.exp(a) + 1.0));
} else {
tmp = t_0;
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): t_0 = math.log1p(math.exp(a)) tmp = 0 if (b <= -4.2e-84) or not (b <= 8.2e-84): tmp = t_0 + (b / (math.exp(a) + 1.0)) else: tmp = t_0 return tmp
a, b = sort([a, b]) function code(a, b) t_0 = log1p(exp(a)) tmp = 0.0 if ((b <= -4.2e-84) || !(b <= 8.2e-84)) tmp = Float64(t_0 + Float64(b / Float64(exp(a) + 1.0))); else tmp = t_0; end return tmp end
NOTE: a and b should be sorted in increasing order before calling this function.
code[a_, b_] := Block[{t$95$0 = N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]}, If[Or[LessEqual[b, -4.2e-84], N[Not[LessEqual[b, 8.2e-84]], $MachinePrecision]], N[(t$95$0 + N[(b / N[(N[Exp[a], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
t_0 := \mathsf{log1p}\left(e^{a}\right)\\
\mathbf{if}\;b \leq -4.2 \cdot 10^{-84} \lor \neg \left(b \leq 8.2 \cdot 10^{-84}\right):\\
\;\;\;\;t_0 + \frac{b}{e^{a} + 1}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if b < -4.19999999999999996e-84 or 8.2000000000000001e-84 < b Initial program 71.5%
Taylor expanded in b around 0 45.3%
log1p-def45.3%
Simplified45.3%
if -4.19999999999999996e-84 < b < 8.2000000000000001e-84Initial program 99.0%
Taylor expanded in b around 0 99.0%
log1p-def99.2%
Simplified99.2%
Final simplification73.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (or (<= b -4.2e-84) (not (<= b 8.2e-84))) (log1p (+ b (exp a))) (log1p (exp a))))
assert(a < b);
double code(double a, double b) {
double tmp;
if ((b <= -4.2e-84) || !(b <= 8.2e-84)) {
tmp = log1p((b + exp(a)));
} else {
tmp = log1p(exp(a));
}
return tmp;
}
assert a < b;
public static double code(double a, double b) {
double tmp;
if ((b <= -4.2e-84) || !(b <= 8.2e-84)) {
tmp = Math.log1p((b + Math.exp(a)));
} else {
tmp = Math.log1p(Math.exp(a));
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if (b <= -4.2e-84) or not (b <= 8.2e-84): tmp = math.log1p((b + math.exp(a))) else: tmp = math.log1p(math.exp(a)) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if ((b <= -4.2e-84) || !(b <= 8.2e-84)) tmp = log1p(Float64(b + exp(a))); else tmp = log1p(exp(a)); end return tmp end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[Or[LessEqual[b, -4.2e-84], N[Not[LessEqual[b, 8.2e-84]], $MachinePrecision]], N[Log[1 + N[(b + N[Exp[a], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4.2 \cdot 10^{-84} \lor \neg \left(b \leq 8.2 \cdot 10^{-84}\right):\\
\;\;\;\;\mathsf{log1p}\left(b + e^{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\end{array}
\end{array}
if b < -4.19999999999999996e-84 or 8.2000000000000001e-84 < b Initial program 71.5%
Taylor expanded in b around 0 30.9%
associate-+r+30.9%
+-commutative30.9%
Simplified30.9%
Taylor expanded in a around inf 30.9%
log1p-def42.6%
Simplified42.6%
if -4.19999999999999996e-84 < b < 8.2000000000000001e-84Initial program 99.0%
Taylor expanded in b around 0 99.0%
log1p-def99.2%
Simplified99.2%
Final simplification72.3%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (log1p (exp a)))
assert(a < b);
double code(double a, double b) {
return log1p(exp(a));
}
assert a < b;
public static double code(double a, double b) {
return Math.log1p(Math.exp(a));
}
[a, b] = sort([a, b]) def code(a, b): return math.log1p(math.exp(a))
a, b = sort([a, b]) function code(a, b) return log1p(exp(a)) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{log1p}\left(e^{a}\right)
\end{array}
Initial program 85.9%
Taylor expanded in b around 0 66.5%
log1p-def66.7%
Simplified66.7%
Final simplification66.7%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -1.4) (* b 0.5) (+ (log (+ b 2.0)) (* a 0.5))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -1.4) {
tmp = b * 0.5;
} else {
tmp = log((b + 2.0)) + (a * 0.5);
}
return tmp;
}
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
real(8) :: tmp
if (a <= (-1.4d0)) then
tmp = b * 0.5d0
else
tmp = log((b + 2.0d0)) + (a * 0.5d0)
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b) {
double tmp;
if (a <= -1.4) {
tmp = b * 0.5;
} else {
tmp = Math.log((b + 2.0)) + (a * 0.5);
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if a <= -1.4: tmp = b * 0.5 else: tmp = math.log((b + 2.0)) + (a * 0.5) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -1.4) tmp = Float64(b * 0.5); else tmp = Float64(log(Float64(b + 2.0)) + Float64(a * 0.5)); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
tmp = 0.0;
if (a <= -1.4)
tmp = b * 0.5;
else
tmp = log((b + 2.0)) + (a * 0.5);
end
tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[LessEqual[a, -1.4], N[(b * 0.5), $MachinePrecision], N[(N[Log[N[(b + 2.0), $MachinePrecision]], $MachinePrecision] + N[(a * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1.4:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log \left(b + 2\right) + a \cdot 0.5\\
\end{array}
\end{array}
if a < -1.3999999999999999Initial program 68.6%
Taylor expanded in a around 0 3.7%
log1p-def3.7%
Simplified3.7%
Taylor expanded in b around 0 4.0%
*-commutative4.0%
Simplified4.0%
Taylor expanded in b around inf 9.6%
*-commutative9.6%
Simplified9.6%
if -1.3999999999999999 < a Initial program 91.4%
Taylor expanded in b around 0 66.0%
associate-+r+66.0%
+-commutative66.0%
Simplified66.0%
Taylor expanded in a around 0 64.9%
+-commutative64.9%
+-commutative64.9%
Simplified64.9%
Taylor expanded in b around 0 64.9%
*-commutative64.9%
Simplified64.9%
Final simplification51.5%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -1.0) (* b 0.5) (log (+ b (+ a 2.0)))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -1.0) {
tmp = b * 0.5;
} else {
tmp = log((b + (a + 2.0)));
}
return tmp;
}
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
real(8) :: tmp
if (a <= (-1.0d0)) then
tmp = b * 0.5d0
else
tmp = log((b + (a + 2.0d0)))
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b) {
double tmp;
if (a <= -1.0) {
tmp = b * 0.5;
} else {
tmp = Math.log((b + (a + 2.0)));
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if a <= -1.0: tmp = b * 0.5 else: tmp = math.log((b + (a + 2.0))) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -1.0) tmp = Float64(b * 0.5); else tmp = log(Float64(b + Float64(a + 2.0))); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
tmp = 0.0;
if (a <= -1.0)
tmp = b * 0.5;
else
tmp = log((b + (a + 2.0)));
end
tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[LessEqual[a, -1.0], N[(b * 0.5), $MachinePrecision], N[Log[N[(b + N[(a + 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log \left(b + \left(a + 2\right)\right)\\
\end{array}
\end{array}
if a < -1Initial program 68.6%
Taylor expanded in a around 0 3.7%
log1p-def3.7%
Simplified3.7%
Taylor expanded in b around 0 4.0%
*-commutative4.0%
Simplified4.0%
Taylor expanded in b around inf 9.6%
*-commutative9.6%
Simplified9.6%
if -1 < a Initial program 91.4%
Taylor expanded in b around 0 66.0%
associate-+r+66.0%
+-commutative66.0%
Simplified66.0%
Taylor expanded in a around 0 64.8%
+-commutative64.8%
+-commutative64.8%
associate-+l+64.8%
Simplified64.8%
Final simplification51.4%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -1.0) (* b 0.5) (log (+ a 2.0))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -1.0) {
tmp = b * 0.5;
} else {
tmp = log((a + 2.0));
}
return tmp;
}
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
real(8) :: tmp
if (a <= (-1.0d0)) then
tmp = b * 0.5d0
else
tmp = log((a + 2.0d0))
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b) {
double tmp;
if (a <= -1.0) {
tmp = b * 0.5;
} else {
tmp = Math.log((a + 2.0));
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if a <= -1.0: tmp = b * 0.5 else: tmp = math.log((a + 2.0)) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -1.0) tmp = Float64(b * 0.5); else tmp = log(Float64(a + 2.0)); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
tmp = 0.0;
if (a <= -1.0)
tmp = b * 0.5;
else
tmp = log((a + 2.0));
end
tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[LessEqual[a, -1.0], N[(b * 0.5), $MachinePrecision], N[Log[N[(a + 2.0), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -1:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log \left(a + 2\right)\\
\end{array}
\end{array}
if a < -1Initial program 68.6%
Taylor expanded in a around 0 3.7%
log1p-def3.7%
Simplified3.7%
Taylor expanded in b around 0 4.0%
*-commutative4.0%
Simplified4.0%
Taylor expanded in b around inf 9.6%
*-commutative9.6%
Simplified9.6%
if -1 < a Initial program 91.4%
Taylor expanded in b around 0 66.0%
associate-+r+66.0%
+-commutative66.0%
Simplified66.0%
Taylor expanded in a around 0 64.8%
+-commutative64.8%
+-commutative64.8%
associate-+l+64.8%
Simplified64.8%
Taylor expanded in b around 0 65.4%
+-commutative65.4%
Simplified65.4%
Final simplification51.8%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -80.0) (* b 0.5) (log 2.0)))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -80.0) {
tmp = b * 0.5;
} else {
tmp = log(2.0);
}
return tmp;
}
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
real(8) :: tmp
if (a <= (-80.0d0)) then
tmp = b * 0.5d0
else
tmp = log(2.0d0)
end if
code = tmp
end function
assert a < b;
public static double code(double a, double b) {
double tmp;
if (a <= -80.0) {
tmp = b * 0.5;
} else {
tmp = Math.log(2.0);
}
return tmp;
}
[a, b] = sort([a, b]) def code(a, b): tmp = 0 if a <= -80.0: tmp = b * 0.5 else: tmp = math.log(2.0) return tmp
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -80.0) tmp = Float64(b * 0.5); else tmp = log(2.0); end return tmp end
a, b = num2cell(sort([a, b])){:}
function tmp_2 = code(a, b)
tmp = 0.0;
if (a <= -80.0)
tmp = b * 0.5;
else
tmp = log(2.0);
end
tmp_2 = tmp;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := If[LessEqual[a, -80.0], N[(b * 0.5), $MachinePrecision], N[Log[2.0], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -80:\\
\;\;\;\;b \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\log 2\\
\end{array}
\end{array}
if a < -80Initial program 68.7%
Taylor expanded in a around 0 3.6%
log1p-def3.6%
Simplified3.6%
Taylor expanded in b around 0 3.9%
*-commutative3.9%
Simplified3.9%
Taylor expanded in b around inf 9.7%
*-commutative9.7%
Simplified9.7%
if -80 < a Initial program 91.2%
Taylor expanded in b around 0 66.5%
log1p-def66.7%
Simplified66.7%
Taylor expanded in a around 0 64.2%
Final simplification51.2%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* b 0.5))
assert(a < b);
double code(double a, double b) {
return 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 = b * 0.5d0
end function
assert a < b;
public static double code(double a, double b) {
return b * 0.5;
}
[a, b] = sort([a, b]) def code(a, b): return b * 0.5
a, b = sort([a, b]) function code(a, b) return Float64(b * 0.5) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = b * 0.5;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(b * 0.5), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
b \cdot 0.5
\end{array}
Initial program 85.9%
Taylor expanded in a around 0 67.5%
log1p-def67.5%
Simplified67.5%
Taylor expanded in b around 0 50.0%
*-commutative50.0%
Simplified50.0%
Taylor expanded in b around inf 4.8%
*-commutative4.8%
Simplified4.8%
Final simplification4.8%
herbie shell --seed 2024018
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))