Average Error: 21.4 → 0.3
Time: 1.8s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.28151121097985566 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \mathbf{elif}\;x \le 3.3825854527583296 \cdot 10^{81}:\\ \;\;\;\;\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.28151121097985566 \cdot 10^{154}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\

\mathbf{elif}\;x \le 3.3825854527583296 \cdot 10^{81}:\\
\;\;\;\;\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 r510909 = x;
        double r510910 = r510909 * r510909;
        double r510911 = y;
        double r510912 = r510910 + r510911;
        double r510913 = sqrt(r510912);
        return r510913;
}

double f(double x, double y) {
        double r510914 = x;
        double r510915 = -1.2815112109798557e+154;
        bool r510916 = r510914 <= r510915;
        double r510917 = 0.5;
        double r510918 = y;
        double r510919 = r510918 / r510914;
        double r510920 = fma(r510917, r510919, r510914);
        double r510921 = -r510920;
        double r510922 = 3.3825854527583296e+81;
        bool r510923 = r510914 <= r510922;
        double r510924 = r510914 * r510914;
        double r510925 = r510924 + r510918;
        double r510926 = sqrt(r510925);
        double r510927 = r510923 ? r510926 : r510920;
        double r510928 = r510916 ? r510921 : r510927;
        return r510928;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.4
Target0.5
Herbie0.3
\[\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.2815112109798557e+154

    1. Initial program 63.9

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

      \[\leadsto \color{blue}{-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)}\]
    3. Simplified0.0

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

    if -1.2815112109798557e+154 < x < 3.3825854527583296e+81

    1. Initial program 0.0

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

    if 3.3825854527583296e+81 < x

    1. Initial program 44.2

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

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

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

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

Reproduce

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