Average Error: 38.1 → 25.8
Time: 14.1s
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{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\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{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\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 r1129310 = x;
        double r1129311 = r1129310 * r1129310;
        double r1129312 = y;
        double r1129313 = r1129312 * r1129312;
        double r1129314 = r1129311 + r1129313;
        double r1129315 = z;
        double r1129316 = r1129315 * r1129315;
        double r1129317 = r1129314 + r1129316;
        double r1129318 = 3.0;
        double r1129319 = r1129317 / r1129318;
        double r1129320 = sqrt(r1129319);
        return r1129320;
}

double f(double x, double y, double z) {
        double r1129321 = z;
        double r1129322 = -2.2291282360035104e+133;
        bool r1129323 = r1129321 <= r1129322;
        double r1129324 = 3.0;
        double r1129325 = sqrt(r1129324);
        double r1129326 = r1129321 / r1129325;
        double r1129327 = -r1129326;
        double r1129328 = 2.8539710068846394e+65;
        bool r1129329 = r1129321 <= r1129328;
        double r1129330 = x;
        double r1129331 = r1129330 * r1129330;
        double r1129332 = y;
        double r1129333 = r1129332 * r1129332;
        double r1129334 = r1129331 + r1129333;
        double r1129335 = r1129321 * r1129321;
        double r1129336 = r1129334 + r1129335;
        double r1129337 = sqrt(r1129336);
        double r1129338 = fma(r1129330, r1129330, r1129333);
        double r1129339 = fma(r1129321, r1129321, r1129338);
        double r1129340 = sqrt(r1129339);
        double r1129341 = r1129324 / r1129340;
        double r1129342 = r1129337 / r1129341;
        double r1129343 = sqrt(r1129342);
        double r1129344 = 0.3333333333333333;
        double r1129345 = sqrt(r1129344);
        double r1129346 = r1129321 * r1129345;
        double r1129347 = r1129329 ? r1129343 : r1129346;
        double r1129348 = r1129323 ? r1129327 : r1129347;
        return r1129348;
}

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. Using strategy rm
    3. Applied add-sqr-sqrt59.8

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied associate-/r*59.8

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{z}{\sqrt{3}}}\]
    7. 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. Using strategy rm
    3. Applied add-sqr-sqrt29.2

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

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

      \[\leadsto \sqrt{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\color{blue}{\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. Taylor expanded around 0 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{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\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)))