
(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 9 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) 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}
if (exp.f64 a) < 1Initial program 55.5%
Taylor expanded in b around 0 78.0%
log1p-define78.0%
Simplified78.0%
if 1 < (exp.f64 a) Initial program 69.8%
Taylor expanded in a around 0 20.8%
log1p-define20.8%
Simplified20.8%
Final simplification75.5%
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}
if (exp.f64 a) < 1Initial program 55.5%
Taylor expanded in b around 0 54.2%
associate-+r+54.2%
*-commutative54.2%
Simplified54.2%
Taylor expanded in b around 0 53.4%
associate-+l+53.4%
log1p-define76.9%
Applied egg-rr76.9%
if 1 < (exp.f64 a) Initial program 69.8%
Taylor expanded in a around 0 20.8%
log1p-define20.8%
Simplified20.8%
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}
if b < 3.5000000000000001e-84Initial program 54.5%
Taylor expanded in b around 0 52.3%
log1p-define52.3%
Simplified52.3%
if 3.5000000000000001e-84 < b Initial program 73.1%
Taylor expanded in a around 0 68.6%
log1p-define68.6%
Simplified68.6%
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}
if b < 1e-83Initial program 54.5%
Taylor expanded in b around 0 52.3%
log1p-define52.3%
Simplified52.3%
if 1e-83 < b Initial program 73.1%
Taylor expanded in a around 0 68.6%
Taylor expanded in b around 0 64.4%
*-commutative64.4%
Simplified64.4%
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}
Initial program 56.2%
Taylor expanded in a around 0 52.6%
Taylor expanded in b around 0 51.4%
*-commutative51.4%
Simplified51.4%
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}
Initial program 56.2%
Taylor expanded in b around 0 53.2%
log1p-define53.2%
Simplified53.2%
Taylor expanded in a around 0 52.0%
*-commutative52.0%
Simplified52.0%
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}
Initial program 56.2%
Taylor expanded in a around 0 52.6%
Taylor expanded in b around 0 51.1%
+-commutative51.1%
Simplified51.1%
Final simplification51.1%
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}
Initial program 56.2%
Taylor expanded in a around 0 52.6%
Taylor expanded in b around 0 50.5%
+-commutative50.5%
Simplified50.5%
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}
Initial program 56.2%
Taylor expanded in b around 0 53.2%
log1p-define53.2%
Simplified53.2%
Taylor expanded in a around 0 50.6%
herbie shell --seed 2024090
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))