Average Error: 21.5 → 0.6
Time: 12.2s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.337826701582892488089574244217473576524 \cdot 10^{154}:\\ \;\;\;\;\frac{\frac{-1}{2} \cdot y}{x} - x\\ \mathbf{elif}\;x \le 1.417169030606568251689508094083542147075 \cdot 10^{48}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \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.337826701582892488089574244217473576524 \cdot 10^{154}:\\
\;\;\;\;\frac{\frac{-1}{2} \cdot y}{x} - x\\

\mathbf{elif}\;x \le 1.417169030606568251689508094083542147075 \cdot 10^{48}:\\
\;\;\;\;\sqrt{x \cdot x + y}\\

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

\end{array}
double f(double x, double y) {
        double r21193275 = x;
        double r21193276 = r21193275 * r21193275;
        double r21193277 = y;
        double r21193278 = r21193276 + r21193277;
        double r21193279 = sqrt(r21193278);
        return r21193279;
}

double f(double x, double y) {
        double r21193280 = x;
        double r21193281 = -1.3378267015828925e+154;
        bool r21193282 = r21193280 <= r21193281;
        double r21193283 = -0.5;
        double r21193284 = y;
        double r21193285 = r21193283 * r21193284;
        double r21193286 = r21193285 / r21193280;
        double r21193287 = r21193286 - r21193280;
        double r21193288 = 1.4171690306065683e+48;
        bool r21193289 = r21193280 <= r21193288;
        double r21193290 = r21193280 * r21193280;
        double r21193291 = r21193290 + r21193284;
        double r21193292 = sqrt(r21193291);
        double r21193293 = 0.5;
        double r21193294 = r21193284 / r21193280;
        double r21193295 = fma(r21193293, r21193294, r21193280);
        double r21193296 = r21193289 ? r21193292 : r21193295;
        double r21193297 = r21193282 ? r21193287 : r21193296;
        return r21193297;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.5
Target0.5
Herbie0.6
\[\begin{array}{l} \mathbf{if}\;x \lt -1.509769801047259255153812752081023359759 \cdot 10^{153}:\\ \;\;\;\;-\left(0.5 \cdot \frac{y}{x} + x\right)\\ \mathbf{elif}\;x \lt 5.582399551122540716781541767466805967807 \cdot 10^{57}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{x} + x\\ \end{array}\]

Derivation

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

    1. Initial program 64.0

      \[\sqrt{x \cdot x + y}\]
    2. Using strategy rm
    3. Applied add-cbrt-cube64.0

      \[\leadsto \color{blue}{\sqrt[3]{\left(\sqrt{x \cdot x + y} \cdot \sqrt{x \cdot x + y}\right) \cdot \sqrt{x \cdot x + y}}}\]
    4. Simplified64.0

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

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

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

    if -1.3378267015828925e+154 < x < 1.4171690306065683e+48

    1. Initial program 0.0

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

    if 1.4171690306065683e+48 < x

    1. Initial program 39.0

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

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

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

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

Reproduce

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

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

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