Average Error: 38.2 → 26.2
Time: 9.5s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.4065040948592316 \cdot 10^{146}:\\ \;\;\;\;-\frac{z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 3.54010436725920069 \cdot 10^{76}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -1.4065040948592316 \cdot 10^{146}:\\
\;\;\;\;-\frac{z}{\sqrt{3}}\\

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

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\

\end{array}
double f(double x, double y, double z) {
        double r724265 = x;
        double r724266 = r724265 * r724265;
        double r724267 = y;
        double r724268 = r724267 * r724267;
        double r724269 = r724266 + r724268;
        double r724270 = z;
        double r724271 = r724270 * r724270;
        double r724272 = r724269 + r724271;
        double r724273 = 3.0;
        double r724274 = r724272 / r724273;
        double r724275 = sqrt(r724274);
        return r724275;
}

double f(double x, double y, double z) {
        double r724276 = z;
        double r724277 = -1.4065040948592316e+146;
        bool r724278 = r724276 <= r724277;
        double r724279 = 3.0;
        double r724280 = sqrt(r724279);
        double r724281 = r724276 / r724280;
        double r724282 = -r724281;
        double r724283 = 3.540104367259201e+76;
        bool r724284 = r724276 <= r724283;
        double r724285 = x;
        double r724286 = y;
        double r724287 = r724286 * r724286;
        double r724288 = fma(r724285, r724285, r724287);
        double r724289 = fma(r724276, r724276, r724288);
        double r724290 = sqrt(r724289);
        double r724291 = 1.0;
        double r724292 = r724291 / r724279;
        double r724293 = sqrt(r724292);
        double r724294 = r724290 * r724293;
        double r724295 = r724276 * r724293;
        double r724296 = r724284 ? r724294 : r724295;
        double r724297 = r724278 ? r724282 : r724296;
        return r724297;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target26.1
Herbie26.2
\[\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 < -1.4065040948592316e+146

    1. Initial program 62.4

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

      \[\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 div-inv62.4

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{z}{\sqrt{3}}}\]
    11. Simplified16.2

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

    if -1.4065040948592316e+146 < z < 3.540104367259201e+76

    1. Initial program 29.6

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

      \[\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 div-inv29.6

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

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

    if 3.540104367259201e+76 < z

    1. Initial program 52.5

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

      \[\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 div-inv52.5

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

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

      \[\leadsto \color{blue}{z} \cdot \sqrt{\frac{1}{3}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.4065040948592316 \cdot 10^{146}:\\ \;\;\;\;-\frac{z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 3.54010436725920069 \cdot 10^{76}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\ \end{array}\]

Reproduce

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