
(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 9 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 (- (+ x (fma (log y) (- -0.5 y) y)) z))
double code(double x, double y, double z) {
return (x + fma(log(y), (-0.5 - y), y)) - z;
}
function code(x, y, z) return Float64(Float64(x + fma(log(y), Float64(-0.5 - y), y)) - z) end
code[x_, y_, z_] := N[(N[(x + N[(N[Log[y], $MachinePrecision] * N[(-0.5 - y), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]
\begin{array}{l}
\\
\left(x + \mathsf{fma}\left(\log y, -0.5 - y, y\right)\right) - z
\end{array}
Initial program 99.9%
lift-+.f64N/A
lift--.f64N/A
sub-negN/A
associate-+l+N/A
lower-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-neg-inN/A
unsub-negN/A
lower--.f64N/A
metadata-eval99.9
Applied rewrites99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ y (- x (* (log y) (+ y 0.5))))))
(if (<= t_0 -1e+44)
(- y (* y (log y)))
(if (<= t_0 1e+134) (- (* (log y) -0.5) z) (fma (log y) -0.5 x)))))
double code(double x, double y, double z) {
double t_0 = y + (x - (log(y) * (y + 0.5)));
double tmp;
if (t_0 <= -1e+44) {
tmp = y - (y * log(y));
} else if (t_0 <= 1e+134) {
tmp = (log(y) * -0.5) - z;
} else {
tmp = fma(log(y), -0.5, x);
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y + Float64(x - Float64(log(y) * Float64(y + 0.5)))) tmp = 0.0 if (t_0 <= -1e+44) tmp = Float64(y - Float64(y * log(y))); elseif (t_0 <= 1e+134) tmp = Float64(Float64(log(y) * -0.5) - z); else tmp = fma(log(y), -0.5, x); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(y + N[(x - N[(N[Log[y], $MachinePrecision] * N[(y + 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -1e+44], N[(y - N[(y * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 1e+134], N[(N[(N[Log[y], $MachinePrecision] * -0.5), $MachinePrecision] - z), $MachinePrecision], N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y + \left(x - \log y \cdot \left(y + 0.5\right)\right)\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{+44}:\\
\;\;\;\;y - y \cdot \log y\\
\mathbf{elif}\;t\_0 \leq 10^{+134}:\\
\;\;\;\;\log y \cdot -0.5 - z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) < -1.0000000000000001e44Initial program 99.7%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
lift-+.f64N/A
lower-/.f6499.5
Applied rewrites99.5%
Taylor expanded in y around inf
cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
distribute-rgt-inN/A
*-lft-identityN/A
cancel-sign-subN/A
log-recN/A
remove-double-negN/A
*-commutativeN/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f6462.9
Applied rewrites62.9%
if -1.0000000000000001e44 < (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) < 9.99999999999999921e133Initial program 100.0%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
lift-+.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-log.f6495.2
Applied rewrites95.2%
Taylor expanded in x around 0
Applied rewrites88.8%
if 9.99999999999999921e133 < (+.f64 (-.f64 x (*.f64 (+.f64 y #s(literal 1/2 binary64)) (log.f64 y))) y) Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
associate--l+N/A
lower-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-log.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6495.4
Applied rewrites95.4%
Taylor expanded in y around 0
Applied rewrites94.6%
Final simplification76.3%
(FPCore (x y z) :precision binary64 (if (<= y 1.75e-10) (fma (log y) -0.5 x) (if (<= y 11000000000.0) (- z) (- y (* y (log y))))))
double code(double x, double y, double z) {
double tmp;
if (y <= 1.75e-10) {
tmp = fma(log(y), -0.5, x);
} else if (y <= 11000000000.0) {
tmp = -z;
} else {
tmp = y - (y * log(y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 1.75e-10) tmp = fma(log(y), -0.5, x); elseif (y <= 11000000000.0) tmp = Float64(-z); else tmp = Float64(y - Float64(y * log(y))); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 1.75e-10], N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision], If[LessEqual[y, 11000000000.0], (-z), N[(y - N[(y * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.75 \cdot 10^{-10}:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right)\\
\mathbf{elif}\;y \leq 11000000000:\\
\;\;\;\;-z\\
\mathbf{else}:\\
\;\;\;\;y - y \cdot \log y\\
\end{array}
\end{array}
if y < 1.7499999999999999e-10Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
associate--l+N/A
lower-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-log.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6470.6
Applied rewrites70.6%
Taylor expanded in y around 0
Applied rewrites70.6%
if 1.7499999999999999e-10 < y < 1.1e10Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6471.8
Applied rewrites71.8%
if 1.1e10 < y Initial program 99.7%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
lift-+.f64N/A
lower-/.f6499.5
Applied rewrites99.5%
Taylor expanded in y around inf
cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
distribute-rgt-inN/A
*-lft-identityN/A
cancel-sign-subN/A
log-recN/A
remove-double-negN/A
*-commutativeN/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f6473.4
Applied rewrites73.4%
(FPCore (x y z) :precision binary64 (if (<= y 1.75e-10) (fma (log y) -0.5 x) (if (<= y 11000000000.0) (- z) (fma (log y) (- y) y))))
double code(double x, double y, double z) {
double tmp;
if (y <= 1.75e-10) {
tmp = fma(log(y), -0.5, x);
} else if (y <= 11000000000.0) {
tmp = -z;
} else {
tmp = fma(log(y), -y, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 1.75e-10) tmp = fma(log(y), -0.5, x); elseif (y <= 11000000000.0) tmp = Float64(-z); else tmp = fma(log(y), Float64(-y), y); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 1.75e-10], N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision], If[LessEqual[y, 11000000000.0], (-z), N[(N[Log[y], $MachinePrecision] * (-y) + y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.75 \cdot 10^{-10}:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right)\\
\mathbf{elif}\;y \leq 11000000000:\\
\;\;\;\;-z\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\log y, -y, y\right)\\
\end{array}
\end{array}
if y < 1.7499999999999999e-10Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
associate--l+N/A
lower-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-log.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6470.6
Applied rewrites70.6%
Taylor expanded in y around 0
Applied rewrites70.6%
if 1.7499999999999999e-10 < y < 1.1e10Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6471.8
Applied rewrites71.8%
if 1.1e10 < y Initial program 99.7%
Taylor expanded in y around inf
sub-negN/A
mul-1-negN/A
remove-double-negN/A
+-commutativeN/A
distribute-rgt-inN/A
log-recN/A
distribute-lft-neg-inN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f64N/A
mul-1-negN/A
lower-neg.f6473.4
Applied rewrites73.4%
(FPCore (x y z) :precision binary64 (if (<= z -6.2e+42) (- z) (if (<= z 1.52e+57) (fma (log y) -0.5 x) (- z))))
double code(double x, double y, double z) {
double tmp;
if (z <= -6.2e+42) {
tmp = -z;
} else if (z <= 1.52e+57) {
tmp = fma(log(y), -0.5, x);
} else {
tmp = -z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= -6.2e+42) tmp = Float64(-z); elseif (z <= 1.52e+57) tmp = fma(log(y), -0.5, x); else tmp = Float64(-z); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, -6.2e+42], (-z), If[LessEqual[z, 1.52e+57], N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision], (-z)]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -6.2 \cdot 10^{+42}:\\
\;\;\;\;-z\\
\mathbf{elif}\;z \leq 1.52 \cdot 10^{+57}:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;-z\\
\end{array}
\end{array}
if z < -6.2000000000000003e42 or 1.51999999999999998e57 < z Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6459.5
Applied rewrites59.5%
if -6.2000000000000003e42 < z < 1.51999999999999998e57Initial program 99.8%
Taylor expanded in z around 0
+-commutativeN/A
associate--l+N/A
lower-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-log.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6497.2
Applied rewrites97.2%
Taylor expanded in y around 0
Applied rewrites60.4%
(FPCore (x y z) :precision binary64 (if (<= y 10500000000.0) (- (fma (log y) -0.5 x) z) (+ y (fma (log y) (- -0.5 y) x))))
double code(double x, double y, double z) {
double tmp;
if (y <= 10500000000.0) {
tmp = fma(log(y), -0.5, x) - z;
} else {
tmp = y + fma(log(y), (-0.5 - y), x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 10500000000.0) tmp = Float64(fma(log(y), -0.5, x) - z); else tmp = Float64(y + fma(log(y), Float64(-0.5 - y), x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 10500000000.0], N[(N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision] - z), $MachinePrecision], N[(y + N[(N[Log[y], $MachinePrecision] * N[(-0.5 - y), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 10500000000:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;y + \mathsf{fma}\left(\log y, -0.5 - y, x\right)\\
\end{array}
\end{array}
if y < 1.05e10Initial program 100.0%
Taylor expanded in y around 0
sub-negN/A
+-commutativeN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f6499.4
Applied rewrites99.4%
if 1.05e10 < y Initial program 99.7%
Taylor expanded in z around 0
+-commutativeN/A
associate--l+N/A
lower-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-log.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6487.9
Applied rewrites87.9%
(FPCore (x y z) :precision binary64 (if (<= y 11000000000.0) (- (fma (log y) -0.5 x) z) (+ y (fma (log y) (- y) x))))
double code(double x, double y, double z) {
double tmp;
if (y <= 11000000000.0) {
tmp = fma(log(y), -0.5, x) - z;
} else {
tmp = y + fma(log(y), -y, x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 11000000000.0) tmp = Float64(fma(log(y), -0.5, x) - z); else tmp = Float64(y + fma(log(y), Float64(-y), x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 11000000000.0], N[(N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision] - z), $MachinePrecision], N[(y + N[(N[Log[y], $MachinePrecision] * (-y) + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 11000000000:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;y + \mathsf{fma}\left(\log y, -y, x\right)\\
\end{array}
\end{array}
if y < 1.1e10Initial program 100.0%
Taylor expanded in y around 0
sub-negN/A
+-commutativeN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f6499.4
Applied rewrites99.4%
if 1.1e10 < y Initial program 99.7%
Taylor expanded in z around 0
+-commutativeN/A
associate--l+N/A
lower-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lower-log.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f6487.9
Applied rewrites87.9%
Taylor expanded in y around inf
Applied rewrites87.6%
(FPCore (x y z) :precision binary64 (if (<= y 3.6e+40) (- (fma (log y) -0.5 x) z) (- y (* y (log y)))))
double code(double x, double y, double z) {
double tmp;
if (y <= 3.6e+40) {
tmp = fma(log(y), -0.5, x) - z;
} else {
tmp = y - (y * log(y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 3.6e+40) tmp = Float64(fma(log(y), -0.5, x) - z); else tmp = Float64(y - Float64(y * log(y))); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 3.6e+40], N[(N[(N[Log[y], $MachinePrecision] * -0.5 + x), $MachinePrecision] - z), $MachinePrecision], N[(y - N[(y * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.6 \cdot 10^{+40}:\\
\;\;\;\;\mathsf{fma}\left(\log y, -0.5, x\right) - z\\
\mathbf{else}:\\
\;\;\;\;y - y \cdot \log y\\
\end{array}
\end{array}
if y < 3.59999999999999996e40Initial program 100.0%
Taylor expanded in y around 0
sub-negN/A
+-commutativeN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-fma.f64N/A
lower-log.f6497.0
Applied rewrites97.0%
if 3.59999999999999996e40 < y Initial program 99.7%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip3-+N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
lift-+.f64N/A
lower-/.f6499.4
Applied rewrites99.4%
Taylor expanded in y around inf
cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
distribute-rgt-inN/A
*-lft-identityN/A
cancel-sign-subN/A
log-recN/A
remove-double-negN/A
*-commutativeN/A
lower--.f64N/A
lower-*.f64N/A
lower-log.f6475.9
Applied rewrites75.9%
(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.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6424.4
Applied rewrites24.4%
(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 2024233
(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))