
(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 2 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}
y_m = (fabs.f64 y) x_m = (fabs.f64 x) NOTE: x_m and y_m should be sorted in increasing order before calling this function. (FPCore (x_m y_m) :precision binary64 (fma x_m (/ (* x_m 0.5) y_m) y_m))
y_m = fabs(y);
x_m = fabs(x);
assert(x_m < y_m);
double code(double x_m, double y_m) {
return fma(x_m, ((x_m * 0.5) / y_m), y_m);
}
y_m = abs(y) x_m = abs(x) x_m, y_m = sort([x_m, y_m]) function code(x_m, y_m) return fma(x_m, Float64(Float64(x_m * 0.5) / y_m), y_m) end
y_m = N[Abs[y], $MachinePrecision] x_m = N[Abs[x], $MachinePrecision] NOTE: x_m and y_m should be sorted in increasing order before calling this function. code[x$95$m_, y$95$m_] := N[(x$95$m * N[(N[(x$95$m * 0.5), $MachinePrecision] / y$95$m), $MachinePrecision] + y$95$m), $MachinePrecision]
\begin{array}{l}
y_m = \left|y\right|
\\
x_m = \left|x\right|
\\
[x_m, y_m] = \mathsf{sort}([x_m, y_m])\\
\\
\mathsf{fma}\left(x\_m, \frac{x\_m \cdot 0.5}{y\_m}, y\_m\right)
\end{array}
Initial program 59.2%
Taylor expanded in x around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
associate-*l/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6426.0
Simplified26.0%
y_m = (fabs.f64 y) x_m = (fabs.f64 x) NOTE: x_m and y_m should be sorted in increasing order before calling this function. (FPCore (x_m y_m) :precision binary64 y_m)
y_m = fabs(y);
x_m = fabs(x);
assert(x_m < y_m);
double code(double x_m, double y_m) {
return y_m;
}
y_m = abs(y)
x_m = abs(x)
NOTE: x_m and y_m should be sorted in increasing order before calling this function.
real(8) function code(x_m, y_m)
real(8), intent (in) :: x_m
real(8), intent (in) :: y_m
code = y_m
end function
y_m = Math.abs(y);
x_m = Math.abs(x);
assert x_m < y_m;
public static double code(double x_m, double y_m) {
return y_m;
}
y_m = math.fabs(y) x_m = math.fabs(x) [x_m, y_m] = sort([x_m, y_m]) def code(x_m, y_m): return y_m
y_m = abs(y) x_m = abs(x) x_m, y_m = sort([x_m, y_m]) function code(x_m, y_m) return y_m end
y_m = abs(y);
x_m = abs(x);
x_m, y_m = num2cell(sort([x_m, y_m])){:}
function tmp = code(x_m, y_m)
tmp = y_m;
end
y_m = N[Abs[y], $MachinePrecision] x_m = N[Abs[x], $MachinePrecision] NOTE: x_m and y_m should be sorted in increasing order before calling this function. code[x$95$m_, y$95$m_] := y$95$m
\begin{array}{l}
y_m = \left|y\right|
\\
x_m = \left|x\right|
\\
[x_m, y_m] = \mathsf{sort}([x_m, y_m])\\
\\
y\_m
\end{array}
Initial program 59.2%
Taylor expanded in x around 0
unpow2N/A
lower-*.f6431.5
Simplified31.5%
sqrt-prodN/A
rem-square-sqrt26.0
Applied egg-rr26.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 2024207
(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))))