Average Error: 21.2 → 0.6
Time: 3.9s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.375014865247395747540068707733175756025 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 3.065884434570755342390558398880505157434 \cdot 10^{46}:\\ \;\;\;\;\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.375014865247395747540068707733175756025 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 3.065884434570755342390558398880505157434 \cdot 10^{46}:\\
\;\;\;\;\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 r600525 = x;
        double r600526 = r600525 * r600525;
        double r600527 = y;
        double r600528 = r600526 + r600527;
        double r600529 = sqrt(r600528);
        return r600529;
}

double f(double x, double y) {
        double r600530 = x;
        double r600531 = -1.3750148652473957e+154;
        bool r600532 = r600530 <= r600531;
        double r600533 = 0.5;
        double r600534 = y;
        double r600535 = r600534 / r600530;
        double r600536 = r600533 * r600535;
        double r600537 = r600530 + r600536;
        double r600538 = -r600537;
        double r600539 = 3.0658844345707553e+46;
        bool r600540 = r600530 <= r600539;
        double r600541 = r600530 * r600530;
        double r600542 = r600541 + r600534;
        double r600543 = sqrt(r600542);
        double r600544 = fma(r600533, r600535, r600530);
        double r600545 = r600540 ? r600543 : r600544;
        double r600546 = r600532 ? r600538 : r600545;
        return r600546;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.2
Target0.5
Herbie0.6
\[\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.3750148652473957e+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.3750148652473957e+154 < x < 3.0658844345707553e+46

    1. Initial program 0.0

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

    if 3.0658844345707553e+46 < x

    1. Initial program 39.7

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

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

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

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

Reproduce

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