
(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%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -5e-85)
(fma y (- z) (* x 0.5))
(if (<= (* x 0.5) 5000000000000.0)
(* y (+ (- 1.0 z) (log z)))
(- (* x 0.5) (* y z)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -5e-85) {
tmp = fma(y, -z, (x * 0.5));
} else if ((x * 0.5) <= 5000000000000.0) {
tmp = y * ((1.0 - z) + log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -5e-85) tmp = fma(y, Float64(-z), Float64(x * 0.5)); elseif (Float64(x * 0.5) <= 5000000000000.0) tmp = Float64(y * Float64(Float64(1.0 - z) + log(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], -5e-85], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 5000000000000.0], N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $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 -5 \cdot 10^{-85}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\mathbf{elif}\;x \cdot 0.5 \leq 5000000000000:\\
\;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -5.0000000000000002e-85Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 84.4%
neg-mul-184.4%
Simplified84.4%
if -5.0000000000000002e-85 < (*.f64 x #s(literal 1/2 binary64)) < 5e12Initial program 99.8%
distribute-rgt-in99.8%
fma-define99.8%
*-commutative99.8%
Applied egg-rr99.8%
Taylor expanded in x around inf 66.3%
Taylor expanded in x around 0 87.6%
distribute-lft-out87.7%
Simplified87.7%
if 5e12 < (*.f64 x #s(literal 1/2 binary64)) Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 88.4%
neg-mul-188.4%
Simplified88.4%
Taylor expanded in y around 0 88.4%
+-commutative88.4%
*-commutative88.4%
mul-1-neg88.4%
unsub-neg88.4%
*-commutative88.4%
Simplified88.4%
Final simplification86.7%
(FPCore (x y z) :precision binary64 (if (<= z 5.2e-293) (* x 0.5) (if (<= z 1.4e-69) (* y (+ 1.0 (log z))) (fma y (- z) (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 5.2e-293) {
tmp = x * 0.5;
} else if (z <= 1.4e-69) {
tmp = 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 <= 5.2e-293) tmp = Float64(x * 0.5); elseif (z <= 1.4e-69) tmp = 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, 5.2e-293], N[(x * 0.5), $MachinePrecision], If[LessEqual[z, 1.4e-69], N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.2 \cdot 10^{-293}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{elif}\;z \leq 1.4 \cdot 10^{-69}:\\
\;\;\;\;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 < 5.1999999999999996e-293Initial program 100.0%
Taylor expanded in x around inf 67.9%
if 5.1999999999999996e-293 < z < 1.3999999999999999e-69Initial program 99.8%
Taylor expanded in x around inf 89.9%
associate-/l*89.8%
+-commutative89.8%
associate--l+89.8%
Simplified89.8%
Taylor expanded in z around 0 89.8%
Taylor expanded in x around 0 57.0%
+-commutative57.0%
Simplified57.0%
if 1.3999999999999999e-69 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 92.8%
neg-mul-192.8%
Simplified92.8%
Final simplification79.3%
(FPCore (x y z) :precision binary64 (if (<= z 5.6e-293) (* x 0.5) (if (<= z 1.45e-68) (* y (+ 1.0 (log z))) (- (* x 0.5) (* y z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 5.6e-293) {
tmp = x * 0.5;
} else if (z <= 1.45e-68) {
tmp = y * (1.0 + log(z));
} 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) :: tmp
if (z <= 5.6d-293) then
tmp = x * 0.5d0
else if (z <= 1.45d-68) then
tmp = y * (1.0d0 + log(z))
else
tmp = (x * 0.5d0) - (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 5.6e-293) {
tmp = x * 0.5;
} else if (z <= 1.45e-68) {
tmp = y * (1.0 + Math.log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 5.6e-293: tmp = x * 0.5 elif z <= 1.45e-68: tmp = y * (1.0 + math.log(z)) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 5.6e-293) tmp = Float64(x * 0.5); elseif (z <= 1.45e-68) tmp = Float64(y * Float64(1.0 + log(z))); else tmp = Float64(Float64(x * 0.5) - Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 5.6e-293) tmp = x * 0.5; elseif (z <= 1.45e-68) tmp = y * (1.0 + log(z)); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 5.6e-293], N[(x * 0.5), $MachinePrecision], If[LessEqual[z, 1.45e-68], N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.6 \cdot 10^{-293}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{elif}\;z \leq 1.45 \cdot 10^{-68}:\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 5.60000000000000051e-293Initial program 100.0%
Taylor expanded in x around inf 67.9%
if 5.60000000000000051e-293 < z < 1.45e-68Initial program 99.8%
Taylor expanded in x around inf 89.9%
associate-/l*89.8%
+-commutative89.8%
associate--l+89.8%
Simplified89.8%
Taylor expanded in z around 0 89.8%
Taylor expanded in x around 0 57.0%
+-commutative57.0%
Simplified57.0%
if 1.45e-68 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 92.8%
neg-mul-192.8%
Simplified92.8%
Taylor expanded in y around 0 92.8%
+-commutative92.8%
*-commutative92.8%
mul-1-neg92.8%
unsub-neg92.8%
*-commutative92.8%
Simplified92.8%
Final simplification79.3%
(FPCore (x y z) :precision binary64 (if (<= z 8.2e-21) (+ (* 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 <= 8.2e-21) {
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 <= 8.2e-21) 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, 8.2e-21], 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 8.2 \cdot 10^{-21}:\\
\;\;\;\;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 < 8.19999999999999988e-21Initial program 99.8%
Taylor expanded in z around 0 99.8%
if 8.19999999999999988e-21 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 98.6%
neg-mul-198.6%
Simplified98.6%
(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%
(FPCore (x y z) :precision binary64 (if (<= z 2.4e+26) (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.4e+26) {
tmp = x * 0.5;
} else {
tmp = 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) :: tmp
if (z <= 2.4d+26) then
tmp = x * 0.5d0
else
tmp = y * -z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 2.4e+26) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 2.4e+26: tmp = x * 0.5 else: tmp = y * -z return tmp
function code(x, y, z) tmp = 0.0 if (z <= 2.4e+26) tmp = Float64(x * 0.5); else tmp = Float64(y * Float64(-z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 2.4e+26) tmp = x * 0.5; else tmp = y * -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 2.4e+26], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.4 \cdot 10^{+26}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-z\right)\\
\end{array}
\end{array}
if z < 2.40000000000000005e26Initial program 99.8%
Taylor expanded in x around inf 53.2%
if 2.40000000000000005e26 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 100.0%
neg-mul-1100.0%
Simplified100.0%
Taylor expanded in z around inf 100.0%
neg-mul-1100.0%
+-commutative100.0%
unsub-neg100.0%
associate-*r/100.0%
Simplified100.0%
Taylor expanded in x around 0 74.0%
neg-mul-174.0%
Simplified74.0%
Final simplification62.0%
(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%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 75.0%
neg-mul-175.0%
Simplified75.0%
Taylor expanded in y around 0 75.0%
+-commutative75.0%
*-commutative75.0%
mul-1-neg75.0%
unsub-neg75.0%
*-commutative75.0%
Simplified75.0%
Final simplification75.0%
(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 42.2%
Final simplification42.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 2024118
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(! :herbie-platform default (- (+ y (* 1/2 x)) (* y (- z (log z)))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))