Average Error: 21.7 → 0.2
Time: 16.9s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.3300132858515627319920499059244220404 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \mathbf{elif}\;x \le 8.331092342254926651866005497662461340443 \cdot 10^{95}:\\ \;\;\;\;\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.3300132858515627319920499059244220404 \cdot 10^{154}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\

\mathbf{elif}\;x \le 8.331092342254926651866005497662461340443 \cdot 10^{95}:\\
\;\;\;\;\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 r347881 = x;
        double r347882 = r347881 * r347881;
        double r347883 = y;
        double r347884 = r347882 + r347883;
        double r347885 = sqrt(r347884);
        return r347885;
}

double f(double x, double y) {
        double r347886 = x;
        double r347887 = -1.3300132858515627e+154;
        bool r347888 = r347886 <= r347887;
        double r347889 = 0.5;
        double r347890 = y;
        double r347891 = r347890 / r347886;
        double r347892 = fma(r347889, r347891, r347886);
        double r347893 = -r347892;
        double r347894 = 8.331092342254927e+95;
        bool r347895 = r347886 <= r347894;
        double r347896 = r347886 * r347886;
        double r347897 = r347896 + r347890;
        double r347898 = sqrt(r347897);
        double r347899 = r347895 ? r347898 : r347892;
        double r347900 = r347888 ? r347893 : r347899;
        return r347900;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.7
Target0.5
Herbie0.2
\[\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.3300132858515627e+154

    1. Initial program 64.0

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

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

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

    if -1.3300132858515627e+154 < x < 8.331092342254927e+95

    1. Initial program 0.0

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

    if 8.331092342254927e+95 < x

    1. Initial program 46.8

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

      \[\leadsto \color{blue}{x + \frac{1}{2} \cdot \frac{y}{x}}\]
    3. Simplified1.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.2

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

Reproduce

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

  :herbie-target
  (if (< x -1.5097698010472593e153) (- (+ (* 0.5 (/ y x)) x)) (if (< x 5.5823995511225407e57) (sqrt (+ (* x x) y)) (+ (* 0.5 (/ y x)) x)))

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