Average Error: 38.5 → 26.1
Time: 19.8s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.310322163651342336927929105577697628919 \cdot 10^{70}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 7.196367607327084015814402412865153880207 \cdot 10^{111}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\frac{3}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -1.310322163651342336927929105577697628919 \cdot 10^{70}:\\
\;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

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

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

\end{array}
double f(double x, double y, double z) {
        double r480982 = x;
        double r480983 = r480982 * r480982;
        double r480984 = y;
        double r480985 = r480984 * r480984;
        double r480986 = r480983 + r480985;
        double r480987 = z;
        double r480988 = r480987 * r480987;
        double r480989 = r480986 + r480988;
        double r480990 = 3.0;
        double r480991 = r480989 / r480990;
        double r480992 = sqrt(r480991);
        return r480992;
}

double f(double x, double y, double z) {
        double r480993 = z;
        double r480994 = -1.3103221636513423e+70;
        bool r480995 = r480993 <= r480994;
        double r480996 = 0.3333333333333333;
        double r480997 = sqrt(r480996);
        double r480998 = r480993 * r480997;
        double r480999 = -r480998;
        double r481000 = 7.196367607327084e+111;
        bool r481001 = r480993 <= r481000;
        double r481002 = x;
        double r481003 = y;
        double r481004 = r481003 * r481003;
        double r481005 = fma(r481002, r481002, r481004);
        double r481006 = fma(r480993, r480993, r481005);
        double r481007 = sqrt(r481006);
        double r481008 = 3.0;
        double r481009 = r481008 / r481007;
        double r481010 = r481007 / r481009;
        double r481011 = sqrt(r481010);
        double r481012 = r481001 ? r481011 : r480998;
        double r481013 = r480995 ? r480999 : r481012;
        return r481013;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.5
Target26.0
Herbie26.1
\[\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 3 regimes
  2. if z < -1.3103221636513423e+70

    1. Initial program 51.9

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

      \[\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 -inf 20.5

      \[\leadsto \color{blue}{-1 \cdot \left(z \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    4. Simplified20.5

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

    if -1.3103221636513423e+70 < z < 7.196367607327084e+111

    1. Initial program 29.9

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

      \[\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)}}}{3}}\]
    5. Applied associate-/l*29.9

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

    if 7.196367607327084e+111 < z

    1. Initial program 56.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -1.310322163651342336927929105577697628919 \cdot 10^{70}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 7.196367607327084015814402412865153880207 \cdot 10^{111}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\frac{3}{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]

Reproduce

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