
(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 8 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 (- (+ (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(Float64(log(z) + 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[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(\log z + 1\right) - z\right)
\end{array}
(FPCore (x y z)
:precision binary64
(if (<= z 4.5e-297)
(* x 0.5)
(if (or (<= z 1.8e-245)
(and (not (<= z 4.8e-214))
(or (<= z 1.02e-186)
(and (not (<= z 1.05e-148))
(or (<= z 8.8e-135)
(and (not (<= z 2.1e-100)) (<= z 1.45e-29)))))))
(* y (+ (log z) 1.0))
(- (* x 0.5) (* y z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 4.5e-297) {
tmp = x * 0.5;
} else if ((z <= 1.8e-245) || (!(z <= 4.8e-214) && ((z <= 1.02e-186) || (!(z <= 1.05e-148) && ((z <= 8.8e-135) || (!(z <= 2.1e-100) && (z <= 1.45e-29))))))) {
tmp = y * (log(z) + 1.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) :: tmp
if (z <= 4.5d-297) then
tmp = x * 0.5d0
else if ((z <= 1.8d-245) .or. (.not. (z <= 4.8d-214)) .and. (z <= 1.02d-186) .or. (.not. (z <= 1.05d-148)) .and. (z <= 8.8d-135) .or. (.not. (z <= 2.1d-100)) .and. (z <= 1.45d-29)) then
tmp = y * (log(z) + 1.0d0)
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 <= 4.5e-297) {
tmp = x * 0.5;
} else if ((z <= 1.8e-245) || (!(z <= 4.8e-214) && ((z <= 1.02e-186) || (!(z <= 1.05e-148) && ((z <= 8.8e-135) || (!(z <= 2.1e-100) && (z <= 1.45e-29))))))) {
tmp = y * (Math.log(z) + 1.0);
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 4.5e-297: tmp = x * 0.5 elif (z <= 1.8e-245) or (not (z <= 4.8e-214) and ((z <= 1.02e-186) or (not (z <= 1.05e-148) and ((z <= 8.8e-135) or (not (z <= 2.1e-100) and (z <= 1.45e-29)))))): tmp = y * (math.log(z) + 1.0) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 4.5e-297) tmp = Float64(x * 0.5); elseif ((z <= 1.8e-245) || (!(z <= 4.8e-214) && ((z <= 1.02e-186) || (!(z <= 1.05e-148) && ((z <= 8.8e-135) || (!(z <= 2.1e-100) && (z <= 1.45e-29))))))) tmp = Float64(y * Float64(log(z) + 1.0)); 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 <= 4.5e-297) tmp = x * 0.5; elseif ((z <= 1.8e-245) || (~((z <= 4.8e-214)) && ((z <= 1.02e-186) || (~((z <= 1.05e-148)) && ((z <= 8.8e-135) || (~((z <= 2.1e-100)) && (z <= 1.45e-29))))))) tmp = y * (log(z) + 1.0); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 4.5e-297], N[(x * 0.5), $MachinePrecision], If[Or[LessEqual[z, 1.8e-245], And[N[Not[LessEqual[z, 4.8e-214]], $MachinePrecision], Or[LessEqual[z, 1.02e-186], And[N[Not[LessEqual[z, 1.05e-148]], $MachinePrecision], Or[LessEqual[z, 8.8e-135], And[N[Not[LessEqual[z, 2.1e-100]], $MachinePrecision], LessEqual[z, 1.45e-29]]]]]]], N[(y * N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 4.5 \cdot 10^{-297}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{elif}\;z \leq 1.8 \cdot 10^{-245} \lor \neg \left(z \leq 4.8 \cdot 10^{-214}\right) \land \left(z \leq 1.02 \cdot 10^{-186} \lor \neg \left(z \leq 1.05 \cdot 10^{-148}\right) \land \left(z \leq 8.8 \cdot 10^{-135} \lor \neg \left(z \leq 2.1 \cdot 10^{-100}\right) \land z \leq 1.45 \cdot 10^{-29}\right)\right):\\
\;\;\;\;y \cdot \left(\log z + 1\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
(FPCore (x y z) :precision binary64 (if (or (<= (* x 0.5) -2e-25) (not (<= (* x 0.5) 5e-92))) (- (* x 0.5) (* y z)) (* y (- (+ (log z) 1.0) z))))
double code(double x, double y, double z) {
double tmp;
if (((x * 0.5) <= -2e-25) || !((x * 0.5) <= 5e-92)) {
tmp = (x * 0.5) - (y * z);
} else {
tmp = y * ((log(z) + 1.0) - 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 (((x * 0.5d0) <= (-2d-25)) .or. (.not. ((x * 0.5d0) <= 5d-92))) then
tmp = (x * 0.5d0) - (y * z)
else
tmp = y * ((log(z) + 1.0d0) - z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (((x * 0.5) <= -2e-25) || !((x * 0.5) <= 5e-92)) {
tmp = (x * 0.5) - (y * z);
} else {
tmp = y * ((Math.log(z) + 1.0) - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if ((x * 0.5) <= -2e-25) or not ((x * 0.5) <= 5e-92): tmp = (x * 0.5) - (y * z) else: tmp = y * ((math.log(z) + 1.0) - z) return tmp
function code(x, y, z) tmp = 0.0 if ((Float64(x * 0.5) <= -2e-25) || !(Float64(x * 0.5) <= 5e-92)) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); else tmp = Float64(y * Float64(Float64(log(z) + 1.0) - z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (((x * 0.5) <= -2e-25) || ~(((x * 0.5) <= 5e-92))) tmp = (x * 0.5) - (y * z); else tmp = y * ((log(z) + 1.0) - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[N[(x * 0.5), $MachinePrecision], -2e-25], N[Not[LessEqual[N[(x * 0.5), $MachinePrecision], 5e-92]], $MachinePrecision]], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], N[(y * N[(N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -2 \cdot 10^{-25} \lor \neg \left(x \cdot 0.5 \leq 5 \cdot 10^{-92}\right):\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(\left(\log z + 1\right) - z\right)\\
\end{array}
\end{array}
(FPCore (x y z) :precision binary64 (if (<= z 0.068) (+ (* x 0.5) (* y (+ (log z) 1.0))) (+ (* x 0.5) (* y (- 1.0 z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.068) {
tmp = (x * 0.5) + (y * (log(z) + 1.0));
} else {
tmp = (x * 0.5) + (y * (1.0 - 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.068d0) then
tmp = (x * 0.5d0) + (y * (log(z) + 1.0d0))
else
tmp = (x * 0.5d0) + (y * (1.0d0 - z))
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 0.068) {
tmp = (x * 0.5) + (y * (Math.log(z) + 1.0));
} else {
tmp = (x * 0.5) + (y * (1.0 - z));
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 0.068: tmp = (x * 0.5) + (y * (math.log(z) + 1.0)) else: tmp = (x * 0.5) + (y * (1.0 - z)) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 0.068) tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(log(z) + 1.0))); else tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(1.0 - z))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 0.068) tmp = (x * 0.5) + (y * (log(z) + 1.0)); else tmp = (x * 0.5) + (y * (1.0 - z)); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 0.068], N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(1.0 - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.068:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(\log z + 1\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(1 - z\right)\\
\end{array}
\end{array}
(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}
(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 2.2e+53) (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.2e+53) {
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.2d+53) 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.2e+53) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 2.2e+53: tmp = x * 0.5 else: tmp = y * -z return tmp
function code(x, y, z) tmp = 0.0 if (z <= 2.2e+53) 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.2e+53) tmp = x * 0.5; else tmp = y * -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 2.2e+53], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.2 \cdot 10^{+53}:\\
\;\;\;\;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 2024008
(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)))))