Average Error: 22.0 → 0.1
Time: 1.4s
Precision: 64
\[\sqrt{x \cdot x + y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.33520561977810183 \cdot 10^{154}:\\ \;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\ \mathbf{elif}\;x \le 8.45687055249789216 \cdot 10^{117}:\\ \;\;\;\;\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.33520561977810183 \cdot 10^{154}:\\
\;\;\;\;-\left(x + \frac{1}{2} \cdot \frac{y}{x}\right)\\

\mathbf{elif}\;x \le 8.45687055249789216 \cdot 10^{117}:\\
\;\;\;\;\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 r464058 = x;
        double r464059 = r464058 * r464058;
        double r464060 = y;
        double r464061 = r464059 + r464060;
        double r464062 = sqrt(r464061);
        return r464062;
}

double f(double x, double y) {
        double r464063 = x;
        double r464064 = -1.3352056197781018e+154;
        bool r464065 = r464063 <= r464064;
        double r464066 = 0.5;
        double r464067 = y;
        double r464068 = r464067 / r464063;
        double r464069 = r464066 * r464068;
        double r464070 = r464063 + r464069;
        double r464071 = -r464070;
        double r464072 = 8.456870552497892e+117;
        bool r464073 = r464063 <= r464072;
        double r464074 = r464063 * r464063;
        double r464075 = r464074 + r464067;
        double r464076 = sqrt(r464075);
        double r464077 = fma(r464066, r464068, r464063);
        double r464078 = r464073 ? r464076 : r464077;
        double r464079 = r464065 ? r464071 : r464078;
        return r464079;
}

Error

Bits error versus x

Bits error versus y

Target

Original22.0
Target0.6
Herbie0.1
\[\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.3352056197781018e+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.3352056197781018e+154 < x < 8.456870552497892e+117

    1. Initial program 0.0

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

    if 8.456870552497892e+117 < x

    1. Initial program 52.8

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

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

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

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

Reproduce

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