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

\mathbf{elif}\;x \le 1.0458219696427684 \cdot 10^{97}:\\
\;\;\;\;\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 r396817 = x;
        double r396818 = r396817 * r396817;
        double r396819 = y;
        double r396820 = r396818 + r396819;
        double r396821 = sqrt(r396820);
        return r396821;
}

double f(double x, double y) {
        double r396822 = x;
        double r396823 = -1.3014522151396273e+154;
        bool r396824 = r396822 <= r396823;
        double r396825 = 0.5;
        double r396826 = y;
        double r396827 = r396826 / r396822;
        double r396828 = fma(r396825, r396827, r396822);
        double r396829 = -r396828;
        double r396830 = 1.0458219696427684e+97;
        bool r396831 = r396822 <= r396830;
        double r396832 = r396822 * r396822;
        double r396833 = r396832 + r396826;
        double r396834 = sqrt(r396833);
        double r396835 = r396831 ? r396834 : r396828;
        double r396836 = r396824 ? r396829 : r396835;
        return r396836;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.1
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.3014522151396273e+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)}\]
    3. Simplified0

      \[\leadsto \color{blue}{-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)}\]

    if -1.3014522151396273e+154 < x < 1.0458219696427684e+97

    1. Initial program 0.0

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

    if 1.0458219696427684e+97 < x

    1. Initial program 47.1

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

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

      \[\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.30145221513962733 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \mathbf{elif}\;x \le 1.0458219696427684 \cdot 10^{97}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \end{array}\]

Reproduce

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