Average Error: 38.1 → 25.8
Time: 13.5s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.2291282360035104 \cdot 10^{133}:\\ \;\;\;\;-\frac{z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 2.8539710068846394 \cdot 10^{65}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\frac{3}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.333333333333333315}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -2.2291282360035104 \cdot 10^{133}:\\
\;\;\;\;-\frac{z}{\sqrt{3}}\\

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

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{0.333333333333333315}\\

\end{array}
double f(double x, double y, double z) {
        double r958369 = x;
        double r958370 = r958369 * r958369;
        double r958371 = y;
        double r958372 = r958371 * r958371;
        double r958373 = r958370 + r958372;
        double r958374 = z;
        double r958375 = r958374 * r958374;
        double r958376 = r958373 + r958375;
        double r958377 = 3.0;
        double r958378 = r958376 / r958377;
        double r958379 = sqrt(r958378);
        return r958379;
}

double f(double x, double y, double z) {
        double r958380 = z;
        double r958381 = -2.2291282360035104e+133;
        bool r958382 = r958380 <= r958381;
        double r958383 = 3.0;
        double r958384 = sqrt(r958383);
        double r958385 = r958380 / r958384;
        double r958386 = -r958385;
        double r958387 = 2.8539710068846394e+65;
        bool r958388 = r958380 <= r958387;
        double r958389 = x;
        double r958390 = y;
        double r958391 = r958390 * r958390;
        double r958392 = fma(r958389, r958389, r958391);
        double r958393 = fma(r958380, r958380, r958392);
        double r958394 = sqrt(r958393);
        double r958395 = r958383 / r958394;
        double r958396 = r958394 / r958395;
        double r958397 = sqrt(r958396);
        double r958398 = 0.3333333333333333;
        double r958399 = sqrt(r958398);
        double r958400 = r958380 * r958399;
        double r958401 = r958388 ? r958397 : r958400;
        double r958402 = r958382 ? r958386 : r958401;
        return r958402;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.1
Target25.6
Herbie25.8
\[\begin{array}{l} \mathbf{if}\;z \lt -6.3964793941097758 \cdot 10^{136}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \lt 7.3202936944041821 \cdot 10^{117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.333333333333333315} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if z < -2.2291282360035104e+133

    1. Initial program 59.8

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

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

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}{3}}\]
    5. Applied associate-/l*59.7

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

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

      \[\leadsto \sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\frac{3}{\color{blue}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}} \cdot \sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}}}\]
    9. Applied add-sqr-sqrt59.8

      \[\leadsto \sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\frac{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}} \cdot \sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}}\]
    10. Applied times-frac59.8

      \[\leadsto \sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\color{blue}{\frac{\sqrt{3}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}} \cdot \frac{\sqrt{3}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}}}\]
    11. Applied add-sqr-sqrt59.8

      \[\leadsto \sqrt{\frac{\sqrt{\color{blue}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}{\frac{\sqrt{3}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}} \cdot \frac{\sqrt{3}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}}\]
    12. Applied sqrt-prod59.8

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}} \cdot \sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}{\frac{\sqrt{3}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}} \cdot \frac{\sqrt{3}}{\sqrt{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}}\]
    13. Applied times-frac59.8

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{z}{\sqrt{3}}}\]
    17. Simplified17.4

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

    if -2.2291282360035104e+133 < z < 2.8539710068846394e+65

    1. Initial program 29.2

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

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

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}{3}}\]
    5. Applied associate-/l*29.2

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

    if 2.8539710068846394e+65 < z

    1. Initial program 51.4

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.2291282360035104 \cdot 10^{133}:\\ \;\;\;\;-\frac{z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 2.8539710068846394 \cdot 10^{65}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\frac{3}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Reproduce

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

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

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