Average Error: 37.8 → 25.9
Time: 17.9s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.919949817214073938734336094914511930365 \cdot 10^{152}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 3.216933802137221684316757518669772199128 \cdot 10^{-308}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{3}}\\ \mathbf{elif}\;z \le 1.082662187591500004465358532585611193411 \cdot 10^{-284}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 2.70108591667212070105861187564907986718 \cdot 10^{121}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{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.919949817214073938734336094914511930365 \cdot 10^{152}:\\
\;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

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

\mathbf{elif}\;z \le 1.082662187591500004465358532585611193411 \cdot 10^{-284}:\\
\;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r396556 = x;
        double r396557 = r396556 * r396556;
        double r396558 = y;
        double r396559 = r396558 * r396558;
        double r396560 = r396557 + r396559;
        double r396561 = z;
        double r396562 = r396561 * r396561;
        double r396563 = r396560 + r396562;
        double r396564 = 3.0;
        double r396565 = r396563 / r396564;
        double r396566 = sqrt(r396565);
        return r396566;
}

double f(double x, double y, double z) {
        double r396567 = z;
        double r396568 = -1.919949817214074e+152;
        bool r396569 = r396567 <= r396568;
        double r396570 = 0.3333333333333333;
        double r396571 = sqrt(r396570);
        double r396572 = r396567 * r396571;
        double r396573 = -r396572;
        double r396574 = 3.2169338021372217e-308;
        bool r396575 = r396567 <= r396574;
        double r396576 = x;
        double r396577 = y;
        double r396578 = r396577 * r396577;
        double r396579 = fma(r396576, r396576, r396578);
        double r396580 = fma(r396567, r396567, r396579);
        double r396581 = 3.0;
        double r396582 = r396580 / r396581;
        double r396583 = sqrt(r396582);
        double r396584 = 1.0826621875915e-284;
        bool r396585 = r396567 <= r396584;
        double r396586 = r396577 * r396571;
        double r396587 = 2.7010859166721207e+121;
        bool r396588 = r396567 <= r396587;
        double r396589 = sqrt(r396580);
        double r396590 = sqrt(r396581);
        double r396591 = r396589 / r396590;
        double r396592 = r396567 / r396590;
        double r396593 = r396588 ? r396591 : r396592;
        double r396594 = r396585 ? r396586 : r396593;
        double r396595 = r396575 ? r396583 : r396594;
        double r396596 = r396569 ? r396573 : r396595;
        return r396596;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.7
Herbie25.9
\[\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 5 regimes
  2. if z < -1.919949817214074e+152

    1. Initial program 63.7

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

      \[\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-inv63.7

      \[\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-prod63.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}}}\]
    6. Using strategy rm
    7. Applied add-exp-log63.7

      \[\leadsto \sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \color{blue}{e^{\log \left(\sqrt{\frac{1}{3}}\right)}}\]
    8. Applied add-exp-log63.7

      \[\leadsto \color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\right)}} \cdot e^{\log \left(\sqrt{\frac{1}{3}}\right)}\]
    9. Applied prod-exp63.7

      \[\leadsto \color{blue}{e^{\log \left(\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\right) + \log \left(\sqrt{\frac{1}{3}}\right)}}\]
    10. Simplified63.7

      \[\leadsto e^{\color{blue}{\log \left(\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\right)}}\]
    11. Taylor expanded around -inf 17.2

      \[\leadsto \color{blue}{-1 \cdot \left(z \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    12. Simplified17.2

      \[\leadsto \color{blue}{-z \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]

    if -1.919949817214074e+152 < z < 3.2169338021372217e-308

    1. Initial program 29.1

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

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

    if 3.2169338021372217e-308 < z < 1.0826621875915e-284

    1. Initial program 32.7

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

      \[\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 0 48.1

      \[\leadsto \color{blue}{y \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]

    if 1.0826621875915e-284 < z < 2.7010859166721207e+121

    1. Initial program 28.8

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

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

    if 2.7010859166721207e+121 < z

    1. Initial program 57.9

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

      \[\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 sqrt-div57.9

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

      \[\leadsto \frac{\color{blue}{z}}{\sqrt{3}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification25.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.919949817214073938734336094914511930365 \cdot 10^{152}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 3.216933802137221684316757518669772199128 \cdot 10^{-308}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{3}}\\ \mathbf{elif}\;z \le 1.082662187591500004465358532585611193411 \cdot 10^{-284}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 2.70108591667212070105861187564907986718 \cdot 10^{121}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]

Reproduce

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