
(FPCore (x y z) :precision binary64 (/ (* x y) (* (* z z) (+ z 1.0))))
double code(double x, double y, double z) {
return (x * y) / ((z * z) * (z + 1.0));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * y) / ((z * z) * (z + 1.0d0))
end function
public static double code(double x, double y, double z) {
return (x * y) / ((z * z) * (z + 1.0));
}
def code(x, y, z): return (x * y) / ((z * z) * (z + 1.0))
function code(x, y, z) return Float64(Float64(x * y) / Float64(Float64(z * z) * Float64(z + 1.0))) end
function tmp = code(x, y, z) tmp = (x * y) / ((z * z) * (z + 1.0)); end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] / N[(N[(z * z), $MachinePrecision] * N[(z + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot y}{\left(z \cdot z\right) \cdot \left(z + 1\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (* x y) (* (* z z) (+ z 1.0))))
double code(double x, double y, double z) {
return (x * y) / ((z * z) * (z + 1.0));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * y) / ((z * z) * (z + 1.0d0))
end function
public static double code(double x, double y, double z) {
return (x * y) / ((z * z) * (z + 1.0));
}
def code(x, y, z): return (x * y) / ((z * z) * (z + 1.0))
function code(x, y, z) return Float64(Float64(x * y) / Float64(Float64(z * z) * Float64(z + 1.0))) end
function tmp = code(x, y, z) tmp = (x * y) / ((z * z) * (z + 1.0)); end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] / N[(N[(z * z), $MachinePrecision] * N[(z + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot y}{\left(z \cdot z\right) \cdot \left(z + 1\right)}
\end{array}
y\_m = (fabs.f64 y)
y\_s = (copysign.f64 #s(literal 1 binary64) y)
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
NOTE: x_m, y_m, and z should be sorted in increasing order before calling this function.
(FPCore (x_s y_s x_m y_m z)
:precision binary64
(let* ((t_0 (/ (* (/ x_m z) (/ y_m z)) z)))
(*
x_s
(*
y_s
(if (<= z -1.08e+46)
t_0
(if (<= z 2.4e+15) (/ y_m (* (/ z x_m) (fma z z z))) t_0))))))y\_m = fabs(y);
y\_s = copysign(1.0, y);
x\_m = fabs(x);
x\_s = copysign(1.0, x);
assert(x_m < y_m && y_m < z);
double code(double x_s, double y_s, double x_m, double y_m, double z) {
double t_0 = ((x_m / z) * (y_m / z)) / z;
double tmp;
if (z <= -1.08e+46) {
tmp = t_0;
} else if (z <= 2.4e+15) {
tmp = y_m / ((z / x_m) * fma(z, z, z));
} else {
tmp = t_0;
}
return x_s * (y_s * tmp);
}
y\_m = abs(y) y\_s = copysign(1.0, y) x\_m = abs(x) x\_s = copysign(1.0, x) x_m, y_m, z = sort([x_m, y_m, z]) function code(x_s, y_s, x_m, y_m, z) t_0 = Float64(Float64(Float64(x_m / z) * Float64(y_m / z)) / z) tmp = 0.0 if (z <= -1.08e+46) tmp = t_0; elseif (z <= 2.4e+15) tmp = Float64(y_m / Float64(Float64(z / x_m) * fma(z, z, z))); else tmp = t_0; end return Float64(x_s * Float64(y_s * tmp)) end
y\_m = N[Abs[y], $MachinePrecision]
y\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y_m, and z should be sorted in increasing order before calling this function.
code[x$95$s_, y$95$s_, x$95$m_, y$95$m_, z_] := Block[{t$95$0 = N[(N[(N[(x$95$m / z), $MachinePrecision] * N[(y$95$m / z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]}, N[(x$95$s * N[(y$95$s * If[LessEqual[z, -1.08e+46], t$95$0, If[LessEqual[z, 2.4e+15], N[(y$95$m / N[(N[(z / x$95$m), $MachinePrecision] * N[(z * z + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
y\_m = \left|y\right|
\\
y\_s = \mathsf{copysign}\left(1, y\right)
\\
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y_m, z] = \mathsf{sort}([x_m, y_m, z])\\
\\
\begin{array}{l}
t_0 := \frac{\frac{x\_m}{z} \cdot \frac{y\_m}{z}}{z}\\
x\_s \cdot \left(y\_s \cdot \begin{array}{l}
\mathbf{if}\;z \leq -1.08 \cdot 10^{+46}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 2.4 \cdot 10^{+15}:\\
\;\;\;\;\frac{y\_m}{\frac{z}{x\_m} \cdot \mathsf{fma}\left(z, z, z\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}\right)
\end{array}
\end{array}
if z < -1.07999999999999994e46 or 2.4e15 < z Initial program 85.3%
lift-/.f64N/A
lift-*.f64N/A
lift-*.f64N/A
times-fracN/A
lift-*.f64N/A
associate-/r*N/A
associate-*l/N/A
clear-numN/A
inv-powN/A
clear-numN/A
inv-powN/A
unpow-prod-downN/A
times-fracN/A
lift-*.f64N/A
lower-/.f64N/A
Applied rewrites99.8%
Taylor expanded in z around inf
lower-/.f6499.8
Applied rewrites99.8%
if -1.07999999999999994e46 < z < 2.4e15Initial program 78.5%
lift-/.f64N/A
lift-*.f64N/A
lift-*.f64N/A
times-fracN/A
*-commutativeN/A
clear-numN/A
frac-timesN/A
associate-/l*N/A
*-commutativeN/A
lift-*.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
distribute-lft1-inN/A
lower-fma.f64N/A
lower-/.f6499.0
Applied rewrites99.0%
Final simplification99.3%
y\_m = (fabs.f64 y)
y\_s = (copysign.f64 #s(literal 1 binary64) y)
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
NOTE: x_m, y_m, and z should be sorted in increasing order before calling this function.
(FPCore (x_s y_s x_m y_m z)
:precision binary64
(let* ((t_0 (/ x_m (* (* (/ z y_m) z) z))) (t_1 (* (+ 1.0 z) (* z z))))
(*
x_s
(*
y_s
(if (<= t_1 -20000.0)
t_0
(if (<= t_1 5e-278)
(* (/ y_m z) (/ x_m z))
(if (<= t_1 1e+23) (* (/ x_m (* (fma z z z) z)) y_m) t_0)))))))y\_m = fabs(y);
y\_s = copysign(1.0, y);
x\_m = fabs(x);
x\_s = copysign(1.0, x);
assert(x_m < y_m && y_m < z);
double code(double x_s, double y_s, double x_m, double y_m, double z) {
double t_0 = x_m / (((z / y_m) * z) * z);
double t_1 = (1.0 + z) * (z * z);
double tmp;
if (t_1 <= -20000.0) {
tmp = t_0;
} else if (t_1 <= 5e-278) {
tmp = (y_m / z) * (x_m / z);
} else if (t_1 <= 1e+23) {
tmp = (x_m / (fma(z, z, z) * z)) * y_m;
} else {
tmp = t_0;
}
return x_s * (y_s * tmp);
}
y\_m = abs(y) y\_s = copysign(1.0, y) x\_m = abs(x) x\_s = copysign(1.0, x) x_m, y_m, z = sort([x_m, y_m, z]) function code(x_s, y_s, x_m, y_m, z) t_0 = Float64(x_m / Float64(Float64(Float64(z / y_m) * z) * z)) t_1 = Float64(Float64(1.0 + z) * Float64(z * z)) tmp = 0.0 if (t_1 <= -20000.0) tmp = t_0; elseif (t_1 <= 5e-278) tmp = Float64(Float64(y_m / z) * Float64(x_m / z)); elseif (t_1 <= 1e+23) tmp = Float64(Float64(x_m / Float64(fma(z, z, z) * z)) * y_m); else tmp = t_0; end return Float64(x_s * Float64(y_s * tmp)) end
y\_m = N[Abs[y], $MachinePrecision]
y\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[y]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y_m, and z should be sorted in increasing order before calling this function.
code[x$95$s_, y$95$s_, x$95$m_, y$95$m_, z_] := Block[{t$95$0 = N[(x$95$m / N[(N[(N[(z / y$95$m), $MachinePrecision] * z), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(1.0 + z), $MachinePrecision] * N[(z * z), $MachinePrecision]), $MachinePrecision]}, N[(x$95$s * N[(y$95$s * If[LessEqual[t$95$1, -20000.0], t$95$0, If[LessEqual[t$95$1, 5e-278], N[(N[(y$95$m / z), $MachinePrecision] * N[(x$95$m / z), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 1e+23], N[(N[(x$95$m / N[(N[(z * z + z), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision] * y$95$m), $MachinePrecision], t$95$0]]]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
y\_m = \left|y\right|
\\
y\_s = \mathsf{copysign}\left(1, y\right)
\\
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y_m, z] = \mathsf{sort}([x_m, y_m, z])\\
\\
\begin{array}{l}
t_0 := \frac{x\_m}{\left(\frac{z}{y\_m} \cdot z\right) \cdot z}\\
t_1 := \left(1 + z\right) \cdot \left(z \cdot z\right)\\
x\_s \cdot \left(y\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \leq -20000:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq 5 \cdot 10^{-278}:\\
\;\;\;\;\frac{y\_m}{z} \cdot \frac{x\_m}{z}\\
\mathbf{elif}\;t\_1 \leq 10^{+23}:\\
\;\;\;\;\frac{x\_m}{\mathsf{fma}\left(z, z, z\right) \cdot z} \cdot y\_m\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}\right)
\end{array}
\end{array}
if (*.f64 (*.f64 z z) (+.f64 z #s(literal 1 binary64))) < -2e4 or 9.9999999999999992e22 < (*.f64 (*.f64 z z) (+.f64 z #s(literal 1 binary64))) Initial program 84.2%
Taylor expanded in z around inf
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
unpow3N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6488.5
Applied rewrites88.5%
Applied rewrites83.5%
Applied rewrites96.0%
if -2e4 < (*.f64 (*.f64 z z) (+.f64 z #s(literal 1 binary64))) < 4.99999999999999985e-278Initial program 74.6%
lift-/.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
times-fracN/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
*-commutativeN/A
lift-+.f64N/A
distribute-lft1-inN/A
lower-fma.f64N/A
lower-/.f6497.6
Applied rewrites97.6%
Taylor expanded in z around 0
lower-/.f6497.3
Applied rewrites97.3%
if 4.99999999999999985e-278 < (*.f64 (*.f64 z z) (+.f64 z #s(literal 1 binary64))) < 9.9999999999999992e22Initial program 92.3%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6499.6
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
distribute-lft1-inN/A
lower-fma.f6499.6
Applied rewrites99.6%
Final simplification97.2%
(FPCore (x y z) :precision binary64 (if (< z 249.6182814532307) (/ (* y (/ x z)) (+ z (* z z))) (/ (* (/ (/ y z) (+ 1.0 z)) x) z)))
double code(double x, double y, double z) {
double tmp;
if (z < 249.6182814532307) {
tmp = (y * (x / z)) / (z + (z * z));
} else {
tmp = (((y / z) / (1.0 + z)) * x) / 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 < 249.6182814532307d0) then
tmp = (y * (x / z)) / (z + (z * z))
else
tmp = (((y / z) / (1.0d0 + z)) * x) / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z < 249.6182814532307) {
tmp = (y * (x / z)) / (z + (z * z));
} else {
tmp = (((y / z) / (1.0 + z)) * x) / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z < 249.6182814532307: tmp = (y * (x / z)) / (z + (z * z)) else: tmp = (((y / z) / (1.0 + z)) * x) / z return tmp
function code(x, y, z) tmp = 0.0 if (z < 249.6182814532307) tmp = Float64(Float64(y * Float64(x / z)) / Float64(z + Float64(z * z))); else tmp = Float64(Float64(Float64(Float64(y / z) / Float64(1.0 + z)) * x) / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z < 249.6182814532307) tmp = (y * (x / z)) / (z + (z * z)); else tmp = (((y / z) / (1.0 + z)) * x) / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Less[z, 249.6182814532307], N[(N[(y * N[(x / z), $MachinePrecision]), $MachinePrecision] / N[(z + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(y / z), $MachinePrecision] / N[(1.0 + z), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z < 249.6182814532307:\\
\;\;\;\;\frac{y \cdot \frac{x}{z}}{z + z \cdot z}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\frac{y}{z}}{1 + z} \cdot x}{z}\\
\end{array}
\end{array}
herbie shell --seed 2024230
(FPCore (x y z)
:name "Statistics.Distribution.Beta:$cvariance from math-functions-0.1.5.2"
:precision binary64
:alt
(! :herbie-platform default (if (< z 2496182814532307/10000000000000) (/ (* y (/ x z)) (+ z (* z z))) (/ (* (/ (/ y z) (+ 1 z)) x) z)))
(/ (* x y) (* (* z z) (+ z 1.0))))