Average Error: 38.2 → 26.5
Time: 9.0s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -7.51459440820440755 \cdot 10^{142}:\\ \;\;\;\;-\left(x \cdot \sqrt{\sqrt{0.333333333333333315}}\right) \cdot \sqrt{\sqrt{0.333333333333333315}}\\ \mathbf{elif}\;x \le -9.04578056600420164 \cdot 10^{-238}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;x \le 1.7225630620991519 \cdot 10^{-270}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;x \le 4.24489976765657814 \cdot 10^{97}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;x \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}\;x \le -7.51459440820440755 \cdot 10^{142}:\\
\;\;\;\;-\left(x \cdot \sqrt{\sqrt{0.333333333333333315}}\right) \cdot \sqrt{\sqrt{0.333333333333333315}}\\

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

\mathbf{elif}\;x \le 1.7225630620991519 \cdot 10^{-270}:\\
\;\;\;\;\frac{y}{\sqrt{3}}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r825824 = x;
        double r825825 = r825824 * r825824;
        double r825826 = y;
        double r825827 = r825826 * r825826;
        double r825828 = r825825 + r825827;
        double r825829 = z;
        double r825830 = r825829 * r825829;
        double r825831 = r825828 + r825830;
        double r825832 = 3.0;
        double r825833 = r825831 / r825832;
        double r825834 = sqrt(r825833);
        return r825834;
}

double f(double x, double y, double z) {
        double r825835 = x;
        double r825836 = -7.514594408204408e+142;
        bool r825837 = r825835 <= r825836;
        double r825838 = 0.3333333333333333;
        double r825839 = sqrt(r825838);
        double r825840 = sqrt(r825839);
        double r825841 = r825835 * r825840;
        double r825842 = r825841 * r825840;
        double r825843 = -r825842;
        double r825844 = -9.045780566004202e-238;
        bool r825845 = r825835 <= r825844;
        double r825846 = z;
        double r825847 = y;
        double r825848 = r825847 * r825847;
        double r825849 = fma(r825835, r825835, r825848);
        double r825850 = fma(r825846, r825846, r825849);
        double r825851 = sqrt(r825850);
        double r825852 = 1.0;
        double r825853 = 3.0;
        double r825854 = r825852 / r825853;
        double r825855 = sqrt(r825854);
        double r825856 = r825851 * r825855;
        double r825857 = 1.722563062099152e-270;
        bool r825858 = r825835 <= r825857;
        double r825859 = sqrt(r825853);
        double r825860 = r825847 / r825859;
        double r825861 = 4.244899767656578e+97;
        bool r825862 = r825835 <= r825861;
        double r825863 = r825835 * r825839;
        double r825864 = r825862 ? r825856 : r825863;
        double r825865 = r825858 ? r825860 : r825864;
        double r825866 = r825845 ? r825856 : r825865;
        double r825867 = r825837 ? r825843 : r825866;
        return r825867;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target26.1
Herbie26.5
\[\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 4 regimes
  2. if x < -7.514594408204408e+142

    1. Initial program 61.4

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Taylor expanded around -inf 14.1

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{0.333333333333333315}\right)}\]
    3. Simplified14.1

      \[\leadsto \color{blue}{-x \cdot \sqrt{0.333333333333333315}}\]
    4. Using strategy rm
    5. Applied add-sqr-sqrt14.1

      \[\leadsto -x \cdot \sqrt{\color{blue}{\sqrt{0.333333333333333315} \cdot \sqrt{0.333333333333333315}}}\]
    6. Applied sqrt-prod14.1

      \[\leadsto -x \cdot \color{blue}{\left(\sqrt{\sqrt{0.333333333333333315}} \cdot \sqrt{\sqrt{0.333333333333333315}}\right)}\]
    7. Applied associate-*r*14.1

      \[\leadsto -\color{blue}{\left(x \cdot \sqrt{\sqrt{0.333333333333333315}}\right) \cdot \sqrt{\sqrt{0.333333333333333315}}}\]

    if -7.514594408204408e+142 < x < -9.045780566004202e-238 or 1.722563062099152e-270 < x < 4.244899767656578e+97

    1. Initial program 28.9

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied div-inv28.9

      \[\leadsto \sqrt{\color{blue}{\left(\left(x \cdot x + y \cdot y\right) + z \cdot z\right) \cdot \frac{1}{3}}}\]
    4. Applied sqrt-prod29.0

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

      \[\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 -9.045780566004202e-238 < x < 1.722563062099152e-270

    1. Initial program 31.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt31.2

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied add-sqr-sqrt31.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}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    5. Applied times-frac31.1

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}} \cdot \frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}}\]
    6. Simplified31.1

      \[\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{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}\]
    7. Simplified31.1

      \[\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}}}}\]
    8. Taylor expanded around 0 44.8

      \[\leadsto \color{blue}{\frac{y}{\sqrt{3}}}\]

    if 4.244899767656578e+97 < x

    1. Initial program 54.9

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Taylor expanded around inf 19.5

      \[\leadsto \color{blue}{x \cdot \sqrt{0.333333333333333315}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification26.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -7.51459440820440755 \cdot 10^{142}:\\ \;\;\;\;-\left(x \cdot \sqrt{\sqrt{0.333333333333333315}}\right) \cdot \sqrt{\sqrt{0.333333333333333315}}\\ \mathbf{elif}\;x \le -9.04578056600420164 \cdot 10^{-238}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;x \le 1.7225630620991519 \cdot 10^{-270}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;x \le 4.24489976765657814 \cdot 10^{97}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \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)))