
(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 11 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 (+ (* x 0.5) (+ (* y (- 1.0 z)) (* y (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + ((y * (1.0 - z)) + (y * 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)) + (y * log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + ((y * (1.0 - z)) + (y * Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + ((y * (1.0 - z)) + (y * math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(Float64(y * Float64(1.0 - z)) + Float64(y * log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + ((y * (1.0 - z)) + (y * log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(N[(y * N[(1.0 - z), $MachinePrecision]), $MachinePrecision] + N[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + \left(y \cdot \left(1 - z\right) + y \cdot \log z\right)
\end{array}
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -5e-101)
(fma y (- z) (* x 0.5))
(if (<= (* x 0.5) 4e-14)
(* 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-101) {
tmp = fma(y, -z, (x * 0.5));
} else if ((x * 0.5) <= 4e-14) {
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-101) tmp = fma(y, Float64(-z), Float64(x * 0.5)); elseif (Float64(x * 0.5) <= 4e-14) 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-101], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 4e-14], 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^{-101}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\mathbf{elif}\;x \cdot 0.5 \leq 4 \cdot 10^{-14}:\\
\;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ y (* y (log z)))))
(if (<= z 1.6e-61)
t_0
(if (<= z 1.4e-35)
(- (* x 0.5) (* y z))
(if (<= z 1.4e-26) 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.6e-61) {
tmp = t_0;
} else if (z <= 1.4e-35) {
tmp = (x * 0.5) - (y * z);
} else if (z <= 1.4e-26) {
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.6e-61) tmp = t_0; elseif (z <= 1.4e-35) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); elseif (z <= 1.4e-26) 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.6e-61], t$95$0, If[LessEqual[z, 1.4e-35], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 1.4e-26], 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.6 \cdot 10^{-61}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq 1.4 \cdot 10^{-35}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{elif}\;z \leq 1.4 \cdot 10^{-26}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
(FPCore (x y z) :precision binary64 (if (or (<= z 1.9e-61) (and (not (<= z 2.7e-36)) (<= z 2.55e-26))) (* y (+ 1.0 (log z))) (- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.9e-61) || (!(z <= 2.7e-36) && (z <= 2.55e-26))) {
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 <= 1.9d-61) .or. (.not. (z <= 2.7d-36)) .and. (z <= 2.55d-26)) 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 <= 1.9e-61) || (!(z <= 2.7e-36) && (z <= 2.55e-26))) {
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 <= 1.9e-61) or (not (z <= 2.7e-36) and (z <= 2.55e-26)): 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 <= 1.9e-61) || (!(z <= 2.7e-36) && (z <= 2.55e-26))) 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 <= 1.9e-61) || (~((z <= 2.7e-36)) && (z <= 2.55e-26))) tmp = y * (1.0 + log(z)); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.9e-61], And[N[Not[LessEqual[z, 2.7e-36]], $MachinePrecision], LessEqual[z, 2.55e-26]]], 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 1.9 \cdot 10^{-61} \lor \neg \left(z \leq 2.7 \cdot 10^{-36}\right) \land z \leq 2.55 \cdot 10^{-26}:\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
(FPCore (x y z) :precision binary64 (if (or (<= z 1.12e-61) (and (not (<= z 1.3e-35)) (<= z 1.75e-26))) (+ y (* y (log z))) (- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.12e-61) || (!(z <= 1.3e-35) && (z <= 1.75e-26))) {
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 <= 1.12d-61) .or. (.not. (z <= 1.3d-35)) .and. (z <= 1.75d-26)) 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 <= 1.12e-61) || (!(z <= 1.3e-35) && (z <= 1.75e-26))) {
tmp = y + (y * Math.log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 1.12e-61) or (not (z <= 1.3e-35) and (z <= 1.75e-26)): 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 <= 1.12e-61) || (!(z <= 1.3e-35) && (z <= 1.75e-26))) 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 <= 1.12e-61) || (~((z <= 1.3e-35)) && (z <= 1.75e-26))) 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, 1.12e-61], And[N[Not[LessEqual[z, 1.3e-35]], $MachinePrecision], LessEqual[z, 1.75e-26]]], 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 1.12 \cdot 10^{-61} \lor \neg \left(z \leq 1.3 \cdot 10^{-35}\right) \land z \leq 1.75 \cdot 10^{-26}:\\
\;\;\;\;y + y \cdot \log z\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
(FPCore (x y z) :precision binary64 (if (<= z 3.2e-9) (+ (* 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 <= 3.2e-9) {
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 <= 3.2e-9) 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, 3.2e-9], 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 3.2 \cdot 10^{-9}:\\
\;\;\;\;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}
(FPCore (x y z) :precision binary64 (if (<= z 3.2e-9) (+ (* x 0.5) (+ y (* y (log z)))) (fma y (- z) (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 3.2e-9) {
tmp = (x * 0.5) + (y + (y * log(z)));
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 3.2e-9) tmp = Float64(Float64(x * 0.5) + Float64(y + Float64(y * log(z)))); else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 3.2e-9], N[(N[(x * 0.5), $MachinePrecision] + N[(y + N[(y * 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 3.2 \cdot 10^{-9}:\\
\;\;\;\;x \cdot 0.5 + \left(y + y \cdot \log z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
(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 (- (* 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}
(FPCore (x y z) :precision binary64 (if (<= z 0.74) (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.74) {
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 <= 0.74d0) 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 <= 0.74) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 0.74: tmp = x * 0.5 else: tmp = y * -z return tmp
function code(x, y, z) tmp = 0.0 if (z <= 0.74) 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 <= 0.74) tmp = x * 0.5; else tmp = y * -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 0.74], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.74:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-z\right)\\
\end{array}
\end{array}
(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}
(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 2024006
(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)))))