
(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 (+ (log1p (exp a)) b))
assert(a < b);
double code(double a, double b) {
return log1p(exp(a)) + b;
}
assert a < b;
public static double code(double a, double b) {
return Math.log1p(Math.exp(a)) + b;
}
[a, b] = sort([a, b]) def code(a, b): return math.log1p(math.exp(a)) + b
a, b = sort([a, b]) function code(a, b) return Float64(log1p(exp(a)) + b) end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision] + b), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{log1p}\left(e^{a}\right) + b
\end{array}
Initial program 52.2%
Taylor expanded in b around 0
*-rgt-identityN/A
associate-*r/N/A
+-commutativeN/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.f6471.7
Applied rewrites71.7%
Applied rewrites71.7%
Applied rewrites71.9%
Taylor expanded in a around inf
Applied rewrites71.9%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= (log (+ (exp a) (exp b))) 0.5) (* (fma 0.125 b 0.5) b) (+ (log 2.0) b)))
assert(a < b);
double code(double a, double b) {
double tmp;
if (log((exp(a) + exp(b))) <= 0.5) {
tmp = fma(0.125, b, 0.5) * b;
} else {
tmp = log(2.0) + b;
}
return tmp;
}
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (log(Float64(exp(a) + exp(b))) <= 0.5) tmp = Float64(fma(0.125, b, 0.5) * b); else tmp = Float64(log(2.0) + 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[Log[N[(N[Exp[a], $MachinePrecision] + N[Exp[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.5], N[(N[(0.125 * b + 0.5), $MachinePrecision] * b), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + b), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;\log \left(e^{a} + e^{b}\right) \leq 0.5:\\
\;\;\;\;\mathsf{fma}\left(0.125, b, 0.5\right) \cdot b\\
\mathbf{else}:\\
\;\;\;\;\log 2 + b\\
\end{array}
\end{array}
if (log.f64 (+.f64 (exp.f64 a) (exp.f64 b))) < 0.5Initial program 9.5%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f645.6
Applied rewrites5.6%
Taylor expanded in b around 0
Applied rewrites2.9%
Taylor expanded in b around inf
Applied rewrites9.8%
if 0.5 < (log.f64 (+.f64 (exp.f64 a) (exp.f64 b))) Initial program 97.6%
Taylor expanded in b around 0
*-rgt-identityN/A
associate-*r/N/A
+-commutativeN/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.f6497.4
Applied rewrites97.4%
Applied rewrites97.3%
Applied rewrites97.7%
Taylor expanded in a around 0
Applied rewrites95.2%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -640.0) (* (fma 0.125 b 0.5) b) (log1p (exp a))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -640.0) {
tmp = fma(0.125, b, 0.5) * b;
} else {
tmp = log1p(exp(a));
}
return tmp;
}
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -640.0) tmp = Float64(fma(0.125, b, 0.5) * b); 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[LessEqual[a, -640.0], N[(N[(0.125 * b + 0.5), $MachinePrecision] * b), $MachinePrecision], N[Log[1 + N[Exp[a], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -640:\\
\;\;\;\;\mathsf{fma}\left(0.125, b, 0.5\right) \cdot b\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(e^{a}\right)\\
\end{array}
\end{array}
if a < -640Initial program 8.8%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f643.8
Applied rewrites3.8%
Taylor expanded in b around 0
Applied rewrites3.9%
Taylor expanded in b around inf
Applied rewrites18.4%
if -640 < a Initial program 66.3%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6463.3
Applied rewrites63.3%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -2.6) (* (fma 0.125 b 0.5) b) (fma (fma (fma (* a a) -0.005208333333333333 0.125) a 0.5) a (log 2.0))))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -2.6) {
tmp = fma(0.125, b, 0.5) * b;
} else {
tmp = fma(fma(fma((a * a), -0.005208333333333333, 0.125), a, 0.5), a, log(2.0));
}
return tmp;
}
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -2.6) tmp = Float64(fma(0.125, b, 0.5) * b); else tmp = fma(fma(fma(Float64(a * a), -0.005208333333333333, 0.125), a, 0.5), a, 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[a, -2.6], N[(N[(0.125 * b + 0.5), $MachinePrecision] * b), $MachinePrecision], N[(N[(N[(N[(a * a), $MachinePrecision] * -0.005208333333333333 + 0.125), $MachinePrecision] * a + 0.5), $MachinePrecision] * a + N[Log[2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2.6:\\
\;\;\;\;\mathsf{fma}\left(0.125, b, 0.5\right) \cdot b\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(a \cdot a, -0.005208333333333333, 0.125\right), a, 0.5\right), a, \log 2\right)\\
\end{array}
\end{array}
if a < -2.60000000000000009Initial program 8.8%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f643.8
Applied rewrites3.8%
Taylor expanded in b around 0
Applied rewrites3.9%
Taylor expanded in b around inf
Applied rewrites18.4%
if -2.60000000000000009 < a Initial program 66.3%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6463.3
Applied rewrites63.3%
Taylor expanded in a around 0
Applied rewrites62.7%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (if (<= a -120.0) (* (fma 0.125 b 0.5) b) (log1p 1.0)))
assert(a < b);
double code(double a, double b) {
double tmp;
if (a <= -120.0) {
tmp = fma(0.125, b, 0.5) * b;
} else {
tmp = log1p(1.0);
}
return tmp;
}
a, b = sort([a, b]) function code(a, b) tmp = 0.0 if (a <= -120.0) tmp = Float64(fma(0.125, b, 0.5) * b); else tmp = log1p(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[a, -120.0], N[(N[(0.125 * b + 0.5), $MachinePrecision] * b), $MachinePrecision], N[Log[1 + 1.0], $MachinePrecision]]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\begin{array}{l}
\mathbf{if}\;a \leq -120:\\
\;\;\;\;\mathsf{fma}\left(0.125, b, 0.5\right) \cdot b\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(1\right)\\
\end{array}
\end{array}
if a < -120Initial program 8.8%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f643.8
Applied rewrites3.8%
Taylor expanded in b around 0
Applied rewrites3.9%
Taylor expanded in b around inf
Applied rewrites18.4%
if -120 < a Initial program 66.3%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6463.3
Applied rewrites63.3%
Taylor expanded in a around 0
Applied rewrites62.0%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* (fma 0.125 b 0.5) b))
assert(a < b);
double code(double a, double b) {
return fma(0.125, b, 0.5) * b;
}
a, b = sort([a, b]) function code(a, b) return Float64(fma(0.125, b, 0.5) * b) 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), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\mathsf{fma}\left(0.125, b, 0.5\right) \cdot b
\end{array}
Initial program 52.2%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6449.2
Applied rewrites49.2%
Taylor expanded in b around 0
Applied rewrites47.3%
Taylor expanded in b around inf
Applied rewrites6.9%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* (* b b) 0.125))
assert(a < b);
double code(double a, double b) {
return (b * 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 = (b * b) * 0.125d0
end function
assert a < b;
public static double code(double a, double b) {
return (b * b) * 0.125;
}
[a, b] = sort([a, b]) def code(a, b): return (b * b) * 0.125
a, b = sort([a, b]) function code(a, b) return Float64(Float64(b * b) * 0.125) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = (b * b) * 0.125;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(N[(b * b), $MachinePrecision] * 0.125), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
\left(b \cdot b\right) \cdot 0.125
\end{array}
Initial program 52.2%
Taylor expanded in a around 0
lower-log1p.f64N/A
lower-exp.f6449.2
Applied rewrites49.2%
Taylor expanded in b around 0
Applied rewrites47.3%
Taylor expanded in b around inf
Applied rewrites4.0%
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 52.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6449.0
Applied rewrites49.0%
Taylor expanded in a around 0
Applied rewrites47.8%
Taylor expanded in a around inf
Applied rewrites4.3%
NOTE: a and b should be sorted in increasing order before calling this function. (FPCore (a b) :precision binary64 (* 0.5 a))
assert(a < b);
double code(double a, double b) {
return 0.5 * a;
}
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 = 0.5d0 * a
end function
assert a < b;
public static double code(double a, double b) {
return 0.5 * a;
}
[a, b] = sort([a, b]) def code(a, b): return 0.5 * a
a, b = sort([a, b]) function code(a, b) return Float64(0.5 * a) end
a, b = num2cell(sort([a, b])){:}
function tmp = code(a, b)
tmp = 0.5 * a;
end
NOTE: a and b should be sorted in increasing order before calling this function. code[a_, b_] := N[(0.5 * a), $MachinePrecision]
\begin{array}{l}
[a, b] = \mathsf{sort}([a, b])\\
\\
0.5 \cdot a
\end{array}
Initial program 52.2%
Taylor expanded in b around 0
lower-log1p.f64N/A
lower-exp.f6449.0
Applied rewrites49.0%
Taylor expanded in a around 0
Applied rewrites47.8%
Taylor expanded in a around inf
Applied rewrites7.6%
herbie shell --seed 2024332
(FPCore (a b)
:name "symmetry log of sum of exp"
:precision binary64
(log (+ (exp a) (exp b))))