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

\mathbf{elif}\;x \le 1.785490461401806573968894092684210024398 \cdot 10^{149}:\\
\;\;\;\;\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 r464378 = x;
        double r464379 = r464378 * r464378;
        double r464380 = y;
        double r464381 = r464379 + r464380;
        double r464382 = sqrt(r464381);
        return r464382;
}

double f(double x, double y) {
        double r464383 = x;
        double r464384 = -1.3211775299738663e+154;
        bool r464385 = r464383 <= r464384;
        double r464386 = 0.5;
        double r464387 = r464386 / r464383;
        double r464388 = y;
        double r464389 = fma(r464387, r464388, r464383);
        double r464390 = -r464389;
        double r464391 = 1.7854904614018066e+149;
        bool r464392 = r464383 <= r464391;
        double r464393 = fma(r464383, r464383, r464388);
        double r464394 = sqrt(r464393);
        double r464395 = r464388 / r464383;
        double r464396 = fma(r464395, r464386, r464383);
        double r464397 = r464392 ? r464394 : r464396;
        double r464398 = r464385 ? r464390 : r464397;
        return r464398;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.7
Target0.6
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.3211775299738663e+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.3211775299738663e+154 < x < 1.7854904614018066e+149

    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.7854904614018066e+149 < x

    1. Initial program 61.8

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

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

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

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