Average Error: 19.8 → 0.7
Time: 8.6s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.3323185489366894 \cdot 10^{+154}:\\ \;\;\;\;\frac{-1}{2} \cdot \frac{y}{x} - x\\ \mathbf{elif}\;x \le 1.3070827329489974 \cdot 10^{+38}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\ \end{array}\]
\sqrt{x \cdot x + y}
\begin{array}{l}
\mathbf{if}\;x \le -1.3323185489366894 \cdot 10^{+154}:\\
\;\;\;\;\frac{-1}{2} \cdot \frac{y}{x} - x\\

\mathbf{elif}\;x \le 1.3070827329489974 \cdot 10^{+38}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\

\end{array}
double f(double x, double y) {
        double r19972353 = x;
        double r19972354 = r19972353 * r19972353;
        double r19972355 = y;
        double r19972356 = r19972354 + r19972355;
        double r19972357 = sqrt(r19972356);
        return r19972357;
}

double f(double x, double y) {
        double r19972358 = x;
        double r19972359 = -1.3323185489366894e+154;
        bool r19972360 = r19972358 <= r19972359;
        double r19972361 = -0.5;
        double r19972362 = y;
        double r19972363 = r19972362 / r19972358;
        double r19972364 = r19972361 * r19972363;
        double r19972365 = r19972364 - r19972358;
        double r19972366 = 1.3070827329489974e+38;
        bool r19972367 = r19972358 <= r19972366;
        double r19972368 = fma(r19972358, r19972358, r19972362);
        double r19972369 = sqrt(r19972368);
        double r19972370 = 0.5;
        double r19972371 = fma(r19972363, r19972370, r19972358);
        double r19972372 = r19972367 ? r19972369 : r19972371;
        double r19972373 = r19972360 ? r19972365 : r19972372;
        return r19972373;
}

Error

Bits error versus x

Bits error versus y

Target

Original19.8
Target0.5
Herbie0.7
\[\begin{array}{l} \mathbf{if}\;x \lt -1.5097698010472593 \cdot 10^{+153}:\\ \;\;\;\;-\left(\frac{1}{2} \cdot \frac{y}{x} + x\right)\\ \mathbf{elif}\;x \lt 5.582399551122541 \cdot 10^{+57}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2} \cdot \frac{y}{x} + x\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if x < -1.3323185489366894e+154

    1. Initial program 59.6

      \[\sqrt{x \cdot x + y}\]
    2. Simplified59.6

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}}\]
    3. Taylor expanded around -inf 0

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

      \[\leadsto \color{blue}{\frac{-1}{2} \cdot \frac{y}{x} - x}\]

    if -1.3323185489366894e+154 < x < 1.3070827329489974e+38

    1. Initial program 0.0

      \[\sqrt{x \cdot x + y}\]
    2. Simplified0.0

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

    if 1.3070827329489974e+38 < x

    1. Initial program 36.4

      \[\sqrt{x \cdot x + y}\]
    2. Simplified36.4

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}}\]
    3. Taylor expanded around inf 2.6

      \[\leadsto \color{blue}{x + \frac{1}{2} \cdot \frac{y}{x}}\]
    4. Simplified2.6

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -1.3323185489366894 \cdot 10^{+154}:\\ \;\;\;\;\frac{-1}{2} \cdot \frac{y}{x} - x\\ \mathbf{elif}\;x \le 1.3070827329489974 \cdot 10^{+38}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019163 +o rules:numerics
(FPCore (x y)
  :name "Linear.Quaternion:$clog from linear-1.19.1.3"

  :herbie-target
  (if (< x -1.5097698010472593e+153) (- (+ (* 1/2 (/ y x)) x)) (if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) (+ (* 1/2 (/ y x)) x)))

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