Average Error: 21.4 → 0.0
Time: 4.8s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.336913857166639347415301363669115809283 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{\frac{1}{2}}{x}, y, x\right)\\ \mathbf{elif}\;x \le 1.574212501990470180534105460064927660599 \cdot 10^{131}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\ \end{array}\]
\sqrt{x \cdot x + y}
\begin{array}{l}
\mathbf{if}\;x \le -1.336913857166639347415301363669115809283 \cdot 10^{154}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{\frac{1}{2}}{x}, y, x\right)\\

\mathbf{elif}\;x \le 1.574212501990470180534105460064927660599 \cdot 10^{131}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\

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

\end{array}
double f(double x, double y) {
        double r1277676 = x;
        double r1277677 = r1277676 * r1277676;
        double r1277678 = y;
        double r1277679 = r1277677 + r1277678;
        double r1277680 = sqrt(r1277679);
        return r1277680;
}

double f(double x, double y) {
        double r1277681 = x;
        double r1277682 = -1.3369138571666393e+154;
        bool r1277683 = r1277681 <= r1277682;
        double r1277684 = 0.5;
        double r1277685 = r1277684 / r1277681;
        double r1277686 = y;
        double r1277687 = fma(r1277685, r1277686, r1277681);
        double r1277688 = -r1277687;
        double r1277689 = 1.5742125019904702e+131;
        bool r1277690 = r1277681 <= r1277689;
        double r1277691 = fma(r1277681, r1277681, r1277686);
        double r1277692 = sqrt(r1277691);
        double r1277693 = r1277686 / r1277681;
        double r1277694 = fma(r1277693, r1277684, r1277681);
        double r1277695 = r1277690 ? r1277692 : r1277694;
        double r1277696 = r1277683 ? r1277688 : r1277695;
        return r1277696;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.4
Target0.4
Herbie0.0
\[\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.3369138571666393e+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}{-\mathsf{fma}\left(\frac{\frac{1}{2}}{x}, y, x\right)}\]

    if -1.3369138571666393e+154 < x < 1.5742125019904702e+131

    1. Initial program 0.0

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

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}}\]

    if 1.5742125019904702e+131 < x

    1. Initial program 55.8

      \[\sqrt{x \cdot x + y}\]
    2. Simplified55.8

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

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

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

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

Reproduce

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