Average Error: 21.5 → 0.6
Time: 9.1s
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 r308177 = x;
        double r308178 = r308177 * r308177;
        double r308179 = y;
        double r308180 = r308178 + r308179;
        double r308181 = sqrt(r308180);
        return r308181;
}

double f(double x, double y) {
        double r308182 = x;
        double r308183 = -1.3378267015828925e+154;
        bool r308184 = r308182 <= r308183;
        double r308185 = 0.5;
        double r308186 = y;
        double r308187 = r308186 / r308182;
        double r308188 = fma(r308185, r308187, r308182);
        double r308189 = -r308188;
        double r308190 = 1.4171690306065683e+48;
        bool r308191 = r308182 <= r308190;
        double r308192 = r308182 * r308182;
        double r308193 = r308192 + r308186;
        double r308194 = sqrt(r308193);
        double r308195 = r308191 ? r308194 : r308188;
        double r308196 = r308184 ? r308189 : r308195;
        return r308196;
}

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)))