Average Error: 37.4 → 25.3
Time: 18.4s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\ \;\;\;\;-\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;y \le 4.581660079438061697636144959988270294902 \cdot 10^{111}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\
\;\;\;\;-\frac{y}{\sqrt{3}}\\

\mathbf{elif}\;y \le 4.581660079438061697636144959988270294902 \cdot 10^{111}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\sqrt{3}}\\

\mathbf{else}:\\
\;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

\end{array}
double f(double x, double y, double z) {
        double r34671403 = x;
        double r34671404 = r34671403 * r34671403;
        double r34671405 = y;
        double r34671406 = r34671405 * r34671405;
        double r34671407 = r34671404 + r34671406;
        double r34671408 = z;
        double r34671409 = r34671408 * r34671408;
        double r34671410 = r34671407 + r34671409;
        double r34671411 = 3.0;
        double r34671412 = r34671410 / r34671411;
        double r34671413 = sqrt(r34671412);
        return r34671413;
}

double f(double x, double y, double z) {
        double r34671414 = y;
        double r34671415 = -7.465878436556341e+142;
        bool r34671416 = r34671414 <= r34671415;
        double r34671417 = 3.0;
        double r34671418 = sqrt(r34671417);
        double r34671419 = r34671414 / r34671418;
        double r34671420 = -r34671419;
        double r34671421 = 4.5816600794380617e+111;
        bool r34671422 = r34671414 <= r34671421;
        double r34671423 = x;
        double r34671424 = z;
        double r34671425 = r34671424 * r34671424;
        double r34671426 = fma(r34671423, r34671423, r34671425);
        double r34671427 = fma(r34671414, r34671414, r34671426);
        double r34671428 = sqrt(r34671427);
        double r34671429 = r34671428 / r34671418;
        double r34671430 = 0.3333333333333333;
        double r34671431 = sqrt(r34671430);
        double r34671432 = r34671414 * r34671431;
        double r34671433 = r34671422 ? r34671429 : r34671432;
        double r34671434 = r34671416 ? r34671420 : r34671433;
        return r34671434;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.4
Target25.2
Herbie25.3
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109775845820908799933348003545 \cdot 10^{136}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \lt 7.320293694404182125923160810847974073098 \cdot 10^{117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -7.465878436556341e+142

    1. Initial program 61.3

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified61.3

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}}\]
    3. Using strategy rm
    4. Applied sqrt-div61.3

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

      \[\leadsto \frac{\color{blue}{-1 \cdot y}}{\sqrt{3}}\]
    6. Simplified16.7

      \[\leadsto \frac{\color{blue}{-y}}{\sqrt{3}}\]

    if -7.465878436556341e+142 < y < 4.5816600794380617e+111

    1. Initial program 28.4

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified28.4

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}}\]
    3. Using strategy rm
    4. Applied sqrt-div28.6

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

    if 4.5816600794380617e+111 < y

    1. Initial program 56.2

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified56.2

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

      \[\leadsto \color{blue}{y \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification25.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\ \;\;\;\;-\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;y \le 4.581660079438061697636144959988270294902 \cdot 10^{111}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]

Reproduce

herbie shell --seed 2019172 +o rules:numerics
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"

  :herbie-target
  (if (< z -6.396479394109776e+136) (/ (- z) (sqrt 3.0)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3.0)) (* (sqrt 0.3333333333333333) z)))

  (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))