Average Error: 35.7 → 24.3
Time: 18.7s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.3282248930815427 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-y\right)\\ \mathbf{elif}\;y \le 8.243533173233274 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}
\begin{array}{l}
\mathbf{if}\;y \le -1.3282248930815427 \cdot 10^{+154}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-y\right)\\

\mathbf{elif}\;y \le 8.243533173233274 \cdot 10^{+154}:\\
\;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3.0}}\\

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

\end{array}
double f(double x, double y, double z) {
        double r31028415 = x;
        double r31028416 = r31028415 * r31028415;
        double r31028417 = y;
        double r31028418 = r31028417 * r31028417;
        double r31028419 = r31028416 + r31028418;
        double r31028420 = z;
        double r31028421 = r31028420 * r31028420;
        double r31028422 = r31028419 + r31028421;
        double r31028423 = 3.0;
        double r31028424 = r31028422 / r31028423;
        double r31028425 = sqrt(r31028424);
        return r31028425;
}

double f(double x, double y, double z) {
        double r31028426 = y;
        double r31028427 = -1.3282248930815427e+154;
        bool r31028428 = r31028426 <= r31028427;
        double r31028429 = 0.3333333333333333;
        double r31028430 = sqrt(r31028429);
        double r31028431 = -r31028426;
        double r31028432 = r31028430 * r31028431;
        double r31028433 = 8.243533173233274e+154;
        bool r31028434 = r31028426 <= r31028433;
        double r31028435 = x;
        double r31028436 = z;
        double r31028437 = r31028436 * r31028436;
        double r31028438 = fma(r31028435, r31028435, r31028437);
        double r31028439 = fma(r31028426, r31028426, r31028438);
        double r31028440 = 3.0;
        double r31028441 = r31028439 / r31028440;
        double r31028442 = sqrt(r31028441);
        double r31028443 = r31028430 * r31028426;
        double r31028444 = r31028434 ? r31028442 : r31028443;
        double r31028445 = r31028428 ? r31028432 : r31028444;
        return r31028445;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original35.7
Target24.6
Herbie24.3
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;\frac{-z}{\sqrt{3.0}}\\ \mathbf{elif}\;z \lt 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -1.3282248930815427e+154

    1. Initial program 59.3

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

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

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\color{blue}{1 \cdot 3.0}}}\]
    5. Applied add-sqr-sqrt59.3

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)} \cdot \sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}}{1 \cdot 3.0}}\]
    6. Applied times-frac59.3

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{1} \cdot \frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{3.0}}}\]
    7. Applied sqrt-prod59.3

      \[\leadsto \color{blue}{\sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{1}} \cdot \sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{3.0}}}\]
    8. Using strategy rm
    9. Applied div-inv59.3

      \[\leadsto \sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{1}} \cdot \sqrt{\color{blue}{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)} \cdot \frac{1}{3.0}}}\]
    10. Applied sqrt-prod59.3

      \[\leadsto \sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{1}} \cdot \color{blue}{\left(\sqrt{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}} \cdot \sqrt{\frac{1}{3.0}}\right)}\]
    11. Applied associate-*r*59.3

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

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}} \cdot \sqrt{\frac{1}{3.0}}\]
    13. Taylor expanded around -inf 14.2

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333}\right)}\]
    14. Simplified14.2

      \[\leadsto \color{blue}{\sqrt{0.3333333333333333} \cdot \left(-y\right)}\]

    if -1.3282248930815427e+154 < y < 8.243533173233274e+154

    1. Initial program 27.7

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

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

    if 8.243533173233274e+154 < y

    1. Initial program 59.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3282248930815427 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-y\right)\\ \mathbf{elif}\;y \le 8.243533173233274 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot y\\ \end{array}\]

Reproduce

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