Data.Octree.Internal:octantDistance from Octree-0.5.4.2

Percentage Accurate: 54.4% → 100.0%
Time: 1.7s
Alternatives: 2
Speedup: 1.9×

Specification

?
\[\sqrt{x \cdot x + y \cdot y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (sqrt (+ (* x x) (* y y))))
double code(double x, double y) {
	return sqrt(((x * x) + (y * y)));
}
real(8) function code(x, y)
use fmin_fmax_functions
    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]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	sqrt(((x * x) + (y * y)))
END code
\sqrt{x \cdot x + y \cdot y}

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.4% accurate, 1.0× speedup?

\[\sqrt{x \cdot x + y \cdot y} \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (sqrt (+ (* x x) (* y y))))
double code(double x, double y) {
	return sqrt(((x * x) + (y * y)));
}
real(8) function code(x, y)
use fmin_fmax_functions
    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]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	sqrt(((x * x) + (y * y)))
END code
\sqrt{x \cdot x + y \cdot y}

Alternative 1: 100.0% accurate, 0.7× speedup?

\[\mathsf{hypot}\left(x, y\right) \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (hypot x y))
double code(double x, double y) {
	return hypot(x, y);
}
public static double code(double x, double y) {
	return Math.hypot(x, y);
}
def code(x, y):
	return math.hypot(x, y)
function code(x, y)
	return hypot(x, y)
end
function tmp = code(x, y)
	tmp = hypot(x, y);
end
code[x_, y_] := N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	sqrt(((x ^ (2)) + (y ^ (2))))
END code
\mathsf{hypot}\left(x, y\right)
Derivation
  1. Initial program 54.4%

    \[\sqrt{x \cdot x + y \cdot y} \]
  2. Applied rewrites100.0%

    \[\leadsto \mathsf{hypot}\left(x, y\right) \]
  3. Add Preprocessing

Alternative 2: 99.1% accurate, 1.9× speedup?

\[\mathsf{max}\left(\left|x\right|, \left|y\right|\right) \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (fmax (fabs x) (fabs y)))
double code(double x, double y) {
	return fmax(fabs(x), fabs(y));
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = fmax(abs(x), abs(y))
end function
public static double code(double x, double y) {
	return fmax(Math.abs(x), Math.abs(y));
}
def code(x, y):
	return fmax(math.fabs(x), math.fabs(y))
function code(x, y)
	return fmax(abs(x), abs(y))
end
function tmp = code(x, y)
	tmp = max(abs(x), abs(y));
end
code[x_, y_] := N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	LET tmp = IF ((abs(x)) > (abs(y))) THEN (abs(x)) ELSE (abs(y)) ENDIF IN
	tmp
END code
\mathsf{max}\left(\left|x\right|, \left|y\right|\right)
Derivation
  1. Initial program 54.4%

    \[\sqrt{x \cdot x + y \cdot y} \]
  2. Taylor expanded in x around -inf

    \[\leadsto -1 \cdot x \]
  3. Step-by-step derivation
    1. Applied rewrites27.4%

      \[\leadsto -1 \cdot x \]
    2. Step-by-step derivation
      1. Applied rewrites27.4%

        \[\leadsto -x \]
      2. Taylor expanded in undef-var around zero

        \[\leadsto -0 \]
      3. Step-by-step derivation
        1. Applied rewrites2.7%

          \[\leadsto -0 \]
        2. Taylor expanded in y around inf

          \[\leadsto y \]
        3. Step-by-step derivation
          1. Applied rewrites27.4%

            \[\leadsto y \]
          2. Add Preprocessing

          Reproduce

          ?
          herbie shell --seed 2026092 
          (FPCore (x y)
            :name "Data.Octree.Internal:octantDistance  from Octree-0.5.4.2"
            :precision binary64
            (sqrt (+ (* x x) (* y y))))