
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(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 * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(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 * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma y (+ (- 1.0 z) (log z)) (* x 0.5)))
double code(double x, double y, double z) {
return fma(y, ((1.0 - z) + log(z)), (x * 0.5));
}
function code(x, y, z) return fma(y, Float64(Float64(1.0 - z) + log(z)), Float64(x * 0.5)) end
code[x_, y_, z_] := N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, \left(1 - z\right) + \log z, x \cdot 0.5\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* y (+ 1.0 (log z)))))
(if (<= z 9.5e-162)
t_0
(if (<= z 3.3e-142)
(* x (- 0.5 (* z (/ y x))))
(if (<= z 7e-44) t_0 (fma y (- z) (* x 0.5)))))))
double code(double x, double y, double z) {
double t_0 = y * (1.0 + log(z));
double tmp;
if (z <= 9.5e-162) {
tmp = t_0;
} else if (z <= 3.3e-142) {
tmp = x * (0.5 - (z * (y / x)));
} else if (z <= 7e-44) {
tmp = t_0;
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y * Float64(1.0 + log(z))) tmp = 0.0 if (z <= 9.5e-162) tmp = t_0; elseif (z <= 3.3e-142) tmp = Float64(x * Float64(0.5 - Float64(z * Float64(y / x)))); elseif (z <= 7e-44) tmp = t_0; else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 9.5e-162], t$95$0, If[LessEqual[z, 3.3e-142], N[(x * N[(0.5 - N[(z * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 7e-44], t$95$0, N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(1 + \log z\right)\\
\mathbf{if}\;z \leq 9.5 \cdot 10^{-162}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 3.3 \cdot 10^{-142}:\\
\;\;\;\;x \cdot \left(0.5 - z \cdot \frac{y}{x}\right)\\
\mathbf{elif}\;z \leq 7 \cdot 10^{-44}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 9.5000000000000004e-162 or 3.2999999999999997e-142 < z < 6.9999999999999995e-44Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 62.7%
Taylor expanded in z around 0 62.7%
if 9.5000000000000004e-162 < z < 3.2999999999999997e-142Initial program 99.8%
Taylor expanded in x around inf 89.5%
associate-/l*89.5%
+-commutative89.5%
associate--l+89.5%
Simplified89.5%
Taylor expanded in z around inf 78.8%
mul-1-neg78.8%
*-commutative78.8%
associate-*r/78.9%
distribute-rgt-neg-out78.9%
distribute-neg-frac278.9%
Simplified78.9%
if 6.9999999999999995e-44 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 91.6%
mul-1-neg91.6%
Simplified91.6%
Final simplification79.9%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -2e-127)
(fma y (- z) (* x 0.5))
(if (<= (* x 0.5) 5e+57)
(* y (- (+ 1.0 (log z)) z))
(- (* x 0.5) (* y z)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -2e-127) {
tmp = fma(y, -z, (x * 0.5));
} else if ((x * 0.5) <= 5e+57) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -2e-127) tmp = fma(y, Float64(-z), Float64(x * 0.5)); elseif (Float64(x * 0.5) <= 5e+57) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = Float64(Float64(x * 0.5) - Float64(y * z)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(x * 0.5), $MachinePrecision], -2e-127], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 5e+57], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -2 \cdot 10^{-127}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\mathbf{elif}\;x \cdot 0.5 \leq 5 \cdot 10^{+57}:\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -2.0000000000000001e-127Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 86.6%
mul-1-neg86.6%
Simplified86.6%
if -2.0000000000000001e-127 < (*.f64 x #s(literal 1/2 binary64)) < 4.99999999999999972e57Initial program 99.8%
Taylor expanded in x around inf 79.9%
associate-/l*74.3%
+-commutative74.3%
associate--l+74.3%
Simplified74.3%
Taylor expanded in x around 0 90.1%
if 4.99999999999999972e57 < (*.f64 x #s(literal 1/2 binary64)) Initial program 100.0%
Taylor expanded in z around inf 92.8%
associate-*r*92.8%
mul-1-neg92.8%
Simplified92.8%
distribute-lft-neg-out92.8%
unsub-neg92.8%
Applied egg-rr92.8%
Final simplification89.2%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* y (+ 1.0 (log z)))))
(if (<= z 7e-164)
t_0
(if (<= z 5.5e-143)
(* x (- 0.5 (* z (/ y x))))
(if (<= z 8.6e-44) t_0 (- (* x 0.5) (* y z)))))))
double code(double x, double y, double z) {
double t_0 = y * (1.0 + log(z));
double tmp;
if (z <= 7e-164) {
tmp = t_0;
} else if (z <= 5.5e-143) {
tmp = x * (0.5 - (z * (y / x)));
} else if (z <= 8.6e-44) {
tmp = t_0;
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: t_0
real(8) :: tmp
t_0 = y * (1.0d0 + log(z))
if (z <= 7d-164) then
tmp = t_0
else if (z <= 5.5d-143) then
tmp = x * (0.5d0 - (z * (y / x)))
else if (z <= 8.6d-44) then
tmp = t_0
else
tmp = (x * 0.5d0) - (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y * (1.0 + Math.log(z));
double tmp;
if (z <= 7e-164) {
tmp = t_0;
} else if (z <= 5.5e-143) {
tmp = x * (0.5 - (z * (y / x)));
} else if (z <= 8.6e-44) {
tmp = t_0;
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): t_0 = y * (1.0 + math.log(z)) tmp = 0 if z <= 7e-164: tmp = t_0 elif z <= 5.5e-143: tmp = x * (0.5 - (z * (y / x))) elif z <= 8.6e-44: tmp = t_0 else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) t_0 = Float64(y * Float64(1.0 + log(z))) tmp = 0.0 if (z <= 7e-164) tmp = t_0; elseif (z <= 5.5e-143) tmp = Float64(x * Float64(0.5 - Float64(z * Float64(y / x)))); elseif (z <= 8.6e-44) tmp = t_0; else tmp = Float64(Float64(x * 0.5) - Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) t_0 = y * (1.0 + log(z)); tmp = 0.0; if (z <= 7e-164) tmp = t_0; elseif (z <= 5.5e-143) tmp = x * (0.5 - (z * (y / x))); elseif (z <= 8.6e-44) tmp = t_0; else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 7e-164], t$95$0, If[LessEqual[z, 5.5e-143], N[(x * N[(0.5 - N[(z * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 8.6e-44], t$95$0, N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(1 + \log z\right)\\
\mathbf{if}\;z \leq 7 \cdot 10^{-164}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 5.5 \cdot 10^{-143}:\\
\;\;\;\;x \cdot \left(0.5 - z \cdot \frac{y}{x}\right)\\
\mathbf{elif}\;z \leq 8.6 \cdot 10^{-44}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 6.9999999999999999e-164 or 5.50000000000000041e-143 < z < 8.60000000000000027e-44Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 62.7%
Taylor expanded in z around 0 62.7%
if 6.9999999999999999e-164 < z < 5.50000000000000041e-143Initial program 99.8%
Taylor expanded in x around inf 89.5%
associate-/l*89.5%
+-commutative89.5%
associate--l+89.5%
Simplified89.5%
Taylor expanded in z around inf 78.8%
mul-1-neg78.8%
*-commutative78.8%
associate-*r/78.9%
distribute-rgt-neg-out78.9%
distribute-neg-frac278.9%
Simplified78.9%
if 8.60000000000000027e-44 < z Initial program 100.0%
Taylor expanded in z around inf 91.6%
associate-*r*91.6%
mul-1-neg91.6%
Simplified91.6%
distribute-lft-neg-out91.6%
unsub-neg91.6%
Applied egg-rr91.6%
Final simplification79.8%
(FPCore (x y z) :precision binary64 (if (<= z 0.28) (+ (* x 0.5) (* y (+ 1.0 (log z)))) (fma y (- z) (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.28) {
tmp = (x * 0.5) + (y * (1.0 + log(z)));
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.28) tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(1.0 + log(z)))); else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.28], N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.28:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(1 + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.28000000000000003Initial program 99.7%
Taylor expanded in z around 0 99.2%
if 0.28000000000000003 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 98.1%
mul-1-neg98.1%
Simplified98.1%
Final simplification98.7%
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(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 * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.6e-41) (not (<= x 3e+63))) (* x 0.5) (* z (- y))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.6e-41) || !(x <= 3e+63)) {
tmp = x * 0.5;
} else {
tmp = z * -y;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-1.6d-41)) .or. (.not. (x <= 3d+63))) then
tmp = x * 0.5d0
else
tmp = z * -y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -1.6e-41) || !(x <= 3e+63)) {
tmp = x * 0.5;
} else {
tmp = z * -y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -1.6e-41) or not (x <= 3e+63): tmp = x * 0.5 else: tmp = z * -y return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -1.6e-41) || !(x <= 3e+63)) tmp = Float64(x * 0.5); else tmp = Float64(z * Float64(-y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -1.6e-41) || ~((x <= 3e+63))) tmp = x * 0.5; else tmp = z * -y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.6e-41], N[Not[LessEqual[x, 3e+63]], $MachinePrecision]], N[(x * 0.5), $MachinePrecision], N[(z * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.6 \cdot 10^{-41} \lor \neg \left(x \leq 3 \cdot 10^{+63}\right):\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(-y\right)\\
\end{array}
\end{array}
if x < -1.60000000000000006e-41 or 2.99999999999999999e63 < x Initial program 99.9%
Taylor expanded in x around inf 71.0%
if -1.60000000000000006e-41 < x < 2.99999999999999999e63Initial program 99.8%
Taylor expanded in x around inf 81.6%
associate-/l*76.0%
+-commutative76.0%
associate--l+76.0%
Simplified76.0%
Taylor expanded in z around inf 45.1%
associate-*r*45.1%
mul-1-neg45.1%
Simplified45.1%
Final simplification56.5%
(FPCore (x y z) :precision binary64 (- (* x 0.5) (* y z)))
double code(double x, double y, double z) {
return (x * 0.5) - (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 * 0.5d0) - (y * z)
end function
public static double code(double x, double y, double z) {
return (x * 0.5) - (y * z);
}
def code(x, y, z): return (x * 0.5) - (y * z)
function code(x, y, z) return Float64(Float64(x * 0.5) - Float64(y * z)) end
function tmp = code(x, y, z) tmp = (x * 0.5) - (y * z); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 - y \cdot z
\end{array}
Initial program 99.9%
Taylor expanded in z around inf 70.9%
associate-*r*70.9%
mul-1-neg70.9%
Simplified70.9%
distribute-lft-neg-out70.9%
unsub-neg70.9%
Applied egg-rr70.9%
Final simplification70.9%
(FPCore (x y z) :precision binary64 (* x 0.5))
double code(double x, double y, double z) {
return x * 0.5;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x * 0.5d0
end function
public static double code(double x, double y, double z) {
return x * 0.5;
}
def code(x, y, z): return x * 0.5
function code(x, y, z) return Float64(x * 0.5) end
function tmp = code(x, y, z) tmp = x * 0.5; end
code[x_, y_, z_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
Initial program 99.9%
Taylor expanded in x around inf 39.2%
Final simplification39.2%
(FPCore (x y z) :precision binary64 (- (+ y (* 0.5 x)) (* y (- z (log z)))))
double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (y + (0.5d0 * x)) - (y * (z - log(z)))
end function
public static double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - Math.log(z)));
}
def code(x, y, z): return (y + (0.5 * x)) - (y * (z - math.log(z)))
function code(x, y, z) return Float64(Float64(y + Float64(0.5 * x)) - Float64(y * Float64(z - log(z)))) end
function tmp = code(x, y, z) tmp = (y + (0.5 * x)) - (y * (z - log(z))); end
code[x_, y_, z_] := N[(N[(y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision] - N[(y * N[(z - N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + 0.5 \cdot x\right) - y \cdot \left(z - \log z\right)
\end{array}
herbie shell --seed 2024077
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(- (+ y (* 0.5 x)) (* y (- z (log z))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))