
(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 (+ 1.0 (- (log z) z)) y (* x 0.5)))
double code(double x, double y, double z) {
return fma((1.0 + (log(z) - z)), y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(1.0 + Float64(log(z) - z)), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[(N[(1.0 + N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 + \left(\log z - z\right), y, x \cdot 0.5\right)
\end{array}
Initial program 99.8%
+-commutative99.8%
*-commutative99.8%
fma-def99.8%
sub-neg99.8%
associate-+l+99.8%
+-commutative99.8%
sub-neg99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -2e-16)
(- (* x 0.5) (* z y))
(if (<= (* x 0.5) 4e-12)
(* y (- (+ 1.0 (log z)) z))
(fma (- z) y (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -2e-16) {
tmp = (x * 0.5) - (z * y);
} else if ((x * 0.5) <= 4e-12) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -2e-16) tmp = Float64(Float64(x * 0.5) - Float64(z * y)); elseif (Float64(x * 0.5) <= 4e-12) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(x * 0.5), $MachinePrecision], -2e-16], N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 4e-12], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -2 \cdot 10^{-16}:\\
\;\;\;\;x \cdot 0.5 - z \cdot y\\
\mathbf{elif}\;x \cdot 0.5 \leq 4 \cdot 10^{-12}:\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if (*.f64 x 1/2) < -2e-16Initial program 100.0%
Taylor expanded in z around inf 94.0%
mul-1-neg94.0%
distribute-rgt-neg-out94.0%
Simplified94.0%
*-commutative94.0%
distribute-rgt-neg-out94.0%
unsub-neg94.0%
*-commutative94.0%
Applied egg-rr94.0%
if -2e-16 < (*.f64 x 1/2) < 3.99999999999999992e-12Initial program 99.7%
+-commutative99.7%
*-commutative99.7%
fma-def99.8%
sub-neg99.8%
associate-+l+99.7%
+-commutative99.7%
sub-neg99.7%
Applied egg-rr99.7%
Taylor expanded in y around inf 89.0%
if 3.99999999999999992e-12 < (*.f64 x 1/2) Initial program 99.9%
+-commutative99.9%
*-commutative99.9%
fma-def99.9%
sub-neg99.9%
associate-+l+99.9%
+-commutative99.9%
sub-neg99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 88.4%
neg-mul-188.4%
Simplified88.4%
Final simplification90.1%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* y (+ 1.0 (log z)))))
(if (<= z 9e-191)
t_0
(if (<= z 1.45e-149)
(- (* x 0.5) (* z y))
(if (<= z 3.8e-61) t_0 (fma (- z) y (* x 0.5)))))))
double code(double x, double y, double z) {
double t_0 = y * (1.0 + log(z));
double tmp;
if (z <= 9e-191) {
tmp = t_0;
} else if (z <= 1.45e-149) {
tmp = (x * 0.5) - (z * y);
} else if (z <= 3.8e-61) {
tmp = t_0;
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y * Float64(1.0 + log(z))) tmp = 0.0 if (z <= 9e-191) tmp = t_0; elseif (z <= 1.45e-149) tmp = Float64(Float64(x * 0.5) - Float64(z * y)); elseif (z <= 3.8e-61) tmp = t_0; else tmp = fma(Float64(-z), y, 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, 9e-191], t$95$0, If[LessEqual[z, 1.45e-149], N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 3.8e-61], t$95$0, N[((-z) * y + 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 \cdot 10^{-191}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq 1.45 \cdot 10^{-149}:\\
\;\;\;\;x \cdot 0.5 - z \cdot y\\
\mathbf{elif}\;z \leq 3.8 \cdot 10^{-61}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 9.00000000000000017e-191 or 1.45e-149 < z < 3.7999999999999998e-61Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 58.7%
if 9.00000000000000017e-191 < z < 1.45e-149Initial program 99.7%
Taylor expanded in z around inf 68.0%
mul-1-neg68.0%
distribute-rgt-neg-out68.0%
Simplified68.0%
*-commutative68.0%
distribute-rgt-neg-out68.0%
unsub-neg68.0%
*-commutative68.0%
Applied egg-rr68.0%
if 3.7999999999999998e-61 < z Initial program 99.9%
+-commutative99.9%
*-commutative99.9%
fma-def99.9%
sub-neg99.9%
associate-+l+99.9%
+-commutative99.9%
sub-neg99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 91.8%
neg-mul-191.8%
Simplified91.8%
Final simplification78.7%
(FPCore (x y z) :precision binary64 (if (or (<= z 5.6e-191) (and (not (<= z 2.5e-149)) (<= z 2.4e-61))) (* y (+ 1.0 (log z))) (- (* x 0.5) (* z y))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 5.6e-191) || (!(z <= 2.5e-149) && (z <= 2.4e-61))) {
tmp = y * (1.0 + log(z));
} else {
tmp = (x * 0.5) - (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 ((z <= 5.6d-191) .or. (.not. (z <= 2.5d-149)) .and. (z <= 2.4d-61)) then
tmp = y * (1.0d0 + log(z))
else
tmp = (x * 0.5d0) - (z * y)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 5.6e-191) || (!(z <= 2.5e-149) && (z <= 2.4e-61))) {
tmp = y * (1.0 + Math.log(z));
} else {
tmp = (x * 0.5) - (z * y);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 5.6e-191) or (not (z <= 2.5e-149) and (z <= 2.4e-61)): tmp = y * (1.0 + math.log(z)) else: tmp = (x * 0.5) - (z * y) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 5.6e-191) || (!(z <= 2.5e-149) && (z <= 2.4e-61))) tmp = Float64(y * Float64(1.0 + log(z))); else tmp = Float64(Float64(x * 0.5) - Float64(z * y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 5.6e-191) || (~((z <= 2.5e-149)) && (z <= 2.4e-61))) tmp = y * (1.0 + log(z)); else tmp = (x * 0.5) - (z * y); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 5.6e-191], And[N[Not[LessEqual[z, 2.5e-149]], $MachinePrecision], LessEqual[z, 2.4e-61]]], N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.6 \cdot 10^{-191} \lor \neg \left(z \leq 2.5 \cdot 10^{-149}\right) \land z \leq 2.4 \cdot 10^{-61}:\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - z \cdot y\\
\end{array}
\end{array}
if z < 5.60000000000000023e-191 or 2.49999999999999984e-149 < z < 2.4000000000000001e-61Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 58.7%
if 5.60000000000000023e-191 < z < 2.49999999999999984e-149 or 2.4000000000000001e-61 < z Initial program 99.9%
Taylor expanded in z around inf 88.8%
mul-1-neg88.8%
distribute-rgt-neg-out88.8%
Simplified88.8%
*-commutative88.8%
distribute-rgt-neg-out88.8%
unsub-neg88.8%
*-commutative88.8%
Applied egg-rr88.8%
Final simplification78.7%
(FPCore (x y z) :precision binary64 (if (<= z 0.28) (+ (* x 0.5) (* y (+ 1.0 (log z)))) (fma (- z) y (* 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(-z, y, (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(Float64(-z), y, 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[((-z) * y + 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(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.28000000000000003Initial program 99.7%
Taylor expanded in z around 0 98.4%
if 0.28000000000000003 < z Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
fma-def100.0%
sub-neg100.0%
associate-+l+100.0%
+-commutative100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in z around inf 97.9%
neg-mul-197.9%
Simplified97.9%
Final simplification98.2%
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (log z) (- 1.0 z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * (log(z) + (1.0 - 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 * (log(z) + (1.0d0 - z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * (Math.log(z) + (1.0 - z)));
}
def code(x, y, z): return (x * 0.5) + (y * (math.log(z) + (1.0 - z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(log(z) + Float64(1.0 - z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * (log(z) + (1.0 - z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\log z + \left(1 - z\right)\right)
\end{array}
Initial program 99.8%
Final simplification99.8%
(FPCore (x y z) :precision binary64 (- (* x 0.5) (* z y)))
double code(double x, double y, double z) {
return (x * 0.5) - (z * y);
}
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) - (z * y)
end function
public static double code(double x, double y, double z) {
return (x * 0.5) - (z * y);
}
def code(x, y, z): return (x * 0.5) - (z * y)
function code(x, y, z) return Float64(Float64(x * 0.5) - Float64(z * y)) end
function tmp = code(x, y, z) tmp = (x * 0.5) - (z * y); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 - z \cdot y
\end{array}
Initial program 99.8%
Taylor expanded in z around inf 73.4%
mul-1-neg73.4%
distribute-rgt-neg-out73.4%
Simplified73.4%
*-commutative73.4%
distribute-rgt-neg-out73.4%
unsub-neg73.4%
*-commutative73.4%
Applied egg-rr73.4%
Final simplification73.4%
(FPCore (x y z) :precision binary64 (if (<= z 7.5e+33) (* x 0.5) (* z (- y))))
double code(double x, double y, double z) {
double tmp;
if (z <= 7.5e+33) {
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 (z <= 7.5d+33) 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 (z <= 7.5e+33) {
tmp = x * 0.5;
} else {
tmp = z * -y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 7.5e+33: tmp = x * 0.5 else: tmp = z * -y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 7.5e+33) 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 (z <= 7.5e+33) tmp = x * 0.5; else tmp = z * -y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 7.5e+33], N[(x * 0.5), $MachinePrecision], N[(z * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 7.5 \cdot 10^{+33}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(-y\right)\\
\end{array}
\end{array}
if z < 7.50000000000000046e33Initial program 99.7%
Taylor expanded in x around inf 50.0%
if 7.50000000000000046e33 < z Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
fma-def100.0%
sub-neg100.0%
associate-+l+100.0%
+-commutative100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in z around inf 81.3%
mul-1-neg81.3%
*-commutative81.3%
distribute-rgt-neg-in81.3%
Simplified81.3%
Final simplification63.6%
(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.8%
Taylor expanded in x around inf 37.4%
Final simplification37.4%
(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 2023223
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:herbie-target
(- (+ y (* 0.5 x)) (* y (- z (log z))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))