
(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 6 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 57.6%
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 (fma (* 0.5 x) (/ x y) y))
double code(double x, double y) {
return fma((0.5 * x), (x / y), y);
}
function code(x, y) return fma(Float64(0.5 * x), Float64(x / y), y) end
code[x_, y_] := N[(N[(0.5 * x), $MachinePrecision] * N[(x / y), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5 \cdot x, \frac{x}{y}, y\right)
\end{array}
Initial program 57.6%
Taylor expanded in y around inf
*-commutativeN/A
+-commutativeN/A
distribute-lft1-inN/A
associate-*r/N/A
associate-*l/N/A
associate-/l*N/A
unpow2N/A
associate-/r*N/A
*-inversesN/A
unpow2N/A
associate-*r*N/A
associate-*l*N/A
associate-/l*N/A
*-rgt-identityN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-/.f6426.4
Applied rewrites26.4%
(FPCore (x y) :precision binary64 (sqrt (fma x x (* y y))))
double code(double x, double y) {
return sqrt(fma(x, x, (y * y)));
}
function code(x, y) return sqrt(fma(x, x, Float64(y * y))) end
code[x_, y_] := N[Sqrt[N[(x * x + N[(y * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}
\end{array}
Initial program 57.6%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6457.6
Applied rewrites57.6%
(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 57.6%
Taylor expanded in y around inf
unpow2N/A
lower-*.f6431.8
Applied rewrites31.8%
(FPCore (x y) :precision binary64 (sqrt (* x x)))
double code(double x, double y) {
return sqrt((x * x));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt((x * x))
end function
public static double code(double x, double y) {
return Math.sqrt((x * x));
}
def code(x, y): return math.sqrt((x * x))
function code(x, y) return sqrt(Float64(x * x)) end
function tmp = code(x, y) tmp = sqrt((x * x)); end
code[x_, y_] := N[Sqrt[N[(x * x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x}
\end{array}
Initial program 57.6%
Taylor expanded in y around 0
unpow2N/A
lower-*.f6429.9
Applied rewrites29.9%
(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 57.6%
Taylor expanded in x around -inf
mul-1-negN/A
lower-neg.f6427.4
Applied rewrites27.4%
(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 2024244
(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))))