
(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%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -5e-60)
(- (* x 0.5) (* y z))
(if (<= (* x 0.5) 2e-126)
(* y (- (+ 1.0 (log z)) z))
(fma y (- z) (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -5e-60) {
tmp = (x * 0.5) - (y * z);
} else if ((x * 0.5) <= 2e-126) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -5e-60) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); elseif (Float64(x * 0.5) <= 2e-126) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(x * 0.5), $MachinePrecision], -5e-60], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 2e-126], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -5 \cdot 10^{-60}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{elif}\;x \cdot 0.5 \leq 2 \cdot 10^{-126}:\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if (*.f64 x 1/2) < -5.0000000000000001e-60Initial program 100.0%
Taylor expanded in z around inf 87.9%
mul-1-neg87.9%
distribute-rgt-neg-in87.9%
Simplified87.9%
distribute-rgt-neg-out87.9%
unsub-neg87.9%
Applied egg-rr87.9%
if -5.0000000000000001e-60 < (*.f64 x 1/2) < 1.9999999999999999e-126Initial program 99.9%
distribute-lft-in99.7%
Applied egg-rr99.7%
Taylor expanded in x around 0 90.6%
Taylor expanded in y around 0 90.7%
if 1.9999999999999999e-126 < (*.f64 x 1/2) Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 82.4%
mul-1-neg82.4%
Simplified82.4%
Final simplification87.0%
(FPCore (x y z) :precision binary64 (if (<= z 6.6e-196) (- (* x 0.5) (* y z)) (if (<= z 6.5e-51) (* y (+ 1.0 (log z))) (fma y (- z) (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 6.6e-196) {
tmp = (x * 0.5) - (y * z);
} else if (z <= 6.5e-51) {
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 <= 6.6e-196) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); elseif (z <= 6.5e-51) 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, 6.6e-196], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 6.5e-51], 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 6.6 \cdot 10^{-196}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{elif}\;z \leq 6.5 \cdot 10^{-51}:\\
\;\;\;\;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 < 6.59999999999999997e-196Initial program 99.9%
Taylor expanded in z around inf 60.8%
mul-1-neg60.8%
distribute-rgt-neg-in60.8%
Simplified60.8%
distribute-rgt-neg-out60.8%
unsub-neg60.8%
Applied egg-rr60.8%
if 6.59999999999999997e-196 < z < 6.5000000000000003e-51Initial program 99.8%
+-commutative99.8%
fma-define99.8%
Simplified99.8%
Taylor expanded in z around 0 99.8%
Taylor expanded in y around inf 63.6%
if 6.5000000000000003e-51 < z Initial program 99.9%
+-commutative99.9%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 93.1%
mul-1-neg93.1%
Simplified93.1%
Final simplification80.0%
(FPCore (x y z) :precision binary64 (if (or (<= z 1.66e-195) (not (<= z 2.2e-51))) (- (* x 0.5) (* y z)) (* y (+ 1.0 (log z)))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.66e-195) || !(z <= 2.2e-51)) {
tmp = (x * 0.5) - (y * z);
} else {
tmp = y * (1.0 + log(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.66d-195) .or. (.not. (z <= 2.2d-51))) then
tmp = (x * 0.5d0) - (y * z)
else
tmp = y * (1.0d0 + log(z))
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 1.66e-195) || !(z <= 2.2e-51)) {
tmp = (x * 0.5) - (y * z);
} else {
tmp = y * (1.0 + Math.log(z));
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 1.66e-195) or not (z <= 2.2e-51): tmp = (x * 0.5) - (y * z) else: tmp = y * (1.0 + math.log(z)) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 1.66e-195) || !(z <= 2.2e-51)) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); else tmp = Float64(y * Float64(1.0 + log(z))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 1.66e-195) || ~((z <= 2.2e-51))) tmp = (x * 0.5) - (y * z); else tmp = y * (1.0 + log(z)); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.66e-195], N[Not[LessEqual[z, 2.2e-51]], $MachinePrecision]], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.66 \cdot 10^{-195} \lor \neg \left(z \leq 2.2 \cdot 10^{-51}\right):\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\end{array}
\end{array}
if z < 1.66e-195 or 2.2e-51 < z Initial program 99.9%
Taylor expanded in z around inf 84.7%
mul-1-neg84.7%
distribute-rgt-neg-in84.7%
Simplified84.7%
distribute-rgt-neg-out84.7%
unsub-neg84.7%
Applied egg-rr84.7%
if 1.66e-195 < z < 2.2e-51Initial program 99.8%
+-commutative99.8%
fma-define99.8%
Simplified99.8%
Taylor expanded in z around 0 99.8%
Taylor expanded in y around inf 63.6%
Final simplification80.0%
(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.9%
Taylor expanded in z around 0 98.8%
if 0.28000000000000003 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 97.0%
mul-1-neg97.0%
Simplified97.0%
Final simplification97.9%
(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%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (if (or (<= z 50000000.0) (and (not (<= z 2.9e+57)) (<= z 2.4e+71))) (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 50000000.0) || (!(z <= 2.9e+57) && (z <= 2.4e+71))) {
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 <= 50000000.0d0) .or. (.not. (z <= 2.9d+57)) .and. (z <= 2.4d+71)) 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 <= 50000000.0) || (!(z <= 2.9e+57) && (z <= 2.4e+71))) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 50000000.0) or (not (z <= 2.9e+57) and (z <= 2.4e+71)): tmp = x * 0.5 else: tmp = y * -z return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 50000000.0) || (!(z <= 2.9e+57) && (z <= 2.4e+71))) 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 <= 50000000.0) || (~((z <= 2.9e+57)) && (z <= 2.4e+71))) tmp = x * 0.5; else tmp = y * -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 50000000.0], And[N[Not[LessEqual[z, 2.9e+57]], $MachinePrecision], LessEqual[z, 2.4e+71]]], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 50000000 \lor \neg \left(z \leq 2.9 \cdot 10^{+57}\right) \land z \leq 2.4 \cdot 10^{+71}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-z\right)\\
\end{array}
\end{array}
if z < 5e7 or 2.9000000000000002e57 < z < 2.39999999999999981e71Initial program 99.9%
Taylor expanded in x around inf 50.4%
if 5e7 < z < 2.9000000000000002e57 or 2.39999999999999981e71 < z Initial program 100.0%
distribute-lft-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 73.3%
Taylor expanded in y around -inf 73.3%
mul-1-neg73.3%
*-commutative73.3%
distribute-rgt-neg-in73.3%
mul-1-neg73.3%
log-rec73.3%
mul-1-neg73.3%
unsub-neg73.3%
log-rec73.3%
Simplified73.3%
Taylor expanded in z around inf 72.7%
Final simplification60.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.9%
Taylor expanded in z around inf 74.2%
mul-1-neg74.2%
distribute-rgt-neg-in74.2%
Simplified74.2%
distribute-rgt-neg-out74.2%
unsub-neg74.2%
Applied egg-rr74.2%
Final simplification74.2%
(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 39.0%
Final simplification39.0%
(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 2024040
(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)))))