Average Error: 21.1 → 0.3
Time: 1.2s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.298475003874099941155891200323427793939 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 2.116552450926022022585758234935512177514 \cdot 10^{82}:\\ \;\;\;\;\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.298475003874099941155891200323427793939 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 2.116552450926022022585758234935512177514 \cdot 10^{82}:\\
\;\;\;\;\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 r472684 = x;
        double r472685 = r472684 * r472684;
        double r472686 = y;
        double r472687 = r472685 + r472686;
        double r472688 = sqrt(r472687);
        return r472688;
}

double f(double x, double y) {
        double r472689 = x;
        double r472690 = -1.2984750038741e+154;
        bool r472691 = r472689 <= r472690;
        double r472692 = 0.5;
        double r472693 = y;
        double r472694 = r472693 / r472689;
        double r472695 = r472692 * r472694;
        double r472696 = r472689 + r472695;
        double r472697 = -r472696;
        double r472698 = 2.116552450926022e+82;
        bool r472699 = r472689 <= r472698;
        double r472700 = r472689 * r472689;
        double r472701 = r472700 + r472693;
        double r472702 = sqrt(r472701);
        double r472703 = fma(r472692, r472694, r472689);
        double r472704 = r472699 ? r472702 : r472703;
        double r472705 = r472691 ? r472697 : r472704;
        return r472705;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.1
Target0.5
Herbie0.3
\[\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.2984750038741e+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)}\]

    if -1.2984750038741e+154 < x < 2.116552450926022e+82

    1. Initial program 0.0

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

    if 2.116552450926022e+82 < x

    1. Initial program 44.6

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

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

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

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

Reproduce

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

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