
(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
(let* ((t_0 (* y (+ 1.0 (log z)))) (t_1 (- (* x 0.5) (* y z))))
(if (<= z 5.2e-151)
t_0
(if (<= z 6.4e-123)
t_1
(if (<= z 2.25e-115)
t_0
(if (<= z 3.5e-64)
t_1
(if (<= z 5.2e-11) t_0 (fma y (- z) (* x 0.5)))))))))
double code(double x, double y, double z) {
double t_0 = y * (1.0 + log(z));
double t_1 = (x * 0.5) - (y * z);
double tmp;
if (z <= 5.2e-151) {
tmp = t_0;
} else if (z <= 6.4e-123) {
tmp = t_1;
} else if (z <= 2.25e-115) {
tmp = t_0;
} else if (z <= 3.5e-64) {
tmp = t_1;
} else if (z <= 5.2e-11) {
tmp = t_0;
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y * Float64(1.0 + log(z))) t_1 = Float64(Float64(x * 0.5) - Float64(y * z)) tmp = 0.0 if (z <= 5.2e-151) tmp = t_0; elseif (z <= 6.4e-123) tmp = t_1; elseif (z <= 2.25e-115) tmp = t_0; elseif (z <= 3.5e-64) tmp = t_1; elseif (z <= 5.2e-11) 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[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 5.2e-151], t$95$0, If[LessEqual[z, 6.4e-123], t$95$1, If[LessEqual[z, 2.25e-115], t$95$0, If[LessEqual[z, 3.5e-64], t$95$1, If[LessEqual[z, 5.2e-11], t$95$0, N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(1 + \log z\right)\\
t_1 := x \cdot 0.5 - y \cdot z\\
\mathbf{if}\;z \leq 5.2 \cdot 10^{-151}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 6.4 \cdot 10^{-123}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 2.25 \cdot 10^{-115}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 3.5 \cdot 10^{-64}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 5.2 \cdot 10^{-11}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 5.2000000000000001e-151 or 6.39999999999999957e-123 < z < 2.25000000000000011e-115 or 3.5000000000000003e-64 < z < 5.2000000000000001e-11Initial program 99.8%
Taylor expanded in z around 0 99.4%
Taylor expanded in x around 0 61.7%
if 5.2000000000000001e-151 < z < 6.39999999999999957e-123 or 2.25000000000000011e-115 < z < 3.5000000000000003e-64Initial program 99.9%
Taylor expanded in z around inf 68.3%
associate-*r*68.3%
mul-1-neg68.3%
Simplified68.3%
if 5.2000000000000001e-11 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 97.9%
mul-1-neg97.9%
Simplified97.9%
Final simplification80.6%
(FPCore (x y z)
:precision binary64
(if (or (<= z 3.4e-152)
(and (not (<= z 5.5e-123))
(or (<= z 2.25e-115)
(and (not (<= z 1.25e-64)) (<= z 4.4e-11)))))
(* y (+ 1.0 (log z)))
(- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 3.4e-152) || (!(z <= 5.5e-123) && ((z <= 2.25e-115) || (!(z <= 1.25e-64) && (z <= 4.4e-11))))) {
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 <= 3.4d-152) .or. (.not. (z <= 5.5d-123)) .and. (z <= 2.25d-115) .or. (.not. (z <= 1.25d-64)) .and. (z <= 4.4d-11)) 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 <= 3.4e-152) || (!(z <= 5.5e-123) && ((z <= 2.25e-115) || (!(z <= 1.25e-64) && (z <= 4.4e-11))))) {
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 <= 3.4e-152) or (not (z <= 5.5e-123) and ((z <= 2.25e-115) or (not (z <= 1.25e-64) and (z <= 4.4e-11)))): 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 <= 3.4e-152) || (!(z <= 5.5e-123) && ((z <= 2.25e-115) || (!(z <= 1.25e-64) && (z <= 4.4e-11))))) 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 <= 3.4e-152) || (~((z <= 5.5e-123)) && ((z <= 2.25e-115) || (~((z <= 1.25e-64)) && (z <= 4.4e-11))))) 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, 3.4e-152], And[N[Not[LessEqual[z, 5.5e-123]], $MachinePrecision], Or[LessEqual[z, 2.25e-115], And[N[Not[LessEqual[z, 1.25e-64]], $MachinePrecision], LessEqual[z, 4.4e-11]]]]], 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 3.4 \cdot 10^{-152} \lor \neg \left(z \leq 5.5 \cdot 10^{-123}\right) \land \left(z \leq 2.25 \cdot 10^{-115} \lor \neg \left(z \leq 1.25 \cdot 10^{-64}\right) \land z \leq 4.4 \cdot 10^{-11}\right):\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 3.39999999999999984e-152 or 5.5e-123 < z < 2.25000000000000011e-115 or 1.25000000000000008e-64 < z < 4.4000000000000003e-11Initial program 99.8%
Taylor expanded in z around 0 99.4%
Taylor expanded in x around 0 61.7%
if 3.39999999999999984e-152 < z < 5.5e-123 or 2.25000000000000011e-115 < z < 1.25000000000000008e-64 or 4.4000000000000003e-11 < z Initial program 99.9%
Taylor expanded in z around inf 92.9%
associate-*r*92.9%
mul-1-neg92.9%
Simplified92.9%
Final simplification80.6%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -4e-101)
(fma y (- z) (* x 0.5))
(if (<= (* x 0.5) 1e-21)
(* y (- (+ 1.0 (log z)) z))
(- (* x 0.5) (* y z)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -4e-101) {
tmp = fma(y, -z, (x * 0.5));
} else if ((x * 0.5) <= 1e-21) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -4e-101) tmp = fma(y, Float64(-z), Float64(x * 0.5)); elseif (Float64(x * 0.5) <= 1e-21) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - 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], -4e-101], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 1e-21], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $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 -4 \cdot 10^{-101}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\mathbf{elif}\;x \cdot 0.5 \leq 10^{-21}:\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -4.00000000000000021e-101Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 82.4%
mul-1-neg82.4%
Simplified82.4%
if -4.00000000000000021e-101 < (*.f64 x #s(literal 1/2 binary64)) < 9.99999999999999908e-22Initial program 99.8%
Taylor expanded in x around -inf 68.8%
mul-1-neg68.8%
distribute-rgt-neg-in68.8%
fma-neg68.8%
*-commutative68.8%
+-commutative68.8%
associate--l+68.8%
+-commutative68.8%
associate-/l*68.7%
+-commutative68.7%
metadata-eval68.7%
Simplified68.7%
clear-num68.6%
un-div-inv68.7%
Applied egg-rr68.7%
Taylor expanded in x around 0 92.6%
if 9.99999999999999908e-22 < (*.f64 x #s(literal 1/2 binary64)) Initial program 99.9%
Taylor expanded in z around inf 88.7%
associate-*r*88.7%
mul-1-neg88.7%
Simplified88.7%
Final simplification88.5%
(FPCore (x y z) :precision binary64 (if (<= z 0.0245) (+ (* 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.0245) {
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.0245) 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.0245], 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.0245:\\
\;\;\;\;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.024500000000000001Initial program 99.8%
Taylor expanded in z around 0 98.7%
if 0.024500000000000001 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 99.3%
mul-1-neg99.3%
Simplified99.3%
Final simplification99.0%
(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 (<= x -8.8e-41) (not (<= x 2e-21))) (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -8.8e-41) || !(x <= 2e-21)) {
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 ((x <= (-8.8d-41)) .or. (.not. (x <= 2d-21))) 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 ((x <= -8.8e-41) || !(x <= 2e-21)) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -8.8e-41) or not (x <= 2e-21): tmp = x * 0.5 else: tmp = y * -z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -8.8e-41) || !(x <= 2e-21)) 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 ((x <= -8.8e-41) || ~((x <= 2e-21))) tmp = x * 0.5; else tmp = y * -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -8.8e-41], N[Not[LessEqual[x, 2e-21]], $MachinePrecision]], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.8 \cdot 10^{-41} \lor \neg \left(x \leq 2 \cdot 10^{-21}\right):\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-z\right)\\
\end{array}
\end{array}
if x < -8.7999999999999999e-41 or 1.99999999999999982e-21 < x Initial program 99.9%
Taylor expanded in x around inf 67.8%
if -8.7999999999999999e-41 < x < 1.99999999999999982e-21Initial program 99.8%
Taylor expanded in x around -inf 70.6%
mul-1-neg70.6%
distribute-rgt-neg-in70.6%
fma-neg70.6%
*-commutative70.6%
+-commutative70.6%
associate--l+70.6%
+-commutative70.6%
associate-/l*70.5%
+-commutative70.5%
metadata-eval70.5%
Simplified70.5%
clear-num70.5%
un-div-inv70.5%
Applied egg-rr70.5%
Taylor expanded in z around inf 49.4%
mul-1-neg49.4%
distribute-rgt-neg-in49.4%
Simplified49.4%
Final simplification59.2%
(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 72.6%
associate-*r*72.6%
mul-1-neg72.6%
Simplified72.6%
Final simplification72.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.9%
Taylor expanded in x around inf 41.2%
Final simplification41.2%
(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 2024078
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(- (+ y (* 0.5 x)) (* y (- z (log z))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))