Average Error: 21.8 → 0.0
Time: 4.1s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.33487114297610053 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 5.95124163362423574 \cdot 10^{140}:\\ \;\;\;\;\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.33487114297610053 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 5.95124163362423574 \cdot 10^{140}:\\
\;\;\;\;\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 r521869 = x;
        double r521870 = r521869 * r521869;
        double r521871 = y;
        double r521872 = r521870 + r521871;
        double r521873 = sqrt(r521872);
        return r521873;
}

double f(double x, double y) {
        double r521874 = x;
        double r521875 = -1.3348711429761005e+154;
        bool r521876 = r521874 <= r521875;
        double r521877 = 0.5;
        double r521878 = y;
        double r521879 = r521878 / r521874;
        double r521880 = r521877 * r521879;
        double r521881 = r521874 + r521880;
        double r521882 = -r521881;
        double r521883 = 5.951241633624236e+140;
        bool r521884 = r521874 <= r521883;
        double r521885 = r521874 * r521874;
        double r521886 = r521885 + r521878;
        double r521887 = sqrt(r521886);
        double r521888 = fma(r521877, r521879, r521874);
        double r521889 = r521884 ? r521887 : r521888;
        double r521890 = r521876 ? r521882 : r521889;
        return r521890;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.8
Target0.5
Herbie0.0
\[\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.3348711429761005e+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.3348711429761005e+154 < x < 5.951241633624236e+140

    1. Initial program 0.0

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

    if 5.951241633624236e+140 < x

    1. Initial program 59.3

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

      \[\leadsto \color{blue}{x + \frac{1}{2} \cdot \frac{y}{x}}\]
    3. Simplified0.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.0

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

Reproduce

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