Data.Octree.Internal:octantDistance from Octree-0.5.4.2

Percentage Accurate: 54.2% → 99.6%
Time: 3.8s
Alternatives: 2
Speedup: 24.0×

Specification

?
\[\begin{array}{l} \\ \sqrt{x \cdot x + y \cdot y} \end{array} \]
(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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 2 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 54.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{x \cdot x + y \cdot y} \end{array} \]
(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}

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\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} \]
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}
Derivation
  1. Initial program 59.2%

    \[\sqrt{x \cdot x + y \cdot y} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{y + \frac{1}{2} \cdot \frac{{x}^{2}}{y}} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{x}^{2}}{y} + y} \]
    2. *-lft-identityN/A

      \[\leadsto \frac{1}{2} \cdot \frac{\color{blue}{1 \cdot {x}^{2}}}{y} + y \]
    3. associate-*l/N/A

      \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\frac{1}{y} \cdot {x}^{2}\right)} + y \]
    4. associate-*l*N/A

      \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot {x}^{2}} + y \]
    5. unpow2N/A

      \[\leadsto \left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot \color{blue}{\left(x \cdot x\right)} + y \]
    6. associate-*r*N/A

      \[\leadsto \color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right) \cdot x} + y \]
    7. *-commutativeN/A

      \[\leadsto \color{blue}{x \cdot \left(\left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x\right)} + y \]
    8. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\frac{1}{2} \cdot \frac{1}{y}\right) \cdot x, y\right)} \]
    9. associate-*r/N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{\frac{1}{2} \cdot 1}{y}} \cdot x, y\right) \]
    10. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(x, \frac{\color{blue}{\frac{1}{2}}}{y} \cdot x, y\right) \]
    11. associate-*l/N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{\frac{1}{2} \cdot x}{y}}, y\right) \]
    12. lower-/.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\frac{\frac{1}{2} \cdot x}{y}}, y\right) \]
    13. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(x, \frac{\color{blue}{x \cdot \frac{1}{2}}}{y}, y\right) \]
    14. lower-*.f6426.0

      \[\leadsto \mathsf{fma}\left(x, \frac{\color{blue}{x \cdot 0.5}}{y}, y\right) \]
  5. Simplified26.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \frac{x \cdot 0.5}{y}, y\right)} \]
  6. Add Preprocessing

Alternative 2: 99.1% accurate, 24.0× speedup?

\[\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} \]
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}
Derivation
  1. Initial program 59.2%

    \[\sqrt{x \cdot x + y \cdot y} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \sqrt{\color{blue}{{y}^{2}}} \]
  4. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
    2. lower-*.f6431.5

      \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
  5. Simplified31.5%

    \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
  6. Step-by-step derivation
    1. sqrt-prodN/A

      \[\leadsto \color{blue}{\sqrt{y} \cdot \sqrt{y}} \]
    2. rem-square-sqrt26.0

      \[\leadsto \color{blue}{y} \]
  7. Applied egg-rr26.0%

    \[\leadsto \color{blue}{y} \]
  8. Add Preprocessing

Developer Target 1: 53.2% accurate, 0.7× speedup?

\[\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} \]
(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}

Reproduce

?
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))))