Average Error: 31.6 → 18.7
Time: 2.2s
Precision: binary64
\[\sqrt{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \leq -1.8899720103254926 \cdot 10^{+146}:\\ \;\;\;\;\left|x\right|\\ \mathbf{elif}\;y \leq -2.454304521609729 \cdot 10^{-75}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq 8.447014685477577 \cdot 10^{-160}:\\ \;\;\;\;\left|x\right|\\ \mathbf{elif}\;y \leq 1.6336242095797044 \cdot 10^{-143}:\\ \;\;\;\;y\\ \mathbf{elif}\;y \leq 1.3066676791772896 \cdot 10^{-35}:\\ \;\;\;\;\left|x\right|\\ \mathbf{elif}\;y \leq 4.248125126133587 \cdot 10^{+95}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;y\\ \end{array}\]
\sqrt{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \leq -1.8899720103254926 \cdot 10^{+146}:\\
\;\;\;\;\left|x\right|\\

\mathbf{elif}\;y \leq -2.454304521609729 \cdot 10^{-75}:\\
\;\;\;\;\sqrt{x \cdot x + y \cdot y}\\

\mathbf{elif}\;y \leq 8.447014685477577 \cdot 10^{-160}:\\
\;\;\;\;\left|x\right|\\

\mathbf{elif}\;y \leq 1.6336242095797044 \cdot 10^{-143}:\\
\;\;\;\;y\\

\mathbf{elif}\;y \leq 1.3066676791772896 \cdot 10^{-35}:\\
\;\;\;\;\left|x\right|\\

\mathbf{elif}\;y \leq 4.248125126133587 \cdot 10^{+95}:\\
\;\;\;\;\sqrt{x \cdot x + y \cdot y}\\

\mathbf{else}:\\
\;\;\;\;y\\

\end{array}
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -1.8899720103254926e+146)
   (fabs x)
   (if (<= y -2.454304521609729e-75)
     (sqrt (+ (* x x) (* y y)))
     (if (<= y 8.447014685477577e-160)
       (fabs x)
       (if (<= y 1.6336242095797044e-143)
         y
         (if (<= y 1.3066676791772896e-35)
           (fabs x)
           (if (<= y 4.248125126133587e+95) (sqrt (+ (* x x) (* y y))) y)))))))
double code(double x, double y) {
	return sqrt((x * x) + (y * y));
}
double code(double x, double y) {
	double tmp;
	if (y <= -1.8899720103254926e+146) {
		tmp = fabs(x);
	} else if (y <= -2.454304521609729e-75) {
		tmp = sqrt((x * x) + (y * y));
	} else if (y <= 8.447014685477577e-160) {
		tmp = fabs(x);
	} else if (y <= 1.6336242095797044e-143) {
		tmp = y;
	} else if (y <= 1.3066676791772896e-35) {
		tmp = fabs(x);
	} else if (y <= 4.248125126133587e+95) {
		tmp = sqrt((x * x) + (y * y));
	} else {
		tmp = y;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original31.6
Target17.1
Herbie18.7
\[\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}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -1.88997201032549258e146 or -2.4543045216097289e-75 < y < 8.44701468547757748e-160 or 1.6336242095797044e-143 < y < 1.3066676791772896e-35

    1. Initial program 34.3

      \[\sqrt{x \cdot x + y \cdot y}\]
    2. Taylor expanded around 0 40.6

      \[\leadsto \sqrt{\color{blue}{{x}^{2}}}\]
    3. Simplified40.6

      \[\leadsto \sqrt{\color{blue}{x \cdot x}}\]
    4. Using strategy rm
    5. Applied rem-sqrt-square_binary64_1782322.1

      \[\leadsto \color{blue}{\left|x\right|}\]

    if -1.88997201032549258e146 < y < -2.4543045216097289e-75 or 1.3066676791772896e-35 < y < 4.24812512613358684e95

    1. Initial program 15.8

      \[\sqrt{x \cdot x + y \cdot y}\]

    if 8.44701468547757748e-160 < y < 1.6336242095797044e-143 or 4.24812512613358684e95 < y

    1. Initial program 48.1

      \[\sqrt{x \cdot x + y \cdot y}\]
    2. Taylor expanded around 0 13.6

      \[\leadsto \color{blue}{y}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification18.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.8899720103254926 \cdot 10^{+146}:\\ \;\;\;\;\left|x\right|\\ \mathbf{elif}\;y \leq -2.454304521609729 \cdot 10^{-75}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq 8.447014685477577 \cdot 10^{-160}:\\ \;\;\;\;\left|x\right|\\ \mathbf{elif}\;y \leq 1.6336242095797044 \cdot 10^{-143}:\\ \;\;\;\;y\\ \mathbf{elif}\;y \leq 1.3066676791772896 \cdot 10^{-35}:\\ \;\;\;\;\left|x\right|\\ \mathbf{elif}\;y \leq 4.248125126133587 \cdot 10^{+95}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;y\\ \end{array}\]

Reproduce

herbie shell --seed 2020344 
(FPCore (x y)
  :name "Data.Octree.Internal:octantDistance  from Octree-0.5.4.2"
  :precision binary64

  :herbie-target
  (if (< x -1.1236950826599826e+145) (- x) (if (< x 1.116557621183362e+93) (sqrt (+ (* x x) (* y y))) x))

  (sqrt (+ (* x x) (* y y))))