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

\mathbf{elif}\;x \le 8.970759006124063597546869044071596379693 \cdot 10^{123}:\\
\;\;\;\;\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 r380744 = x;
        double r380745 = r380744 * r380744;
        double r380746 = y;
        double r380747 = r380745 + r380746;
        double r380748 = sqrt(r380747);
        return r380748;
}

double f(double x, double y) {
        double r380749 = x;
        double r380750 = -1.3545742274115277e+154;
        bool r380751 = r380749 <= r380750;
        double r380752 = 0.5;
        double r380753 = y;
        double r380754 = r380753 / r380749;
        double r380755 = fma(r380752, r380754, r380749);
        double r380756 = -r380755;
        double r380757 = 8.970759006124064e+123;
        bool r380758 = r380749 <= r380757;
        double r380759 = r380749 * r380749;
        double r380760 = r380759 + r380753;
        double r380761 = sqrt(r380760);
        double r380762 = r380758 ? r380761 : r380755;
        double r380763 = r380751 ? r380756 : r380762;
        return r380763;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.6
Target0.5
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.3545742274115277e+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.3545742274115277e+154 < x < 8.970759006124064e+123

    1. Initial program 0.0

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

    if 8.970759006124064e+123 < x

    1. Initial program 54.2

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

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

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

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

Reproduce

herbie shell --seed 2019212 +o rules:numerics
(FPCore (x y)
  :name "Linear.Quaternion:$clog from linear-1.19.1.3"
  :precision binary64

  :herbie-target
  (if (< x -1.5097698010472593e153) (- (+ (* 0.5 (/ y x)) x)) (if (< x 5.5823995511225407e57) (sqrt (+ (* x x) y)) (+ (* 0.5 (/ y x)) x)))

  (sqrt (+ (* x x) y)))