
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) (* y y))))
double code(double x, double y) {
return sqrt(((x * x) + (y * y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + (y * y)))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + (y * y)));
}
def code(x, y): return math.sqrt(((x * x) + (y * y)))
function code(x, y) return sqrt(Float64(Float64(x * x) + Float64(y * y))) end
function tmp = code(x, y) tmp = sqrt(((x * x) + (y * y))); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y \cdot y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) (* y y))))
double code(double x, double y) {
return sqrt(((x * x) + (y * y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + (y * y)))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + (y * y)));
}
def code(x, y): return math.sqrt(((x * x) + (y * y)))
function code(x, y) return sqrt(Float64(Float64(x * x) + Float64(y * y))) end
function tmp = code(x, y) tmp = sqrt(((x * x) + (y * y))); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y \cdot y}
\end{array}
(FPCore (x y) :precision binary64 (hypot y x))
double code(double x, double y) {
return hypot(y, x);
}
public static double code(double x, double y) {
return Math.hypot(y, x);
}
def code(x, y): return math.hypot(y, x)
function code(x, y) return hypot(y, x) end
function tmp = code(x, y) tmp = hypot(y, x); end
code[x_, y_] := N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{hypot}\left(y, x\right)
\end{array}
Initial program 53.0%
lift-sqrt.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f64100.0
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (* (/ y x) x))) (if (<= y 1.82e-163) t_0 (if (<= y 2e+161) (sqrt (fma y y (* x x))) t_0))))
double code(double x, double y) {
double t_0 = (y / x) * x;
double tmp;
if (y <= 1.82e-163) {
tmp = t_0;
} else if (y <= 2e+161) {
tmp = sqrt(fma(y, y, (x * x)));
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(y / x) * x) tmp = 0.0 if (y <= 1.82e-163) tmp = t_0; elseif (y <= 2e+161) tmp = sqrt(fma(y, y, Float64(x * x))); else tmp = t_0; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(y / x), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[y, 1.82e-163], t$95$0, If[LessEqual[y, 2e+161], N[Sqrt[N[(y * y + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x} \cdot x\\
\mathbf{if}\;y \leq 1.82 \cdot 10^{-163}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 2 \cdot 10^{+161}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(y, y, x \cdot x\right)}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < 1.82e-163 or 2.0000000000000001e161 < y Initial program 44.8%
lift-sqrt.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6414.8
Applied rewrites14.8%
Taylor expanded in x around inf
Applied rewrites10.4%
Taylor expanded in x around 0
Applied rewrites11.4%
if 1.82e-163 < y < 2.0000000000000001e161Initial program 77.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6477.1
Applied rewrites77.1%
(FPCore (x y) :precision binary64 (if (<= (* x x) 5e+295) (fma (/ 0.5 y) (* x x) y) (* (/ y x) x)))
double code(double x, double y) {
double tmp;
if ((x * x) <= 5e+295) {
tmp = fma((0.5 / y), (x * x), y);
} else {
tmp = (y / x) * x;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 5e+295) tmp = fma(Float64(0.5 / y), Float64(x * x), y); else tmp = Float64(Float64(y / x) * x); end return tmp end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 5e+295], N[(N[(0.5 / y), $MachinePrecision] * N[(x * x), $MachinePrecision] + y), $MachinePrecision], N[(N[(y / x), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+295}:\\
\;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{x} \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 4.99999999999999991e295Initial program 69.1%
Taylor expanded in x around 0
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
+-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6431.0
Applied rewrites31.0%
if 4.99999999999999991e295 < (*.f64 x x) Initial program 8.5%
lift-sqrt.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f643.4
Applied rewrites3.4%
Taylor expanded in x around inf
Applied rewrites9.1%
Taylor expanded in x around 0
Applied rewrites8.1%
(FPCore (x y) :precision binary64 (let* ((t_0 (* (/ y x) x))) (if (<= y 5e-155) t_0 (if (<= y 2e+161) (sqrt (* y y)) t_0))))
double code(double x, double y) {
double t_0 = (y / x) * x;
double tmp;
if (y <= 5e-155) {
tmp = t_0;
} else if (y <= 2e+161) {
tmp = sqrt((y * y));
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = (y / x) * x
if (y <= 5d-155) then
tmp = t_0
else if (y <= 2d+161) then
tmp = sqrt((y * y))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (y / x) * x;
double tmp;
if (y <= 5e-155) {
tmp = t_0;
} else if (y <= 2e+161) {
tmp = Math.sqrt((y * y));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (y / x) * x tmp = 0 if y <= 5e-155: tmp = t_0 elif y <= 2e+161: tmp = math.sqrt((y * y)) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(y / x) * x) tmp = 0.0 if (y <= 5e-155) tmp = t_0; elseif (y <= 2e+161) tmp = sqrt(Float64(y * y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (y / x) * x; tmp = 0.0; if (y <= 5e-155) tmp = t_0; elseif (y <= 2e+161) tmp = sqrt((y * y)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(y / x), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[y, 5e-155], t$95$0, If[LessEqual[y, 2e+161], N[Sqrt[N[(y * y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x} \cdot x\\
\mathbf{if}\;y \leq 5 \cdot 10^{-155}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 2 \cdot 10^{+161}:\\
\;\;\;\;\sqrt{y \cdot y}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < 4.9999999999999999e-155 or 2.0000000000000001e161 < y Initial program 45.4%
lift-sqrt.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6414.6
Applied rewrites14.6%
Taylor expanded in x around inf
Applied rewrites10.3%
Taylor expanded in x around 0
Applied rewrites11.3%
if 4.9999999999999999e-155 < y < 2.0000000000000001e161Initial program 77.1%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6450.4
Applied rewrites50.4%
(FPCore (x y) :precision binary64 (fma (* (/ 0.5 y) x) x y))
double code(double x, double y) {
return fma(((0.5 / y) * x), x, y);
}
function code(x, y) return fma(Float64(Float64(0.5 / y) * x), x, y) end
code[x_, y_] := N[(N[(N[(0.5 / y), $MachinePrecision] * x), $MachinePrecision] * x + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{0.5}{y} \cdot x, x, y\right)
\end{array}
Initial program 53.0%
lift-sqrt.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6423.7
Applied rewrites23.7%
Applied rewrites25.2%
(FPCore (x y) :precision binary64 (sqrt (* y y)))
double code(double x, double y) {
return sqrt((y * y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt((y * y))
end function
public static double code(double x, double y) {
return Math.sqrt((y * y));
}
def code(x, y): return math.sqrt((y * y))
function code(x, y) return sqrt(Float64(y * y)) end
function tmp = code(x, y) tmp = sqrt((y * y)); end
code[x_, y_] := N[Sqrt[N[(y * y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{y \cdot y}
\end{array}
Initial program 53.0%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6429.5
Applied rewrites29.5%
(FPCore (x y) :precision binary64 (- x))
double code(double x, double y) {
return -x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -x
end function
public static double code(double x, double y) {
return -x;
}
def code(x, y): return -x
function code(x, y) return Float64(-x) end
function tmp = code(x, y) tmp = -x; end
code[x_, y_] := (-x)
\begin{array}{l}
\\
-x
\end{array}
Initial program 53.0%
Taylor expanded in x around -inf
mul-1-negN/A
lower-neg.f6427.0
Applied rewrites27.0%
(FPCore (x y) :precision binary64 (if (< x -1.1236950826599826e+145) (- x) (if (< x 1.116557621183362e+93) (sqrt (+ (* x x) (* y y))) x)))
double code(double x, double y) {
double tmp;
if (x < -1.1236950826599826e+145) {
tmp = -x;
} else if (x < 1.116557621183362e+93) {
tmp = sqrt(((x * x) + (y * y)));
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x < (-1.1236950826599826d+145)) then
tmp = -x
else if (x < 1.116557621183362d+93) then
tmp = sqrt(((x * x) + (y * y)))
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x < -1.1236950826599826e+145) {
tmp = -x;
} else if (x < 1.116557621183362e+93) {
tmp = Math.sqrt(((x * x) + (y * y)));
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x < -1.1236950826599826e+145: tmp = -x elif x < 1.116557621183362e+93: tmp = math.sqrt(((x * x) + (y * y))) else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x < -1.1236950826599826e+145) tmp = Float64(-x); elseif (x < 1.116557621183362e+93) tmp = sqrt(Float64(Float64(x * x) + Float64(y * y))); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x < -1.1236950826599826e+145) tmp = -x; elseif (x < 1.116557621183362e+93) tmp = sqrt(((x * x) + (y * y))); else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[Less[x, -1.1236950826599826e+145], (-x), If[Less[x, 1.116557621183362e+93], N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x < -1.1236950826599826 \cdot 10^{+145}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x < 1.116557621183362 \cdot 10^{+93}:\\
\;\;\;\;\sqrt{x \cdot x + y \cdot y}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
herbie shell --seed 2024295
(FPCore (x y)
:name "Data.Octree.Internal:octantDistance from Octree-0.5.4.2"
:precision binary64
:alt
(! :herbie-platform default (if (< x -11236950826599826000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- x) (if (< x 1116557621183362000000000000000000000000000000000000000000000000000000000000000000000000000000) (sqrt (+ (* x x) (* y y))) x)))
(sqrt (+ (* x x) (* y y))))