Average Error: 31.6 → 18.8
Time: 1.2s
Precision: binary64
\[\sqrt{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;x \leq -6.518081105388684 \cdot 10^{+114}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -4.142924173025851 \cdot 10^{-210}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{elif}\;x \leq 6.004365564520266 \cdot 10^{-207}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 42148.37987214342:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]
\sqrt{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;x \leq -6.518081105388684 \cdot 10^{+114}:\\
\;\;\;\;-x\\

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

\mathbf{elif}\;x \leq 6.004365564520266 \cdot 10^{-207}:\\
\;\;\;\;y\\

\mathbf{elif}\;x \leq 42148.37987214342:\\
\;\;\;\;\sqrt{x \cdot x + y \cdot y}\\

\mathbf{else}:\\
\;\;\;\;x\\

\end{array}
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= x -6.518081105388684e+114)
   (- x)
   (if (<= x -4.142924173025851e-210)
     (sqrt (+ (* x x) (* y y)))
     (if (<= x 6.004365564520266e-207)
       y
       (if (<= x 42148.37987214342) (sqrt (+ (* x x) (* y y))) x)))))
double code(double x, double y) {
	return sqrt((x * x) + (y * y));
}
double code(double x, double y) {
	double tmp;
	if (x <= -6.518081105388684e+114) {
		tmp = -x;
	} else if (x <= -4.142924173025851e-210) {
		tmp = sqrt((x * x) + (y * y));
	} else if (x <= 6.004365564520266e-207) {
		tmp = y;
	} else if (x <= 42148.37987214342) {
		tmp = sqrt((x * x) + (y * y));
	} else {
		tmp = x;
	}
	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.7
Herbie18.8
\[\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 4 regimes
  2. if x < -6.51808110538868361e114

    1. Initial program 53.7

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

      \[\leadsto \color{blue}{-1 \cdot x}\]
    3. Simplified10.5

      \[\leadsto \color{blue}{-x}\]

    if -6.51808110538868361e114 < x < -4.1429241730258509e-210 or 6.00436556452026608e-207 < x < 42148.3798721434214

    1. Initial program 18.8

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

    if -4.1429241730258509e-210 < x < 6.00436556452026608e-207

    1. Initial program 30.0

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

      \[\leadsto \color{blue}{y}\]

    if 42148.3798721434214 < x

    1. Initial program 40.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -6.518081105388684 \cdot 10^{+114}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq -4.142924173025851 \cdot 10^{-210}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{elif}\;x \leq 6.004365564520266 \cdot 10^{-207}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \leq 42148.37987214342:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]

Reproduce

herbie shell --seed 2020231 
(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))))