Average Error: 21.4 → 0.6
Time: 1.1s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.2288156672747498 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 1.2801916827935602 \cdot 10^{51}:\\ \;\;\;\;\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.2288156672747498 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 1.2801916827935602 \cdot 10^{51}:\\
\;\;\;\;\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 r420792 = x;
        double r420793 = r420792 * r420792;
        double r420794 = y;
        double r420795 = r420793 + r420794;
        double r420796 = sqrt(r420795);
        return r420796;
}

double f(double x, double y) {
        double r420797 = x;
        double r420798 = -1.2288156672747498e+154;
        bool r420799 = r420797 <= r420798;
        double r420800 = 0.5;
        double r420801 = y;
        double r420802 = r420801 / r420797;
        double r420803 = r420800 * r420802;
        double r420804 = r420797 + r420803;
        double r420805 = -r420804;
        double r420806 = 1.2801916827935602e+51;
        bool r420807 = r420797 <= r420806;
        double r420808 = r420797 * r420797;
        double r420809 = r420808 + r420801;
        double r420810 = sqrt(r420809);
        double r420811 = fma(r420800, r420802, r420797);
        double r420812 = r420807 ? r420810 : r420811;
        double r420813 = r420799 ? r420805 : r420812;
        return r420813;
}

Error

Bits error versus x

Bits error versus y

Target

Original21.4
Target0.5
Herbie0.6
\[\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.2288156672747498e+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.2288156672747498e+154 < x < 1.2801916827935602e+51

    1. Initial program 0.0

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

    if 1.2801916827935602e+51 < x

    1. Initial program 39.4

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

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

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

Reproduce

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