Average Error: 37.8 → 25.9
Time: 12.9s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.717333272640103545901465763102229690483 \cdot 10^{89}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 2.673911141236909644899525953792608723737 \cdot 10^{-195}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right) \cdot 0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 3.267479005742163364041445759339258528358 \cdot 10^{-158}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot z\\ \mathbf{elif}\;y \le 1.902370008063671102963145796212009492267 \cdot 10^{138}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right) \cdot 0.3333333333333333148296162562473909929395}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -1.717333272640103545901465763102229690483 \cdot 10^{89}:\\
\;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

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

\mathbf{elif}\;y \le 3.267479005742163364041445759339258528358 \cdot 10^{-158}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot z\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r661400 = x;
        double r661401 = r661400 * r661400;
        double r661402 = y;
        double r661403 = r661402 * r661402;
        double r661404 = r661401 + r661403;
        double r661405 = z;
        double r661406 = r661405 * r661405;
        double r661407 = r661404 + r661406;
        double r661408 = 3.0;
        double r661409 = r661407 / r661408;
        double r661410 = sqrt(r661409);
        return r661410;
}

double f(double x, double y, double z) {
        double r661411 = y;
        double r661412 = -1.7173332726401035e+89;
        bool r661413 = r661411 <= r661412;
        double r661414 = -r661411;
        double r661415 = 0.3333333333333333;
        double r661416 = sqrt(r661415);
        double r661417 = r661414 * r661416;
        double r661418 = 2.6739111412369096e-195;
        bool r661419 = r661411 <= r661418;
        double r661420 = x;
        double r661421 = z;
        double r661422 = 2.0;
        double r661423 = pow(r661421, r661422);
        double r661424 = fma(r661420, r661420, r661423);
        double r661425 = fma(r661411, r661411, r661424);
        double r661426 = r661425 * r661415;
        double r661427 = sqrt(r661426);
        double r661428 = 3.2674790057421634e-158;
        bool r661429 = r661411 <= r661428;
        double r661430 = r661416 * r661421;
        double r661431 = 1.902370008063671e+138;
        bool r661432 = r661411 <= r661431;
        double r661433 = r661416 * r661411;
        double r661434 = r661432 ? r661427 : r661433;
        double r661435 = r661429 ? r661430 : r661434;
        double r661436 = r661419 ? r661427 : r661435;
        double r661437 = r661413 ? r661417 : r661436;
        return r661437;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.8
Herbie25.9
\[\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 4 regimes
  2. if y < -1.7173332726401035e+89

    1. Initial program 53.6

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

      \[\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 add-cube-cbrt53.6

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

      \[\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)}}}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}\]
    6. Applied times-frac53.6

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

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right)}}{\sqrt[3]{3} \cdot \sqrt[3]{3}}} \cdot \frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\sqrt[3]{3}}}\]
    8. Simplified53.6

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    10. Simplified19.4

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

    if -1.7173332726401035e+89 < y < 2.6739111412369096e-195 or 3.2674790057421634e-158 < y < 1.902370008063671e+138

    1. Initial program 28.9

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

      \[\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 0 28.9

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

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

    if 2.6739111412369096e-195 < y < 3.2674790057421634e-158

    1. Initial program 33.2

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

      \[\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 add-cube-cbrt33.2

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

      \[\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)}}}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}\]
    6. Applied times-frac33.3

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

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

      \[\leadsto \sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right)}}{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \color{blue}{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right)}}{\sqrt[3]{3}}}}\]
    9. Taylor expanded around 0 48.1

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

    if 1.902370008063671e+138 < y

    1. Initial program 60.7

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.717333272640103545901465763102229690483 \cdot 10^{89}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 2.673911141236909644899525953792608723737 \cdot 10^{-195}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right) \cdot 0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 3.267479005742163364041445759339258528358 \cdot 10^{-158}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot z\\ \mathbf{elif}\;y \le 1.902370008063671102963145796212009492267 \cdot 10^{138}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, {z}^{2}\right)\right) \cdot 0.3333333333333333148296162562473909929395}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]

Reproduce

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