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

\mathbf{elif}\;x \le 2.058549686456957362651677908428126133886 \cdot 10^{132}:\\
\;\;\;\;\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 r328179 = x;
        double r328180 = r328179 * r328179;
        double r328181 = y;
        double r328182 = r328180 + r328181;
        double r328183 = sqrt(r328182);
        return r328183;
}

double f(double x, double y) {
        double r328184 = x;
        double r328185 = -1.3754584125205376e+154;
        bool r328186 = r328184 <= r328185;
        double r328187 = 0.5;
        double r328188 = y;
        double r328189 = r328188 / r328184;
        double r328190 = fma(r328187, r328189, r328184);
        double r328191 = -r328190;
        double r328192 = 2.0585496864569574e+132;
        bool r328193 = r328184 <= r328192;
        double r328194 = r328184 * r328184;
        double r328195 = r328194 + r328188;
        double r328196 = sqrt(r328195);
        double r328197 = r328193 ? r328196 : r328190;
        double r328198 = r328186 ? r328191 : r328197;
        return r328198;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.0
Target0.6
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.3754584125205376e+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.3754584125205376e+154 < x < 2.0585496864569574e+132

    1. Initial program 0.0

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

    if 2.0585496864569574e+132 < x

    1. Initial program 56.3

      \[\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.375458412520537572590774977918936206023 \cdot 10^{154}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \mathbf{elif}\;x \le 2.058549686456957362651677908428126133886 \cdot 10^{132}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{2}, \frac{y}{x}, x\right)\\ \end{array}\]

Reproduce

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