
(FPCore (x y z) :precision binary64 (- (+ (- x (* (+ y 0.5) (log y))) y) z))
double code(double x, double y, double z) {
return ((x - ((y + 0.5) * log(y))) + y) - z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((x - ((y + 0.5d0) * log(y))) + y) - z
end function
public static double code(double x, double y, double z) {
return ((x - ((y + 0.5) * Math.log(y))) + y) - z;
}
def code(x, y, z): return ((x - ((y + 0.5) * math.log(y))) + y) - z
function code(x, y, z) return Float64(Float64(Float64(x - Float64(Float64(y + 0.5) * log(y))) + y) - z) end
function tmp = code(x, y, z) tmp = ((x - ((y + 0.5) * log(y))) + y) - z; end
code[x_, y_, z_] := N[(N[(N[(x - N[(N[(y + 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (- (+ (- x (* (+ y 0.5) (log y))) y) z))
double code(double x, double y, double z) {
return ((x - ((y + 0.5) * log(y))) + y) - z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((x - ((y + 0.5d0) * log(y))) + y) - z
end function
public static double code(double x, double y, double z) {
return ((x - ((y + 0.5) * Math.log(y))) + y) - z;
}
def code(x, y, z): return ((x - ((y + 0.5) * math.log(y))) + y) - z
function code(x, y, z) return Float64(Float64(Float64(x - Float64(Float64(y + 0.5) * log(y))) + y) - z) end
function tmp = code(x, y, z) tmp = ((x - ((y + 0.5) * log(y))) + y) - z; end
code[x_, y_, z_] := N[(N[(N[(x - N[(N[(y + 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z
\end{array}
(FPCore (x y z) :precision binary64 (- (+ (fma (+ 0.5 y) (- (log y)) x) y) z))
double code(double x, double y, double z) {
return (fma((0.5 + y), -log(y), x) + y) - z;
}
function code(x, y, z) return Float64(Float64(fma(Float64(0.5 + y), Float64(-log(y)), x) + y) - z) end
code[x_, y_, z_] := N[(N[(N[(N[(0.5 + y), $MachinePrecision] * (-N[Log[y], $MachinePrecision]) + x), $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
\\
\left(\mathsf{fma}\left(0.5 + y, -\log y, x\right) + y\right) - z
\end{array}
Initial program 99.8%
lift--.f64N/A
lift-*.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-neg.f6499.8
Applied rewrites99.8%
(FPCore (x y z) :precision binary64 (if (<= (+ (- x (* (+ y 0.5) (log y))) y) -4000000000.0) (- (- x (- (* (log y) y) y)) z) (- (fma -0.5 (log y) x) z)))
double code(double x, double y, double z) {
double tmp;
if (((x - ((y + 0.5) * log(y))) + y) <= -4000000000.0) {
tmp = (x - ((log(y) * y) - y)) - z;
} else {
tmp = fma(-0.5, log(y), x) - z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(Float64(x - Float64(Float64(y + 0.5) * log(y))) + y) <= -4000000000.0) tmp = Float64(Float64(x - Float64(Float64(log(y) * y) - y)) - z); else tmp = Float64(fma(-0.5, log(y), x) - z); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(N[(x - N[(N[(y + 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision], -4000000000.0], N[(N[(x - N[(N[(N[Log[y], $MachinePrecision] * y), $MachinePrecision] - y), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision], N[(N[(-0.5 * N[Log[y], $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x - \left(y + 0.5\right) \cdot \log y\right) + y \leq -4000000000:\\
\;\;\;\;\left(x - \left(\log y \cdot y - y\right)\right) - z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-0.5, \log y, x\right) - z\\
\end{array}
\end{array}
if (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) < -4e9Initial program 99.7%
Taylor expanded in y around inf
*-commutativeN/A
associate-*r*N/A
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-*.f64N/A
lower-log.f6499.1
Applied rewrites99.1%
lift-+.f64N/A
lift--.f64N/A
associate-+l-N/A
lower--.f64N/A
lower--.f6499.2
Applied rewrites99.2%
if -4e9 < (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (<= (+ (- x (* (+ y 0.5) (log y))) y) -4000000000.0) (- (+ (- x (* (log y) y)) y) z) (- (fma -0.5 (log y) x) z)))
double code(double x, double y, double z) {
double tmp;
if (((x - ((y + 0.5) * log(y))) + y) <= -4000000000.0) {
tmp = ((x - (log(y) * y)) + y) - z;
} else {
tmp = fma(-0.5, log(y), x) - z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(Float64(x - Float64(Float64(y + 0.5) * log(y))) + y) <= -4000000000.0) tmp = Float64(Float64(Float64(x - Float64(log(y) * y)) + y) - z); else tmp = Float64(fma(-0.5, log(y), x) - z); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(N[(x - N[(N[(y + 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision], -4000000000.0], N[(N[(N[(x - N[(N[Log[y], $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision], N[(N[(-0.5 * N[Log[y], $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(x - \left(y + 0.5\right) \cdot \log y\right) + y \leq -4000000000:\\
\;\;\;\;\left(\left(x - \log y \cdot y\right) + y\right) - z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-0.5, \log y, x\right) - z\\
\end{array}
\end{array}
if (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) < -4e9Initial program 99.7%
Taylor expanded in y around inf
*-commutativeN/A
associate-*r*N/A
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-*.f64N/A
lower-log.f6499.1
Applied rewrites99.1%
if -4e9 < (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.05e+79) (not (<= x 4.6e+103))) (- (fma -0.5 (log y) x) z) (- (fma (- (- y) 0.5) (log y) y) z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.05e+79) || !(x <= 4.6e+103)) {
tmp = fma(-0.5, log(y), x) - z;
} else {
tmp = fma((-y - 0.5), log(y), y) - z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -1.05e+79) || !(x <= 4.6e+103)) tmp = Float64(fma(-0.5, log(y), x) - z); else tmp = Float64(fma(Float64(Float64(-y) - 0.5), log(y), y) - z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.05e+79], N[Not[LessEqual[x, 4.6e+103]], $MachinePrecision]], N[(N[(-0.5 * N[Log[y], $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision], N[(N[(N[((-y) - 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.05 \cdot 10^{+79} \lor \neg \left(x \leq 4.6 \cdot 10^{+103}\right):\\
\;\;\;\;\mathsf{fma}\left(-0.5, \log y, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(-y\right) - 0.5, \log y, y\right) - z\\
\end{array}
\end{array}
if x < -1.05000000000000004e79 or 4.60000000000000017e103 < x Initial program 99.9%
Taylor expanded in y around 0
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f6485.9
Applied rewrites85.9%
if -1.05000000000000004e79 < x < 4.60000000000000017e103Initial program 99.8%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
+-commutativeN/A
distribute-lft-neg-inN/A
distribute-rgt-inN/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
distribute-lft-neg-inN/A
fp-cancel-sub-sign-invN/A
associate-*r*N/A
distribute-rgt-out--N/A
*-commutativeN/A
lower-fma.f64N/A
mul-1-negN/A
lower--.f64N/A
lower-neg.f64N/A
lower-log.f6494.9
Applied rewrites94.9%
Final simplification91.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.05e+79) (not (<= x 4.6e+103))) (- (fma -0.5 (log y) x) z) (- y (fma (+ 0.5 y) (log y) z))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.05e+79) || !(x <= 4.6e+103)) {
tmp = fma(-0.5, log(y), x) - z;
} else {
tmp = y - fma((0.5 + y), log(y), z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -1.05e+79) || !(x <= 4.6e+103)) tmp = Float64(fma(-0.5, log(y), x) - z); else tmp = Float64(y - fma(Float64(0.5 + y), log(y), z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.05e+79], N[Not[LessEqual[x, 4.6e+103]], $MachinePrecision]], N[(N[(-0.5 * N[Log[y], $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision], N[(y - N[(N[(0.5 + y), $MachinePrecision] * N[Log[y], $MachinePrecision] + z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.05 \cdot 10^{+79} \lor \neg \left(x \leq 4.6 \cdot 10^{+103}\right):\\
\;\;\;\;\mathsf{fma}\left(-0.5, \log y, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;y - \mathsf{fma}\left(0.5 + y, \log y, z\right)\\
\end{array}
\end{array}
if x < -1.05000000000000004e79 or 4.60000000000000017e103 < x Initial program 99.9%
Taylor expanded in y around 0
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f6485.9
Applied rewrites85.9%
if -1.05000000000000004e79 < x < 4.60000000000000017e103Initial program 99.8%
Taylor expanded in x around 0
lower--.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-+.f64N/A
lower-log.f6494.8
Applied rewrites94.8%
Final simplification91.7%
(FPCore (x y z) :precision binary64 (if (<= y 70000.0) (- (fma -0.5 (log y) x) z) (- (fma (- y) (log y) y) z)))
double code(double x, double y, double z) {
double tmp;
if (y <= 70000.0) {
tmp = fma(-0.5, log(y), x) - z;
} else {
tmp = fma(-y, log(y), y) - z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 70000.0) tmp = Float64(fma(-0.5, log(y), x) - z); else tmp = Float64(fma(Float64(-y), log(y), y) - z); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 70000.0], N[(N[(-0.5 * N[Log[y], $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision], N[(N[((-y) * N[Log[y], $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 70000:\\
\;\;\;\;\mathsf{fma}\left(-0.5, \log y, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-y, \log y, y\right) - z\\
\end{array}
\end{array}
if y < 7e4Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f64100.0
Applied rewrites100.0%
if 7e4 < y Initial program 99.7%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
+-commutativeN/A
distribute-lft-neg-inN/A
distribute-rgt-inN/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
distribute-lft-neg-inN/A
fp-cancel-sub-sign-invN/A
associate-*r*N/A
distribute-rgt-out--N/A
*-commutativeN/A
lower-fma.f64N/A
mul-1-negN/A
lower--.f64N/A
lower-neg.f64N/A
lower-log.f6480.1
Applied rewrites80.1%
Taylor expanded in y around inf
Applied rewrites79.4%
(FPCore (x y z) :precision binary64 (if (<= y 1.25e+62) (- (fma -0.5 (log y) x) z) (* (- 1.0 (log y)) y)))
double code(double x, double y, double z) {
double tmp;
if (y <= 1.25e+62) {
tmp = fma(-0.5, log(y), x) - z;
} else {
tmp = (1.0 - log(y)) * y;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 1.25e+62) tmp = Float64(fma(-0.5, log(y), x) - z); else tmp = Float64(Float64(1.0 - log(y)) * y); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 1.25e+62], N[(N[(-0.5 * N[Log[y], $MachinePrecision] + x), $MachinePrecision] - z), $MachinePrecision], N[(N[(1.0 - N[Log[y], $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.25 \cdot 10^{+62}:\\
\;\;\;\;\mathsf{fma}\left(-0.5, \log y, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;\left(1 - \log y\right) \cdot y\\
\end{array}
\end{array}
if y < 1.25000000000000007e62Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f6495.1
Applied rewrites95.1%
if 1.25000000000000007e62 < y Initial program 99.7%
Taylor expanded in y around inf
*-commutativeN/A
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-*.f64N/A
lower--.f64N/A
lower-log.f6464.0
Applied rewrites64.0%
(FPCore (x y z) :precision binary64 (- y (- (fma (+ 0.5 y) (log y) z) x)))
double code(double x, double y, double z) {
return y - (fma((0.5 + y), log(y), z) - x);
}
function code(x, y, z) return Float64(y - Float64(fma(Float64(0.5 + y), log(y), z) - x)) end
code[x_, y_, z_] := N[(y - N[(N[(N[(0.5 + y), $MachinePrecision] * N[Log[y], $MachinePrecision] + z), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y - \left(\mathsf{fma}\left(0.5 + y, \log y, z\right) - x\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0
associate--l+N/A
+-commutativeN/A
associate-+l-N/A
lower--.f64N/A
lower--.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-+.f64N/A
lower-log.f6499.8
Applied rewrites99.8%
(FPCore (x y z) :precision binary64 (if (<= y 8e+61) (- (fma (log y) 0.5 z)) (* (- 1.0 (log y)) y)))
double code(double x, double y, double z) {
double tmp;
if (y <= 8e+61) {
tmp = -fma(log(y), 0.5, z);
} else {
tmp = (1.0 - log(y)) * y;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 8e+61) tmp = Float64(-fma(log(y), 0.5, z)); else tmp = Float64(Float64(1.0 - log(y)) * y); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 8e+61], (-N[(N[Log[y], $MachinePrecision] * 0.5 + z), $MachinePrecision]), N[(N[(1.0 - N[Log[y], $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 8 \cdot 10^{+61}:\\
\;\;\;\;-\mathsf{fma}\left(\log y, 0.5, z\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - \log y\right) \cdot y\\
\end{array}
\end{array}
if y < 7.9999999999999996e61Initial program 100.0%
Taylor expanded in x around 0
lower--.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-+.f64N/A
lower-log.f6465.0
Applied rewrites65.0%
Taylor expanded in y around 0
Applied rewrites60.1%
if 7.9999999999999996e61 < y Initial program 99.7%
Taylor expanded in y around inf
*-commutativeN/A
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-*.f64N/A
lower--.f64N/A
lower-log.f6464.0
Applied rewrites64.0%
(FPCore (x y z) :precision binary64 (- (fma (log y) 0.5 z)))
double code(double x, double y, double z) {
return -fma(log(y), 0.5, z);
}
function code(x, y, z) return Float64(-fma(log(y), 0.5, z)) end
code[x_, y_, z_] := (-N[(N[Log[y], $MachinePrecision] * 0.5 + z), $MachinePrecision])
\begin{array}{l}
\\
-\mathsf{fma}\left(\log y, 0.5, z\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0
lower--.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-+.f64N/A
lower-log.f6471.6
Applied rewrites71.6%
Taylor expanded in y around 0
Applied rewrites42.2%
(FPCore (x y z) :precision binary64 (- z))
double code(double x, double y, double z) {
return -z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = -z
end function
public static double code(double x, double y, double z) {
return -z;
}
def code(x, y, z): return -z
function code(x, y, z) return Float64(-z) end
function tmp = code(x, y, z) tmp = -z; end
code[x_, y_, z_] := (-z)
\begin{array}{l}
\\
-z
\end{array}
Initial program 99.8%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6429.8
Applied rewrites29.8%
(FPCore (x y z) :precision binary64 (- (- (+ y x) z) (* (+ y 0.5) (log y))))
double code(double x, double y, double z) {
return ((y + x) - z) - ((y + 0.5) * log(y));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((y + x) - z) - ((y + 0.5d0) * log(y))
end function
public static double code(double x, double y, double z) {
return ((y + x) - z) - ((y + 0.5) * Math.log(y));
}
def code(x, y, z): return ((y + x) - z) - ((y + 0.5) * math.log(y))
function code(x, y, z) return Float64(Float64(Float64(y + x) - z) - Float64(Float64(y + 0.5) * log(y))) end
function tmp = code(x, y, z) tmp = ((y + x) - z) - ((y + 0.5) * log(y)); end
code[x_, y_, z_] := N[(N[(N[(y + x), $MachinePrecision] - z), $MachinePrecision] - N[(N[(y + 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(y + x\right) - z\right) - \left(y + 0.5\right) \cdot \log y
\end{array}
herbie shell --seed 2024326
(FPCore (x y z)
:name "Numeric.SpecFunctions:stirlingError from math-functions-0.1.5.2"
:precision binary64
:alt
(! :herbie-platform default (- (- (+ y x) z) (* (+ y 1/2) (log y))))
(- (+ (- x (* (+ y 0.5) (log y))) y) z))