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

\mathbf{elif}\;x \le 2.027374157723114338036480360220146126719 \cdot 10^{123}:\\
\;\;\;\;\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 r492424 = x;
        double r492425 = r492424 * r492424;
        double r492426 = y;
        double r492427 = r492425 + r492426;
        double r492428 = sqrt(r492427);
        return r492428;
}

double f(double x, double y) {
        double r492429 = x;
        double r492430 = -1.3199717282643271e+154;
        bool r492431 = r492429 <= r492430;
        double r492432 = 0.5;
        double r492433 = y;
        double r492434 = r492433 / r492429;
        double r492435 = r492432 * r492434;
        double r492436 = r492429 + r492435;
        double r492437 = -r492436;
        double r492438 = 2.0273741577231143e+123;
        bool r492439 = r492429 <= r492438;
        double r492440 = r492429 * r492429;
        double r492441 = r492440 + r492433;
        double r492442 = sqrt(r492441);
        double r492443 = fma(r492432, r492434, r492429);
        double r492444 = r492439 ? r492442 : r492443;
        double r492445 = r492431 ? r492437 : r492444;
        return r492445;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.4
Target0.5
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.3199717282643271e+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.3199717282643271e+154 < x < 2.0273741577231143e+123

    1. Initial program 0.0

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

    if 2.0273741577231143e+123 < x

    1. Initial program 54.3

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

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

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

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

Reproduce

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