Average Error: 38.1 → 25.6
Time: 21.3s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.74693859576126102544728952505913367202 \cdot 10^{111}:\\ \;\;\;\;-\frac{\frac{z}{\left|\sqrt[3]{3}\right|}}{\sqrt{\sqrt[3]{3}}}\\ \mathbf{elif}\;z \le 4.17182871699966700452276153071931598076 \cdot 10^{130}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -3.74693859576126102544728952505913367202 \cdot 10^{111}:\\
\;\;\;\;-\frac{\frac{z}{\left|\sqrt[3]{3}\right|}}{\sqrt{\sqrt[3]{3}}}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{z}{\sqrt{3}}\\

\end{array}
double f(double x, double y, double z) {
        double r561379 = x;
        double r561380 = r561379 * r561379;
        double r561381 = y;
        double r561382 = r561381 * r561381;
        double r561383 = r561380 + r561382;
        double r561384 = z;
        double r561385 = r561384 * r561384;
        double r561386 = r561383 + r561385;
        double r561387 = 3.0;
        double r561388 = r561386 / r561387;
        double r561389 = sqrt(r561388);
        return r561389;
}

double f(double x, double y, double z) {
        double r561390 = z;
        double r561391 = -3.746938595761261e+111;
        bool r561392 = r561390 <= r561391;
        double r561393 = 3.0;
        double r561394 = cbrt(r561393);
        double r561395 = fabs(r561394);
        double r561396 = r561390 / r561395;
        double r561397 = sqrt(r561394);
        double r561398 = r561396 / r561397;
        double r561399 = -r561398;
        double r561400 = 4.171828716999667e+130;
        bool r561401 = r561390 <= r561400;
        double r561402 = x;
        double r561403 = y;
        double r561404 = r561403 * r561403;
        double r561405 = fma(r561402, r561402, r561404);
        double r561406 = fma(r561390, r561390, r561405);
        double r561407 = sqrt(r561406);
        double r561408 = 1.0;
        double r561409 = r561408 / r561393;
        double r561410 = sqrt(r561409);
        double r561411 = r561407 * r561410;
        double r561412 = sqrt(r561393);
        double r561413 = r561390 / r561412;
        double r561414 = r561401 ? r561411 : r561413;
        double r561415 = r561392 ? r561399 : r561414;
        return r561415;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.1
Target25.6
Herbie25.6
\[\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 < -3.746938595761261e+111

    1. Initial program 56.5

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

      \[\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-sqrt56.5

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

      \[\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)}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    6. Applied times-frac56.5

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

      \[\leadsto \color{blue}{-1 \cdot \frac{z}{\sqrt{3}}}\]
    8. Simplified18.0

      \[\leadsto \color{blue}{-\frac{z}{\sqrt{3}}}\]
    9. Using strategy rm
    10. Applied add-cube-cbrt18.0

      \[\leadsto -\frac{z}{\sqrt{\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    11. Applied sqrt-prod18.0

      \[\leadsto -\frac{z}{\color{blue}{\sqrt{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \sqrt{\sqrt[3]{3}}}}\]
    12. Applied associate-/r*18.0

      \[\leadsto -\color{blue}{\frac{\frac{z}{\sqrt{\sqrt[3]{3} \cdot \sqrt[3]{3}}}}{\sqrt{\sqrt[3]{3}}}}\]
    13. Simplified18.0

      \[\leadsto -\frac{\color{blue}{\frac{z}{\left|\sqrt[3]{3}\right|}}}{\sqrt{\sqrt[3]{3}}}\]

    if -3.746938595761261e+111 < z < 4.171828716999667e+130

    1. Initial program 29.4

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

      \[\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 div-inv29.4

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

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

    if 4.171828716999667e+130 < z

    1. Initial program 59.0

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

      \[\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-sqrt59.0

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

      \[\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)}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    6. Applied times-frac59.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -3.74693859576126102544728952505913367202 \cdot 10^{111}:\\ \;\;\;\;-\frac{\frac{z}{\left|\sqrt[3]{3}\right|}}{\sqrt{\sqrt[3]{3}}}\\ \mathbf{elif}\;z \le 4.17182871699966700452276153071931598076 \cdot 10^{130}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]

Reproduce

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