Average Error: 21.1 → 0.0
Time: 7.3s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.334706067250781795686802411753971031003 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\ \mathbf{elif}\;x \le 1.439938037416395822463756229119736498067 \cdot 10^{138}:\\ \;\;\;\;\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.334706067250781795686802411753971031003 \cdot 10^{154}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\

\mathbf{elif}\;x \le 1.439938037416395822463756229119736498067 \cdot 10^{138}:\\
\;\;\;\;\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 r691478 = x;
        double r691479 = r691478 * r691478;
        double r691480 = y;
        double r691481 = r691479 + r691480;
        double r691482 = sqrt(r691481);
        return r691482;
}

double f(double x, double y) {
        double r691483 = x;
        double r691484 = -1.3347060672507818e+154;
        bool r691485 = r691483 <= r691484;
        double r691486 = y;
        double r691487 = r691486 / r691483;
        double r691488 = 0.5;
        double r691489 = fma(r691487, r691488, r691483);
        double r691490 = -r691489;
        double r691491 = 1.4399380374163958e+138;
        bool r691492 = r691483 <= r691491;
        double r691493 = fma(r691483, r691483, r691486);
        double r691494 = sqrt(r691493);
        double r691495 = r691492 ? r691494 : r691489;
        double r691496 = r691485 ? r691490 : r691495;
        return r691496;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.1
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.3347060672507818e+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{y}{x}, \frac{1}{2}, x\right)}\]

    if -1.3347060672507818e+154 < x < 1.4399380374163958e+138

    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.4399380374163958e+138 < x

    1. Initial program 58.2

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

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

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

      \[\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.334706067250781795686802411753971031003 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\ \mathbf{elif}\;x \le 1.439938037416395822463756229119736498067 \cdot 10^{138}:\\ \;\;\;\;\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 2019351 +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)))