Average Error: 37.4 → 25.3
Time: 14.8s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\ \;\;\;\;-\frac{y}{\sqrt{3}}\\ \mathbf{elif}\;y \le 4.581660079438061697636144959988270294902 \cdot 10^{111}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;y \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}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\
\;\;\;\;-\frac{y}{\sqrt{3}}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r24710050 = x;
        double r24710051 = r24710050 * r24710050;
        double r24710052 = y;
        double r24710053 = r24710052 * r24710052;
        double r24710054 = r24710051 + r24710053;
        double r24710055 = z;
        double r24710056 = r24710055 * r24710055;
        double r24710057 = r24710054 + r24710056;
        double r24710058 = 3.0;
        double r24710059 = r24710057 / r24710058;
        double r24710060 = sqrt(r24710059);
        return r24710060;
}

double f(double x, double y, double z) {
        double r24710061 = y;
        double r24710062 = -7.465878436556341e+142;
        bool r24710063 = r24710061 <= r24710062;
        double r24710064 = 3.0;
        double r24710065 = sqrt(r24710064);
        double r24710066 = r24710061 / r24710065;
        double r24710067 = -r24710066;
        double r24710068 = 4.5816600794380617e+111;
        bool r24710069 = r24710061 <= r24710068;
        double r24710070 = x;
        double r24710071 = z;
        double r24710072 = r24710071 * r24710071;
        double r24710073 = fma(r24710070, r24710070, r24710072);
        double r24710074 = fma(r24710061, r24710061, r24710073);
        double r24710075 = sqrt(r24710074);
        double r24710076 = r24710075 / r24710065;
        double r24710077 = 0.3333333333333333;
        double r24710078 = sqrt(r24710077);
        double r24710079 = r24710061 * r24710078;
        double r24710080 = r24710069 ? r24710076 : r24710079;
        double r24710081 = r24710063 ? r24710067 : r24710080;
        return r24710081;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.4
Target25.2
Herbie25.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 3 regimes
  2. if y < -7.465878436556341e+142

    1. Initial program 61.3

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}}\]
    3. Using strategy rm
    4. Applied sqrt-div61.3

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

      \[\leadsto \frac{\color{blue}{-1 \cdot y}}{\sqrt{3}}\]
    6. Simplified16.7

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

    if -7.465878436556341e+142 < y < 4.5816600794380617e+111

    1. Initial program 28.4

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}}\]
    3. Using strategy rm
    4. Applied sqrt-div28.6

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

    if 4.5816600794380617e+111 < y

    1. Initial program 56.2

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

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

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

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

Reproduce

herbie shell --seed 2019172 +o rules:numerics
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"

  :herbie-target
  (if (< z -6.396479394109776e+136) (/ (- z) (sqrt 3.0)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3.0)) (* (sqrt 0.3333333333333333) z)))

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