Average Error: 21.8 → 0.1
Time: 1.1s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.324213973320318357393253673244626598754 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 1.919685499943437334898227828713962994172 \cdot 10^{112}:\\ \;\;\;\;\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.324213973320318357393253673244626598754 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 1.919685499943437334898227828713962994172 \cdot 10^{112}:\\
\;\;\;\;\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 r518282 = x;
        double r518283 = r518282 * r518282;
        double r518284 = y;
        double r518285 = r518283 + r518284;
        double r518286 = sqrt(r518285);
        return r518286;
}

double f(double x, double y) {
        double r518287 = x;
        double r518288 = -1.3242139733203184e+154;
        bool r518289 = r518287 <= r518288;
        double r518290 = 0.5;
        double r518291 = y;
        double r518292 = r518291 / r518287;
        double r518293 = r518290 * r518292;
        double r518294 = r518287 + r518293;
        double r518295 = -r518294;
        double r518296 = 1.9196854999434373e+112;
        bool r518297 = r518287 <= r518296;
        double r518298 = r518287 * r518287;
        double r518299 = r518298 + r518291;
        double r518300 = sqrt(r518299);
        double r518301 = fma(r518290, r518292, r518287);
        double r518302 = r518297 ? r518300 : r518301;
        double r518303 = r518289 ? r518295 : r518302;
        return r518303;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.8
Target0.6
Herbie0.1
\[\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.3242139733203184e+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.3242139733203184e+154 < x < 1.9196854999434373e+112

    1. Initial program 0.0

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

    if 1.9196854999434373e+112 < x

    1. Initial program 50.5

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

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

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

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

Reproduce

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