Average Error: 21.6 → 0.2
Time: 19.3s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.3410577693198698 \cdot 10^{+154}:\\ \;\;\;\;\frac{-1}{2} \cdot \frac{y}{x} - x\\ \mathbf{elif}\;x \le 3.9127043430585563 \cdot 10^{+87}:\\ \;\;\;\;\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.3410577693198698 \cdot 10^{+154}:\\
\;\;\;\;\frac{-1}{2} \cdot \frac{y}{x} - x\\

\mathbf{elif}\;x \le 3.9127043430585563 \cdot 10^{+87}:\\
\;\;\;\;\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 r19060164 = x;
        double r19060165 = r19060164 * r19060164;
        double r19060166 = y;
        double r19060167 = r19060165 + r19060166;
        double r19060168 = sqrt(r19060167);
        return r19060168;
}

double f(double x, double y) {
        double r19060169 = x;
        double r19060170 = -1.3410577693198698e+154;
        bool r19060171 = r19060169 <= r19060170;
        double r19060172 = -0.5;
        double r19060173 = y;
        double r19060174 = r19060173 / r19060169;
        double r19060175 = r19060172 * r19060174;
        double r19060176 = r19060175 - r19060169;
        double r19060177 = 3.9127043430585563e+87;
        bool r19060178 = r19060169 <= r19060177;
        double r19060179 = r19060169 * r19060169;
        double r19060180 = r19060179 + r19060173;
        double r19060181 = sqrt(r19060180);
        double r19060182 = 0.5;
        double r19060183 = fma(r19060182, r19060174, r19060169);
        double r19060184 = r19060178 ? r19060181 : r19060183;
        double r19060185 = r19060171 ? r19060176 : r19060184;
        return r19060185;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.6
Target0.5
Herbie0.2
\[\begin{array}{l} \mathbf{if}\;x \lt -1.5097698010472593 \cdot 10^{+153}:\\ \;\;\;\;-\left(0.5 \cdot \frac{y}{x} + x\right)\\ \mathbf{elif}\;x \lt 5.582399551122541 \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.3410577693198698e+154

    1. Initial program 64.0

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

      \[\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.3410577693198698e+154 < x < 3.9127043430585563e+87

    1. Initial program 0.0

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

    if 3.9127043430585563e+87 < x

    1. Initial program 45.4

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

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

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

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

Reproduce

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