Average Error: 21.9 → 0.2
Time: 1.1s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.26339127879627209 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 4.85789658872666293 \cdot 10^{84}:\\ \;\;\;\;\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.26339127879627209 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 4.85789658872666293 \cdot 10^{84}:\\
\;\;\;\;\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 r429652 = x;
        double r429653 = r429652 * r429652;
        double r429654 = y;
        double r429655 = r429653 + r429654;
        double r429656 = sqrt(r429655);
        return r429656;
}

double f(double x, double y) {
        double r429657 = x;
        double r429658 = -1.2633912787962721e+154;
        bool r429659 = r429657 <= r429658;
        double r429660 = 0.5;
        double r429661 = y;
        double r429662 = r429661 / r429657;
        double r429663 = r429660 * r429662;
        double r429664 = r429657 + r429663;
        double r429665 = -r429664;
        double r429666 = 4.857896588726663e+84;
        bool r429667 = r429657 <= r429666;
        double r429668 = r429657 * r429657;
        double r429669 = r429668 + r429661;
        double r429670 = sqrt(r429669);
        double r429671 = fma(r429660, r429662, r429657);
        double r429672 = r429667 ? r429670 : r429671;
        double r429673 = r429659 ? r429665 : r429672;
        return r429673;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.9
Target0.5
Herbie0.2
\[\begin{array}{l} \mathbf{if}\;x \lt -1.5097698010472593 \cdot 10^{153}:\\ \;\;\;\;-\left(0.5 \cdot \frac{y}{x} + x\right)\\ \mathbf{elif}\;x \lt 5.5823995511225407 \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.2633912787962721e+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.2633912787962721e+154 < x < 4.857896588726663e+84

    1. Initial program 0.0

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

    if 4.857896588726663e+84 < x

    1. Initial program 45.0

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

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

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

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

Reproduce

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