Average Error: 21.5 → 0.7
Time: 1.3s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.3309154473001696 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 1.20482112249854811 \cdot 10^{36}:\\ \;\;\;\;\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.3309154473001696 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 1.20482112249854811 \cdot 10^{36}:\\
\;\;\;\;\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 r482009 = x;
        double r482010 = r482009 * r482009;
        double r482011 = y;
        double r482012 = r482010 + r482011;
        double r482013 = sqrt(r482012);
        return r482013;
}

double f(double x, double y) {
        double r482014 = x;
        double r482015 = -1.3309154473001696e+154;
        bool r482016 = r482014 <= r482015;
        double r482017 = 0.5;
        double r482018 = y;
        double r482019 = r482018 / r482014;
        double r482020 = r482017 * r482019;
        double r482021 = r482014 + r482020;
        double r482022 = -r482021;
        double r482023 = 1.2048211224985481e+36;
        bool r482024 = r482014 <= r482023;
        double r482025 = r482014 * r482014;
        double r482026 = r482025 + r482018;
        double r482027 = sqrt(r482026);
        double r482028 = fma(r482017, r482019, r482014);
        double r482029 = r482024 ? r482027 : r482028;
        double r482030 = r482016 ? r482022 : r482029;
        return r482030;
}

Error

Bits error versus x

Bits error versus y

Target

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

    1. Initial program 64.0

      \[\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)}\]

    if -1.3309154473001696e+154 < x < 1.2048211224985481e+36

    1. Initial program 0.0

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

    if 1.2048211224985481e+36 < x

    1. Initial program 38.9

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

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

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

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

Reproduce

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