Average Error: 38.2 → 25.7
Time: 15.6s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -9.992239971519507908562597684135415503734 \cdot 10^{87}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 1.432510219356761964890031702559272248021 \cdot 10^{148}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\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 -9.992239971519507908562597684135415503734 \cdot 10^{87}:\\
\;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

\mathbf{elif}\;z \le 1.432510219356761964890031702559272248021 \cdot 10^{148}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}\\

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

\end{array}
double f(double x, double y, double z) {
        double r768864 = x;
        double r768865 = r768864 * r768864;
        double r768866 = y;
        double r768867 = r768866 * r768866;
        double r768868 = r768865 + r768867;
        double r768869 = z;
        double r768870 = r768869 * r768869;
        double r768871 = r768868 + r768870;
        double r768872 = 3.0;
        double r768873 = r768871 / r768872;
        double r768874 = sqrt(r768873);
        return r768874;
}

double f(double x, double y, double z) {
        double r768875 = z;
        double r768876 = -9.992239971519508e+87;
        bool r768877 = r768875 <= r768876;
        double r768878 = 0.3333333333333333;
        double r768879 = sqrt(r768878);
        double r768880 = r768875 * r768879;
        double r768881 = -r768880;
        double r768882 = 1.432510219356762e+148;
        bool r768883 = r768875 <= r768882;
        double r768884 = x;
        double r768885 = y;
        double r768886 = 2.0;
        double r768887 = pow(r768875, r768886);
        double r768888 = fma(r768885, r768885, r768887);
        double r768889 = fma(r768884, r768884, r768888);
        double r768890 = r768878 * r768889;
        double r768891 = sqrt(r768890);
        double r768892 = r768883 ? r768891 : r768880;
        double r768893 = r768877 ? r768881 : r768892;
        return r768893;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.7
Herbie25.7
\[\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 < -9.992239971519508e+87

    1. Initial program 53.9

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

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

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

    if -9.992239971519508e+87 < z < 1.432510219356762e+148

    1. Initial program 29.5

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

      \[\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 29.5

      \[\leadsto \sqrt{\color{blue}{0.3333333333333333148296162562473909929395 \cdot {x}^{2} + \left(0.3333333333333333148296162562473909929395 \cdot {y}^{2} + 0.3333333333333333148296162562473909929395 \cdot {z}^{2}\right)}}\]
    4. Simplified29.5

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

    if 1.432510219356762e+148 < z

    1. Initial program 63.1

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

      \[\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 13.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -9.992239971519507908562597684135415503734 \cdot 10^{87}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le 1.432510219356761964890031702559272248021 \cdot 10^{148}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]

Reproduce

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