Average Error: 21.3 → 0.2
Time: 9.5s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.163461482030304147197123634102301164915 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{\frac{1}{2}}{x}, y, x\right)\\ \mathbf{elif}\;x \le 2.648277919935003443657636145779230905143 \cdot 10^{92}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\ \end{array}\]
\sqrt{x \cdot x + y}
\begin{array}{l}
\mathbf{if}\;x \le -1.163461482030304147197123634102301164915 \cdot 10^{154}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{\frac{1}{2}}{x}, y, x\right)\\

\mathbf{elif}\;x \le 2.648277919935003443657636145779230905143 \cdot 10^{92}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{1}{2}, x\right)\\

\end{array}
double f(double x, double y) {
        double r421023 = x;
        double r421024 = r421023 * r421023;
        double r421025 = y;
        double r421026 = r421024 + r421025;
        double r421027 = sqrt(r421026);
        return r421027;
}

double f(double x, double y) {
        double r421028 = x;
        double r421029 = -1.1634614820303041e+154;
        bool r421030 = r421028 <= r421029;
        double r421031 = 0.5;
        double r421032 = r421031 / r421028;
        double r421033 = y;
        double r421034 = fma(r421032, r421033, r421028);
        double r421035 = -r421034;
        double r421036 = 2.6482779199350034e+92;
        bool r421037 = r421028 <= r421036;
        double r421038 = fma(r421028, r421028, r421033);
        double r421039 = sqrt(r421038);
        double r421040 = r421033 / r421028;
        double r421041 = fma(r421040, r421031, r421028);
        double r421042 = r421037 ? r421039 : r421041;
        double r421043 = r421030 ? r421035 : r421042;
        return r421043;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.3
Target0.6
Herbie0.2
\[\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.1634614820303041e+154

    1. Initial program 64.0

      \[\sqrt{x \cdot x + y}\]
    2. Simplified64.0

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}}\]
    3. Taylor expanded around -inf 0

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

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

    if -1.1634614820303041e+154 < x < 2.6482779199350034e+92

    1. Initial program 0.0

      \[\sqrt{x \cdot x + y}\]
    2. Simplified0.0

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}}\]

    if 2.6482779199350034e+92 < x

    1. Initial program 46.3

      \[\sqrt{x \cdot x + y}\]
    2. Simplified46.3

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}}\]
    3. Taylor expanded around inf 1.0

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

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

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

Reproduce

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