
(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%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ (* x 0.5) (* y (* -1.0 z)))))
(if (<= (* x 0.5) -2e-127)
t_0
(if (<= (* x 0.5) 5e+57) (* y (- (+ 1.0 (log z)) z)) t_0))))
double code(double x, double y, double z) {
double t_0 = (x * 0.5) + (y * (-1.0 * z));
double tmp;
if ((x * 0.5) <= -2e-127) {
tmp = t_0;
} else if ((x * 0.5) <= 5e+57) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = t_0;
}
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) :: t_0
real(8) :: tmp
t_0 = (x * 0.5d0) + (y * ((-1.0d0) * z))
if ((x * 0.5d0) <= (-2d-127)) then
tmp = t_0
else if ((x * 0.5d0) <= 5d+57) then
tmp = y * ((1.0d0 + log(z)) - z)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = (x * 0.5) + (y * (-1.0 * z));
double tmp;
if ((x * 0.5) <= -2e-127) {
tmp = t_0;
} else if ((x * 0.5) <= 5e+57) {
tmp = y * ((1.0 + Math.log(z)) - z);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = (x * 0.5) + (y * (-1.0 * z)) tmp = 0 if (x * 0.5) <= -2e-127: tmp = t_0 elif (x * 0.5) <= 5e+57: tmp = y * ((1.0 + math.log(z)) - z) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(Float64(x * 0.5) + Float64(y * Float64(-1.0 * z))) tmp = 0.0 if (Float64(x * 0.5) <= -2e-127) tmp = t_0; elseif (Float64(x * 0.5) <= 5e+57) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = (x * 0.5) + (y * (-1.0 * z)); tmp = 0.0; if ((x * 0.5) <= -2e-127) tmp = t_0; elseif ((x * 0.5) <= 5e+57) tmp = y * ((1.0 + log(z)) - z); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(-1.0 * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * 0.5), $MachinePrecision], -2e-127], t$95$0, If[LessEqual[N[(x * 0.5), $MachinePrecision], 5e+57], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot 0.5 + y \cdot \left(-1 \cdot z\right)\\
\mathbf{if}\;x \cdot 0.5 \leq -2 \cdot 10^{-127}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \cdot 0.5 \leq 5 \cdot 10^{+57}:\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -2.0000000000000001e-127 or 4.99999999999999972e57 < (*.f64 x #s(literal 1/2 binary64)) Initial program 99.9%
Taylor expanded in z around inf 88.4%
if -2.0000000000000001e-127 < (*.f64 x #s(literal 1/2 binary64)) < 4.99999999999999972e57Initial program 99.8%
Taylor expanded in x around 0 90.1%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -2e-127)
(fma y (* -1.0 z) (* x 0.5))
(if (<= (* x 0.5) 5e+57)
(* y (- (+ 1.0 (log z)) z))
(+ (* x 0.5) (* y (* -1.0 z))))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -2e-127) {
tmp = fma(y, (-1.0 * z), (x * 0.5));
} else if ((x * 0.5) <= 5e+57) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = (x * 0.5) + (y * (-1.0 * z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -2e-127) tmp = fma(y, Float64(-1.0 * z), Float64(x * 0.5)); elseif (Float64(x * 0.5) <= 5e+57) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(-1.0 * z))); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(x * 0.5), $MachinePrecision], -2e-127], N[(y * N[(-1.0 * z), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 5e+57], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $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}\;x \cdot 0.5 \leq -2 \cdot 10^{-127}:\\
\;\;\;\;\mathsf{fma}\left(y, -1 \cdot z, x \cdot 0.5\right)\\
\mathbf{elif}\;x \cdot 0.5 \leq 5 \cdot 10^{+57}:\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(-1 \cdot z\right)\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -2.0000000000000001e-127Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 86.6%
if -2.0000000000000001e-127 < (*.f64 x #s(literal 1/2 binary64)) < 4.99999999999999972e57Initial program 99.8%
Taylor expanded in x around 0 90.1%
if 4.99999999999999972e57 < (*.f64 x #s(literal 1/2 binary64)) Initial program 100.0%
Taylor expanded in z around inf 92.8%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* y (+ 1.0 (log z)))) (t_1 (+ (* x 0.5) (* y (* -1.0 z)))))
(if (<= z 9.5e-162)
t_0
(if (<= z 3.3e-142) t_1 (if (<= z 7e-44) t_0 t_1)))))
double code(double x, double y, double z) {
double t_0 = y * (1.0 + log(z));
double t_1 = (x * 0.5) + (y * (-1.0 * z));
double tmp;
if (z <= 9.5e-162) {
tmp = t_0;
} else if (z <= 3.3e-142) {
tmp = t_1;
} else if (z <= 7e-44) {
tmp = t_0;
} else {
tmp = t_1;
}
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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = y * (1.0d0 + log(z))
t_1 = (x * 0.5d0) + (y * ((-1.0d0) * z))
if (z <= 9.5d-162) then
tmp = t_0
else if (z <= 3.3d-142) then
tmp = t_1
else if (z <= 7d-44) then
tmp = t_0
else
tmp = t_1
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y * (1.0 + Math.log(z));
double t_1 = (x * 0.5) + (y * (-1.0 * z));
double tmp;
if (z <= 9.5e-162) {
tmp = t_0;
} else if (z <= 3.3e-142) {
tmp = t_1;
} else if (z <= 7e-44) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
def code(x, y, z): t_0 = y * (1.0 + math.log(z)) t_1 = (x * 0.5) + (y * (-1.0 * z)) tmp = 0 if z <= 9.5e-162: tmp = t_0 elif z <= 3.3e-142: tmp = t_1 elif z <= 7e-44: tmp = t_0 else: tmp = t_1 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 * Float64(-1.0 * z))) tmp = 0.0 if (z <= 9.5e-162) tmp = t_0; elseif (z <= 3.3e-142) tmp = t_1; elseif (z <= 7e-44) tmp = t_0; else tmp = t_1; end return tmp end
function tmp_2 = code(x, y, z) t_0 = y * (1.0 + log(z)); t_1 = (x * 0.5) + (y * (-1.0 * z)); tmp = 0.0; if (z <= 9.5e-162) tmp = t_0; elseif (z <= 3.3e-142) tmp = t_1; elseif (z <= 7e-44) tmp = t_0; else tmp = t_1; end tmp_2 = 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 * N[(-1.0 * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 9.5e-162], t$95$0, If[LessEqual[z, 3.3e-142], t$95$1, If[LessEqual[z, 7e-44], t$95$0, t$95$1]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(1 + \log z\right)\\
t_1 := x \cdot 0.5 + y \cdot \left(-1 \cdot z\right)\\
\mathbf{if}\;z \leq 9.5 \cdot 10^{-162}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 3.3 \cdot 10^{-142}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 7 \cdot 10^{-44}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if z < 9.5000000000000004e-162 or 3.2999999999999997e-142 < z < 6.9999999999999995e-44Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 62.7%
Taylor expanded in z around 0 62.7%
if 9.5000000000000004e-162 < z < 3.2999999999999997e-142 or 6.9999999999999995e-44 < z Initial program 100.0%
Taylor expanded in z around inf 90.8%
(FPCore (x y z) :precision binary64 (if (<= z 0.28) (+ (* x 0.5) (* y (+ 1.0 (log z)))) (fma y (* -1.0 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, (-1.0 * 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(-1.0 * 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 * N[(-1.0 * z), $MachinePrecision] + 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, -1 \cdot z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.28000000000000003Initial program 99.7%
Taylor expanded in z around 0 99.2%
if 0.28000000000000003 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 98.1%
(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%
(FPCore (x y z) :precision binary64 (if (<= x -4.6e-42) (* 0.5 x) (if (<= x 3.2e+63) (* -1.0 (* y z)) (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (x <= -4.6e-42) {
tmp = 0.5 * x;
} else if (x <= 3.2e+63) {
tmp = -1.0 * (y * z);
} else {
tmp = 0.5 * x;
}
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 <= (-4.6d-42)) then
tmp = 0.5d0 * x
else if (x <= 3.2d+63) then
tmp = (-1.0d0) * (y * z)
else
tmp = 0.5d0 * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -4.6e-42) {
tmp = 0.5 * x;
} else if (x <= 3.2e+63) {
tmp = -1.0 * (y * z);
} else {
tmp = 0.5 * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -4.6e-42: tmp = 0.5 * x elif x <= 3.2e+63: tmp = -1.0 * (y * z) else: tmp = 0.5 * x return tmp
function code(x, y, z) tmp = 0.0 if (x <= -4.6e-42) tmp = Float64(0.5 * x); elseif (x <= 3.2e+63) tmp = Float64(-1.0 * Float64(y * z)); else tmp = Float64(0.5 * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -4.6e-42) tmp = 0.5 * x; elseif (x <= 3.2e+63) tmp = -1.0 * (y * z); else tmp = 0.5 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -4.6e-42], N[(0.5 * x), $MachinePrecision], If[LessEqual[x, 3.2e+63], N[(-1.0 * N[(y * z), $MachinePrecision]), $MachinePrecision], N[(0.5 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.6 \cdot 10^{-42}:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{elif}\;x \leq 3.2 \cdot 10^{+63}:\\
\;\;\;\;-1 \cdot \left(y \cdot z\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot x\\
\end{array}
\end{array}
if x < -4.60000000000000008e-42 or 3.20000000000000011e63 < x Initial program 99.9%
Taylor expanded in x around inf 71.0%
if -4.60000000000000008e-42 < x < 3.20000000000000011e63Initial program 99.8%
Taylor expanded in z around inf 45.1%
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (* -1.0 z))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * (-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 * ((-1.0d0) * z))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * (-1.0 * z));
}
def code(x, y, z): return (x * 0.5) + (y * (-1.0 * z))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(-1.0 * z))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * (-1.0 * z)); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(-1.0 * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(-1 \cdot z\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around inf 70.9%
(FPCore (x y z) :precision binary64 (* 0.5 x))
double code(double x, double y, double z) {
return 0.5 * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 0.5d0 * x
end function
public static double code(double x, double y, double z) {
return 0.5 * x;
}
def code(x, y, z): return 0.5 * x
function code(x, y, z) return Float64(0.5 * x) end
function tmp = code(x, y, z) tmp = 0.5 * x; end
code[x_, y_, z_] := N[(0.5 * x), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot x
\end{array}
Initial program 99.9%
Taylor expanded in x around inf 39.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 2024077 -o generate:simplify
(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)))))