Average Error: 19.7 → 0.1
Time: 2.4s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.3377295553932065 \cdot 10^{+154}:\\ \;\;\;\;\frac{y}{x} \cdot \frac{-1}{2} - x\\ \mathbf{elif}\;x \le 1.7307959125595267 \cdot 10^{+118}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \end{array}\]
\sqrt{x \cdot x + y}
\begin{array}{l}
\mathbf{if}\;x \le -1.3377295553932065 \cdot 10^{+154}:\\
\;\;\;\;\frac{y}{x} \cdot \frac{-1}{2} - x\\

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

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

\end{array}
double f(double x, double y) {
        double r8395325 = x;
        double r8395326 = r8395325 * r8395325;
        double r8395327 = y;
        double r8395328 = r8395326 + r8395327;
        double r8395329 = sqrt(r8395328);
        return r8395329;
}

double f(double x, double y) {
        double r8395330 = x;
        double r8395331 = -1.3377295553932065e+154;
        bool r8395332 = r8395330 <= r8395331;
        double r8395333 = y;
        double r8395334 = r8395333 / r8395330;
        double r8395335 = -0.5;
        double r8395336 = r8395334 * r8395335;
        double r8395337 = r8395336 - r8395330;
        double r8395338 = 1.7307959125595267e+118;
        bool r8395339 = r8395330 <= r8395338;
        double r8395340 = fma(r8395330, r8395330, r8395333);
        double r8395341 = sqrt(r8395340);
        double r8395342 = 0.5;
        double r8395343 = fma(r8395342, r8395334, r8395330);
        double r8395344 = r8395339 ? r8395341 : r8395343;
        double r8395345 = r8395332 ? r8395337 : r8395344;
        return r8395345;
}

Error

Bits error versus x

Bits error versus y

Target

Original19.7
Target0.4
Herbie0.1
\[\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.3377295553932065e+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{y}{x} \cdot \frac{-1}{2} - x}\]

    if -1.3377295553932065e+154 < x < 1.7307959125595267e+118

    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.7307959125595267e+118 < x

    1. Initial program 49.0

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

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

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

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

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

Reproduce

herbie shell --seed 2019156 +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)))