Average Error: 21.5 → 0.6
Time: 11.3s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.337826701582892488089574244217473576524 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \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}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\

\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 r31464457 = x;
        double r31464458 = r31464457 * r31464457;
        double r31464459 = y;
        double r31464460 = r31464458 + r31464459;
        double r31464461 = sqrt(r31464460);
        return r31464461;
}

double f(double x, double y) {
        double r31464462 = x;
        double r31464463 = -1.3378267015828925e+154;
        bool r31464464 = r31464462 <= r31464463;
        double r31464465 = 0.5;
        double r31464466 = y;
        double r31464467 = r31464466 / r31464462;
        double r31464468 = fma(r31464465, r31464467, r31464462);
        double r31464469 = -r31464468;
        double r31464470 = 1.4171690306065683e+48;
        bool r31464471 = r31464462 <= r31464470;
        double r31464472 = r31464462 * r31464462;
        double r31464473 = r31464472 + r31464466;
        double r31464474 = sqrt(r31464473);
        double r31464475 = r31464471 ? r31464474 : r31464468;
        double r31464476 = r31464464 ? r31464469 : r31464475;
        return r31464476;
}

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. Taylor expanded around -inf 0

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

      \[\leadsto \color{blue}{-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)}\]

    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}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \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)))