Average Error: 20.7 → 1.5
Time: 1.5s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.3390824056870139 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 4.608597005595938 \cdot 10^{-20}:\\ \;\;\;\;\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.3390824056870139 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 4.608597005595938 \cdot 10^{-20}:\\
\;\;\;\;\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 r664422 = x;
        double r664423 = r664422 * r664422;
        double r664424 = y;
        double r664425 = r664423 + r664424;
        double r664426 = sqrt(r664425);
        return r664426;
}

double f(double x, double y) {
        double r664427 = x;
        double r664428 = -1.3390824056870139e+154;
        bool r664429 = r664427 <= r664428;
        double r664430 = 0.5;
        double r664431 = y;
        double r664432 = r664431 / r664427;
        double r664433 = r664430 * r664432;
        double r664434 = r664427 + r664433;
        double r664435 = -r664434;
        double r664436 = 4.6085970055959376e-20;
        bool r664437 = r664427 <= r664436;
        double r664438 = r664427 * r664427;
        double r664439 = r664438 + r664431;
        double r664440 = sqrt(r664439);
        double r664441 = fma(r664430, r664432, r664427);
        double r664442 = r664437 ? r664440 : r664441;
        double r664443 = r664429 ? r664435 : r664442;
        return r664443;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.7
Target0.5
Herbie1.5
\[\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.5823995511225407 \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.3390824056870139e+154

    1. Initial program 64.0

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

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

    if -1.3390824056870139e+154 < x < 4.6085970055959376e-20

    1. Initial program 0.0

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

    if 4.6085970055959376e-20 < x

    1. Initial program 31.9

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

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

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

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

Reproduce

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