Data.Octree.Internal:octantDistance from Octree-0.5.4.2

Percentage Accurate: 54.6% → 100.0%
Time: 20.4s
Alternatives: 7
Speedup: 0.2×

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 7 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.6% 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: 100.0% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \mathsf{hypot}\left(y, x\right) \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}
Derivation
  1. Initial program 51.6%

    \[\sqrt{x \cdot x + y \cdot y} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{x \cdot x + y \cdot y}} \]
    2. lift-+.f64N/A

      \[\leadsto \sqrt{\color{blue}{x \cdot x + y \cdot y}} \]
    3. +-commutativeN/A

      \[\leadsto \sqrt{\color{blue}{y \cdot y + x \cdot x}} \]
    4. lift-*.f64N/A

      \[\leadsto \sqrt{\color{blue}{y \cdot y} + x \cdot x} \]
    5. lift-*.f64N/A

      \[\leadsto \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]
    6. lower-hypot.f64100.0

      \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
  4. Applied rewrites100.0%

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

Alternative 2: 48.8% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq 0 \lor \neg \left(y \cdot y \leq 5 \cdot 10^{+300}\right):\\ \;\;\;\;\frac{y}{x} \cdot x\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, x \cdot x\right)}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (or (<= (* y y) 0.0) (not (<= (* y y) 5e+300)))
   (* (/ y x) x)
   (sqrt (fma y y (* x x)))))
