Average Error: 37.8 → 26.3
Time: 14.9s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \cdot y \le 4.439426932358056857070177144442333215206 \cdot 10^{-209}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;y \cdot y \le 3.175284095555138930133331201720651244119 \cdot 10^{-175}:\\ \;\;\;\;\left|\frac{-z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 123213193343404294518476252381184:\\ \;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 7.236242921693302431325514422666640935777 \cdot 10^{54}:\\ \;\;\;\;\left|\frac{-z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 1.596754056659774816555212493880410624898 \cdot 10^{244}:\\ \;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{y}{\sqrt{3}}\right|\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \cdot y \le 4.439426932358056857070177144442333215206 \cdot 10^{-209}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\

\mathbf{elif}\;y \cdot y \le 3.175284095555138930133331201720651244119 \cdot 10^{-175}:\\
\;\;\;\;\left|\frac{-z}{\sqrt{3}}\right|\\

\mathbf{elif}\;y \cdot y \le 123213193343404294518476252381184:\\
\;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\

\mathbf{elif}\;y \cdot y \le 7.236242921693302431325514422666640935777 \cdot 10^{54}:\\
\;\;\;\;\left|\frac{-z}{\sqrt{3}}\right|\\

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

\mathbf{else}:\\
\;\;\;\;\left|\frac{y}{\sqrt{3}}\right|\\

\end{array}
double f(double x, double y, double z) {
        double r648328 = x;
        double r648329 = r648328 * r648328;
        double r648330 = y;
        double r648331 = r648330 * r648330;
        double r648332 = r648329 + r648331;
        double r648333 = z;
        double r648334 = r648333 * r648333;
        double r648335 = r648332 + r648334;
        double r648336 = 3.0;
        double r648337 = r648335 / r648336;
        double r648338 = sqrt(r648337);
        return r648338;
}

double f(double x, double y, double z) {
        double r648339 = y;
        double r648340 = r648339 * r648339;
        double r648341 = 4.439426932358057e-209;
        bool r648342 = r648340 <= r648341;
        double r648343 = z;
        double r648344 = x;
        double r648345 = fma(r648344, r648344, r648340);
        double r648346 = fma(r648343, r648343, r648345);
        double r648347 = sqrt(r648346);
        double r648348 = 1.0;
        double r648349 = 3.0;
        double r648350 = r648348 / r648349;
        double r648351 = sqrt(r648350);
        double r648352 = r648347 * r648351;
        double r648353 = 3.175284095555139e-175;
        bool r648354 = r648340 <= r648353;
        double r648355 = -r648343;
        double r648356 = sqrt(r648349);
        double r648357 = r648355 / r648356;
        double r648358 = fabs(r648357);
        double r648359 = 1.232131933434043e+32;
        bool r648360 = r648340 <= r648359;
        double r648361 = r648347 / r648356;
        double r648362 = fabs(r648361);
        double r648363 = 7.236242921693302e+54;
        bool r648364 = r648340 <= r648363;
        double r648365 = 1.5967540566597748e+244;
        bool r648366 = r648340 <= r648365;
        double r648367 = r648339 / r648356;
        double r648368 = fabs(r648367);
        double r648369 = r648366 ? r648362 : r648368;
        double r648370 = r648364 ? r648358 : r648369;
        double r648371 = r648360 ? r648362 : r648370;
        double r648372 = r648354 ? r648358 : r648371;
        double r648373 = r648342 ? r648352 : r648372;
        return r648373;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.5
Herbie26.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 4 regimes
  2. if (* y y) < 4.439426932358057e-209

    1. Initial program 30.5

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified30.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-inv30.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-prod30.6

      \[\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 4.439426932358057e-209 < (* y y) < 3.175284095555139e-175 or 1.232131933434043e+32 < (* y y) < 7.236242921693302e+54

    1. Initial program 28.1

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

      \[\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-sqrt28.3

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

      \[\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)}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    6. Applied times-frac28.2

      \[\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{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}}}\]
    7. Applied rem-sqrt-square28.2

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

      \[\leadsto \left|\frac{\color{blue}{-1 \cdot z}}{\sqrt{3}}\right|\]
    9. Simplified36.9

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

    if 3.175284095555139e-175 < (* y y) < 1.232131933434043e+32 or 7.236242921693302e+54 < (* y y) < 1.5967540566597748e+244

    1. Initial program 28.0

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

      \[\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-sqrt28.2

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    5. Applied add-sqr-sqrt28.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)}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    6. Applied times-frac28.2

      \[\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{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}}}\]
    7. Applied rem-sqrt-square28.2

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

    if 1.5967540566597748e+244 < (* y y)

    1. Initial program 57.8

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified57.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-sqrt57.8

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    5. Applied add-sqr-sqrt57.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)}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    6. Applied times-frac57.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{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}}}\]
    7. Applied rem-sqrt-square57.8

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

      \[\leadsto \left|\frac{\color{blue}{y}}{\sqrt{3}}\right|\]
  3. Recombined 4 regimes into one program.
  4. Final simplification26.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \le 4.439426932358056857070177144442333215206 \cdot 10^{-209}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;y \cdot y \le 3.175284095555138930133331201720651244119 \cdot 10^{-175}:\\ \;\;\;\;\left|\frac{-z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 123213193343404294518476252381184:\\ \;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 7.236242921693302431325514422666640935777 \cdot 10^{54}:\\ \;\;\;\;\left|\frac{-z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 1.596754056659774816555212493880410624898 \cdot 10^{244}:\\ \;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{y}{\sqrt{3}}\right|\\ \end{array}\]

Reproduce

herbie shell --seed 2019212 +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.3964793941097758e136) (/ (- z) (sqrt 3)) (if (< z 7.3202936944041821e117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3)) (* (sqrt 0.333333333333333315) z)))

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