Average Error: 21.4 → 0.4
Time: 1.3s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.35596794389876828 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 6.210525943263359 \cdot 10^{71}:\\ \;\;\;\;\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.35596794389876828 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 6.210525943263359 \cdot 10^{71}:\\
\;\;\;\;\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 r442783 = x;
        double r442784 = r442783 * r442783;
        double r442785 = y;
        double r442786 = r442784 + r442785;
        double r442787 = sqrt(r442786);
        return r442787;
}

double f(double x, double y) {
        double r442788 = x;
        double r442789 = -1.3559679438987683e+154;
        bool r442790 = r442788 <= r442789;
        double r442791 = 0.5;
        double r442792 = y;
        double r442793 = r442792 / r442788;
        double r442794 = r442791 * r442793;
        double r442795 = r442788 + r442794;
        double r442796 = -r442795;
        double r442797 = 6.210525943263359e+71;
        bool r442798 = r442788 <= r442797;
        double r442799 = r442788 * r442788;
        double r442800 = r442799 + r442792;
        double r442801 = sqrt(r442800);
        double r442802 = fma(r442791, r442793, r442788);
        double r442803 = r442798 ? r442801 : r442802;
        double r442804 = r442790 ? r442796 : r442803;
        return r442804;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.4
Target0.6
Herbie0.4
\[\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.3559679438987683e+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.3559679438987683e+154 < x < 6.210525943263359e+71

    1. Initial program 0.0

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

    if 6.210525943263359e+71 < x

    1. Initial program 42.3

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

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

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

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

Reproduce

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