double code(double x, double y) {
	double tmp;
	if (((y * y) <= 0.0) || !((y * y) <= 5e+300)) {
		tmp = (y / x) * x;
	} else {
		tmp = sqrt(fma(y, y, (x * x)));
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if ((Float64(y * y) <= 0.0) || !(Float64(y * y) <= 5e+300))
		tmp = Float64(Float64(y / x) * x);
	else
		tmp = sqrt(fma(y, y, Float64(x * x)));
	end
	return tmp
end
code[x_, y_] := If[Or[LessEqual[N[(y * y), $MachinePrecision], 0.0], N[Not[LessEqual[N[(y * y), $MachinePrecision], 5e+300]], $MachinePrecision]], N[(N[(y / x), $MachinePrecision] * x), $MachinePrecision], N[Sqrt[N[(y * y + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 0 \lor \neg \left(y \cdot y \leq 5 \cdot 10^{+300}\right):\\
\;\;\;\;\frac{y}{x} \cdot x\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(y, y, x \cdot x\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 0.0 or 5.00000000000000026e300 < (*.f64 y y)

    1. Initial program 25.4%

      \[\sqrt{x \cdot x + y \cdot y} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{x \cdot x + y \cdot y}} \]
      2. lift-+.f64N/A

        \[\leadsto \sqrt{\color{blue}{x \cdot x + y \cdot y}} \]
      3. +-commutativeN/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y + x \cdot x}} \]
      4. lift-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{y \cdot y} + x \cdot x} \]
      5. lift-*.f64N/A

        \[\leadsto \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]
      6. lower-hypot.f64100.0

        \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
    4. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
    5. Taylor expanded in x around 0

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2}}{y}}, {x}^{2}, y\right) \]
      9. unpow2N/A

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)} \]
    8. Taylor expanded in x around inf

      \[\leadsto {x}^{2} \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y} + \frac{y}{{x}^{2}}\right)} \]
    9. Step-by-step derivation
      1. Applied rewrites14.1%

        \[\leadsto \left(\left(\frac{\frac{y}{x}}{x} + \frac{0.5}{y}\right) \cdot x\right) \cdot \color{blue}{x} \]
      2. Taylor expanded in x around 0

        \[\leadsto \frac{y}{x} \cdot x \]
      3. Step-by-step derivation
        1. Applied rewrites18.8%

          \[\leadsto \frac{y}{x} \cdot x \]

        if 0.0 < (*.f64 y y) < 5.00000000000000026e300

        1. Initial program 76.6%

          \[\sqrt{x \cdot x + y \cdot y} \]
        2. Add Preprocessing
        3. Step-by-step derivation
          1. lift-+.f64N/A

            \[\leadsto \sqrt{\color{blue}{x \cdot x + y \cdot y}} \]
          2. +-commutativeN/A

            \[\leadsto \sqrt{\color{blue}{y \cdot y + x \cdot x}} \]
          3. lift-*.f64N/A

            \[\leadsto \sqrt{\color{blue}{y \cdot y} + x \cdot x} \]
          4. lower-fma.f6476.6

            \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(y, y, x \cdot x\right)}} \]
        4. Applied rewrites76.6%

          \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(y, y, x \cdot x\right)}} \]
      4. Recombined 2 regimes into one program.
      5. Final simplification48.4%

        \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 0 \lor \neg \left(y \cdot y \leq 5 \cdot 10^{+300}\right):\\ \;\;\;\;\frac{y}{x} \cdot x\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, x \cdot x\right)}\\ \end{array} \]
      6. Add Preprocessing

      Alternative 3: 35.6% accurate, 0.6× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq 0 \lor \neg \left(y \cdot y \leq 5 \cdot 10^{+300}\right):\\ \;\;\;\;\frac{y}{x} \cdot x\\ \mathbf{else}:\\ \;\;\;\;\sqrt{y \cdot y}\\ \end{array} \end{array} \]
      (FPCore (x y)
       :precision binary64
       (if (or (<= (* y y) 0.0) (not (<= (* y y) 5e+300)))
         (* (/ y x) x)
         (sqrt (* y y))))
      double code(double x, double y) {
      	double tmp;
      	if (((y * y) <= 0.0) || !((y * y) <= 5e+300)) {
      		tmp = (y / x) * x;
      	} else {
      		tmp = sqrt((y * y));
      	}
      	return tmp;
      }
      
      real(8) function code(x, y)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8) :: tmp
          if (((y * y) <= 0.0d0) .or. (.not. ((y * y) <= 5d+300))) then
              tmp = (y / x) * x
          else
              tmp = sqrt((y * y))
          end if
          code = tmp
      end function
      
      public static double code(double x, double y) {
      	double tmp;
      	if (((y * y) <= 0.0) || !((y * y) <= 5e+300)) {
      		tmp = (y / x) * x;
      	} else {
      		tmp = Math.sqrt((y * y));
      	}
      	return tmp;
      }
      
      def code(x, y):
      	tmp = 0
      	if ((y * y) <= 0.0) or not ((y * y) <= 5e+300):
      		tmp = (y / x) * x
      	else:
      		tmp = math.sqrt((y * y))
      	return tmp
      
      function code(x, y)
      	tmp = 0.0
      	if ((Float64(y * y) <= 0.0) || !(Float64(y * y) <= 5e+300))
      		tmp = Float64(Float64(y / x) * x);
      	else
      		tmp = sqrt(Float64(y * y));
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y)
      	tmp = 0.0;
      	if (((y * y) <= 0.0) || ~(((y * y) <= 5e+300)))
      		tmp = (y / x) * x;
      	else
      		tmp = sqrt((y * y));
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := If[Or[LessEqual[N[(y * y), $MachinePrecision], 0.0], N[Not[LessEqual[N[(y * y), $MachinePrecision], 5e+300]], $MachinePrecision]], N[(N[(y / x), $MachinePrecision] * x), $MachinePrecision], N[Sqrt[N[(y * y), $MachinePrecision]], $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;y \cdot y \leq 0 \lor \neg \left(y \cdot y \leq 5 \cdot 10^{+300}\right):\\
      \;\;\;\;\frac{y}{x} \cdot x\\
      
      \mathbf{else}:\\
      \;\;\;\;\sqrt{y \cdot y}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if (*.f64 y y) < 0.0 or 5.00000000000000026e300 < (*.f64 y y)

        1. Initial program 25.4%

          \[\sqrt{x \cdot x + y \cdot y} \]
        2. Add Preprocessing
        3. Step-by-step derivation
          1. lift-sqrt.f64N/A

            \[\leadsto \color{blue}{\sqrt{x \cdot x + y \cdot y}} \]
          2. lift-+.f64N/A

            \[\leadsto \sqrt{\color{blue}{x \cdot x + y \cdot y}} \]
          3. +-commutativeN/A

            \[\leadsto \sqrt{\color{blue}{y \cdot y + x \cdot x}} \]
          4. lift-*.f64N/A

            \[\leadsto \sqrt{\color{blue}{y \cdot y} + x \cdot x} \]
          5. lift-*.f64N/A

            \[\leadsto \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]
          6. lower-hypot.f64100.0

            \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
        4. Applied rewrites100.0%

          \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
        5. Taylor expanded in x around 0

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

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

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

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

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

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

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

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

            \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2}}{y}}, {x}^{2}, y\right) \]
          9. unpow2N/A

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

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

          \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)} \]
        8. Taylor expanded in x around inf

          \[\leadsto {x}^{2} \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y} + \frac{y}{{x}^{2}}\right)} \]
        9. Step-by-step derivation
          1. Applied rewrites14.1%

            \[\leadsto \left(\left(\frac{\frac{y}{x}}{x} + \frac{0.5}{y}\right) \cdot x\right) \cdot \color{blue}{x} \]
          2. Taylor expanded in x around 0

            \[\leadsto \frac{y}{x} \cdot x \]
          3. Step-by-step derivation
            1. Applied rewrites18.8%

              \[\leadsto \frac{y}{x} \cdot x \]

            if 0.0 < (*.f64 y y) < 5.00000000000000026e300

            1. Initial program 76.6%

              \[\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-*.f6457.1

                \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
            5. Applied rewrites57.1%

              \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
          4. Recombined 2 regimes into one program.
          5. Final simplification38.4%

            \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 0 \lor \neg \left(y \cdot y \leq 5 \cdot 10^{+300}\right):\\ \;\;\;\;\frac{y}{x} \cdot x\\ \mathbf{else}:\\ \;\;\;\;\sqrt{y \cdot y}\\ \end{array} \]
          6. Add Preprocessing

          Alternative 4: 26.6% accurate, 0.7× speedup?

          \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 2 \cdot 10^{+175}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} \cdot x\\ \end{array} \end{array} \]
          (FPCore (x y)
           :precision binary64
           (if (<= (* x x) 2e+175) (fma (/ 0.5 y) (* x x) y) (* (/ y x) x)))
          double code(double x, double y) {
          	double tmp;
          	if ((x * x) <= 2e+175) {
          		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) <= 2e+175)
          		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], 2e+175], 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 2 \cdot 10^{+175}:\\
          \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;\frac{y}{x} \cdot x\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if (*.f64 x x) < 1.9999999999999999e175

            1. Initial program 68.9%

              \[\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. lower-fma.f64N/A

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

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

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

                \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2}}{y}}, {x}^{2}, y\right) \]
              9. unpow2N/A

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

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

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

            if 1.9999999999999999e175 < (*.f64 x x)

            1. Initial program 24.1%

              \[\sqrt{x \cdot x + y \cdot y} \]
            2. Add Preprocessing
            3. Step-by-step derivation
              1. lift-sqrt.f64N/A

                \[\leadsto \color{blue}{\sqrt{x \cdot x + y \cdot y}} \]
              2. lift-+.f64N/A

                \[\leadsto \sqrt{\color{blue}{x \cdot x + y \cdot y}} \]
              3. +-commutativeN/A

                \[\leadsto \sqrt{\color{blue}{y \cdot y + x \cdot x}} \]
              4. lift-*.f64N/A

                \[\leadsto \sqrt{\color{blue}{y \cdot y} + x \cdot x} \]
              5. lift-*.f64N/A

                \[\leadsto \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]
              6. lower-hypot.f64100.0

                \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
            4. Applied rewrites100.0%

              \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
            5. Taylor expanded in x around 0

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

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

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

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2}}{y}}, {x}^{2}, y\right) \]
              9. unpow2N/A

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

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

              \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)} \]
            8. Taylor expanded in x around inf

              \[\leadsto {x}^{2} \cdot \color{blue}{\left(\frac{1}{2} \cdot \frac{1}{y} + \frac{y}{{x}^{2}}\right)} \]
            9. Step-by-step derivation
              1. Applied rewrites13.4%

                \[\leadsto \left(\left(\frac{\frac{y}{x}}{x} + \frac{0.5}{y}\right) \cdot x\right) \cdot \color{blue}{x} \]
              2. Taylor expanded in x around 0

                \[\leadsto \frac{y}{x} \cdot x \]
              3. Step-by-step derivation
                1. Applied rewrites12.7%

                  \[\leadsto \frac{y}{x} \cdot x \]
              4. Recombined 2 regimes into one program.
              5. Final simplification27.8%

                \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 2 \cdot 10^{+175}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} \cdot x\\ \end{array} \]
              6. Add Preprocessing

              Alternative 5: 26.9% accurate, 1.0× speedup?

              \[\begin{array}{l} \\ \mathsf{fma}\left(\frac{0.5}{y} \cdot x, x, y\right) \end{array} \]
              (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}
              
              Derivation
              1. Initial program 51.6%

                \[\sqrt{x \cdot x + y \cdot y} \]
              2. Add Preprocessing
              3. Step-by-step derivation
                1. lift-sqrt.f64N/A

                  \[\leadsto \color{blue}{\sqrt{x \cdot x + y \cdot y}} \]
                2. lift-+.f64N/A

                  \[\leadsto \sqrt{\color{blue}{x \cdot x + y \cdot y}} \]
                3. +-commutativeN/A

                  \[\leadsto \sqrt{\color{blue}{y \cdot y + x \cdot x}} \]
                4. lift-*.f64N/A

                  \[\leadsto \sqrt{\color{blue}{y \cdot y} + x \cdot x} \]
                5. lift-*.f64N/A

                  \[\leadsto \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]
                6. lower-hypot.f64100.0

                  \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
              4. Applied rewrites100.0%

                \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
              5. Taylor expanded in x around 0

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

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

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

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

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

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

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

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

                  \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2}}{y}}, {x}^{2}, y\right) \]
                9. unpow2N/A

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

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

                \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{0.5}{y}, x \cdot x, y\right)} \]
              8. Step-by-step derivation
                1. Applied rewrites28.1%

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

                Alternative 6: 29.6% accurate, 1.5× speedup?

                \[\begin{array}{l} \\ \sqrt{y \cdot y} \end{array} \]
                (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}
                
                Derivation
                1. Initial program 51.6%

                  \[\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-*.f6432.2

                    \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
                5. Applied rewrites32.2%

                  \[\leadsto \sqrt{\color{blue}{y \cdot y}} \]
                6. Add Preprocessing

                Alternative 7: 26.6% accurate, 8.0× speedup?

                \[\begin{array}{l} \\ -x \end{array} \]
                (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}
                
                Derivation
                1. Initial program 51.6%

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

                  \[\leadsto \color{blue}{-1 \cdot x} \]
                4. Step-by-step derivation
                  1. mul-1-negN/A

                    \[\leadsto \color{blue}{\mathsf{neg}\left(x\right)} \]
                  2. lower-neg.f6428.4

                    \[\leadsto \color{blue}{-x} \]
                5. Applied rewrites28.4%

                  \[\leadsto \color{blue}{-x} \]
                6. Final simplification28.4%

                  \[\leadsto -x \]
                7. Add Preprocessing

                Developer Target 1: 74.7% 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 2024321 
                (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))))