
(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 7 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.8%
+-commutative99.8%
fma-def99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ y (* y (log z)))))
(if (<= z 1.9e-128)
t_0
(if (<= z 1.1e-101)
(- (* x 0.5) (* y z))
(if (<= z 1.6e-53) t_0 (fma y (- z) (* x 0.5)))))))
double code(double x, double y, double z) {
double t_0 = y + (y * log(z));
double tmp;
if (z <= 1.9e-128) {
tmp = t_0;
} else if (z <= 1.1e-101) {
tmp = (x * 0.5) - (y * z);
} else if (z <= 1.6e-53) {
tmp = t_0;
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y + Float64(y * log(z))) tmp = 0.0 if (z <= 1.9e-128) tmp = t_0; elseif (z <= 1.1e-101) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); elseif (z <= 1.6e-53) 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[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 1.9e-128], t$95$0, If[LessEqual[z, 1.1e-101], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 1.6e-53], t$95$0, N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y + y \cdot \log z\\
\mathbf{if}\;z \leq 1.9 \cdot 10^{-128}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq 1.1 \cdot 10^{-101}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{elif}\;z \leq 1.6 \cdot 10^{-53}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 1.9000000000000001e-128 or 1.0999999999999999e-101 < z < 1.6e-53Initial program 99.7%
distribute-lft-in99.7%
Applied egg-rr99.7%
Taylor expanded in x around 0 60.2%
Taylor expanded in z around 0 60.2%
if 1.9000000000000001e-128 < z < 1.0999999999999999e-101Initial program 99.9%
+-commutative99.9%
add-cube-cbrt99.3%
fma-def99.3%
pow299.3%
Applied egg-rr99.3%
fma-udef99.3%
unpow299.3%
add-cube-cbrt99.9%
distribute-lft-in99.8%
+-commutative99.8%
flip-+94.4%
clear-num94.4%
clear-num94.3%
flip-+99.7%
distribute-lft-in99.8%
+-commutative99.8%
Applied egg-rr99.8%
Taylor expanded in z around inf 73.8%
associate-/r/73.8%
metadata-eval73.8%
mul-1-neg73.8%
unsub-neg73.8%
Applied egg-rr73.8%
if 1.6e-53 < z Initial program 99.9%
+-commutative99.9%
fma-def100.0%
Simplified100.0%
Taylor expanded in z around inf 95.6%
mul-1-neg95.6%
Simplified95.6%
Final simplification80.9%
(FPCore (x y z) :precision binary64 (if (or (<= z 2.4e-129) (and (not (<= z 2.8e-101)) (<= z 1.05e-47))) (+ y (* y (log z))) (- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 2.4e-129) || (!(z <= 2.8e-101) && (z <= 1.05e-47))) {
tmp = y + (y * 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 <= 2.4d-129) .or. (.not. (z <= 2.8d-101)) .and. (z <= 1.05d-47)) then
tmp = y + (y * 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 <= 2.4e-129) || (!(z <= 2.8e-101) && (z <= 1.05e-47))) {
tmp = y + (y * Math.log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 2.4e-129) or (not (z <= 2.8e-101) and (z <= 1.05e-47)): tmp = y + (y * math.log(z)) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 2.4e-129) || (!(z <= 2.8e-101) && (z <= 1.05e-47))) tmp = Float64(y + Float64(y * 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 <= 2.4e-129) || (~((z <= 2.8e-101)) && (z <= 1.05e-47))) tmp = y + (y * log(z)); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 2.4e-129], And[N[Not[LessEqual[z, 2.8e-101]], $MachinePrecision], LessEqual[z, 1.05e-47]]], N[(y + N[(y * 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 2.4 \cdot 10^{-129} \lor \neg \left(z \leq 2.8 \cdot 10^{-101}\right) \land z \leq 1.05 \cdot 10^{-47}:\\
\;\;\;\;y + y \cdot \log z\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 2.39999999999999989e-129 or 2.79999999999999989e-101 < z < 1.05e-47Initial program 99.7%
distribute-lft-in99.7%
Applied egg-rr99.7%
Taylor expanded in x around 0 60.2%
Taylor expanded in z around 0 60.2%
if 2.39999999999999989e-129 < z < 2.79999999999999989e-101 or 1.05e-47 < z Initial program 99.9%
+-commutative99.9%
add-cube-cbrt99.8%
fma-def99.8%
pow299.8%
Applied egg-rr99.8%
fma-udef99.8%
unpow299.8%
add-cube-cbrt99.9%
distribute-lft-in99.3%
+-commutative99.3%
flip-+58.6%
clear-num58.6%
clear-num58.6%
flip-+99.2%
distribute-lft-in99.9%
+-commutative99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 93.1%
associate-/r/93.1%
metadata-eval93.1%
mul-1-neg93.1%
unsub-neg93.1%
Applied egg-rr93.1%
Final simplification80.9%
(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.8%
Taylor expanded in z around 0 98.5%
if 0.28000000000000003 < z Initial program 99.9%
+-commutative99.9%
fma-def100.0%
Simplified100.0%
Taylor expanded in z around inf 99.2%
mul-1-neg99.2%
Simplified99.2%
Final simplification98.8%
(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.8%
Final simplification99.8%
(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.8%
+-commutative99.8%
add-cube-cbrt99.5%
fma-def99.5%
pow299.5%
Applied egg-rr99.5%
fma-udef99.5%
unpow299.5%
add-cube-cbrt99.8%
distribute-lft-in99.4%
+-commutative99.4%
flip-+63.0%
clear-num62.9%
clear-num63.0%
flip-+99.4%
distribute-lft-in99.8%
+-commutative99.8%
Applied egg-rr99.8%
Taylor expanded in z around inf 73.8%
associate-/r/73.8%
metadata-eval73.8%
mul-1-neg73.8%
unsub-neg73.8%
Applied egg-rr73.8%
Final simplification73.8%
(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 40.4%
Final simplification40.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 2023340
(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)))))