Average Error: 37.4 → 25.2
Time: 24.6s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.017165361738837830382572779149406364537 \cdot 10^{89}:\\ \;\;\;\;\left(-z\right) \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 2.188536514136267218025323125256231373834 \cdot 10^{121}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -3.017165361738837830382572779149406364537 \cdot 10^{89}:\\
\;\;\;\;\left(-z\right) \cdot \sqrt{\frac{1}{3}}\\

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

\mathbf{else}:\\
\;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\

\end{array}
double f(double x, double y, double z) {
        double r490519 = x;
        double r490520 = r490519 * r490519;
        double r490521 = y;
        double r490522 = r490521 * r490521;
        double r490523 = r490520 + r490522;
        double r490524 = z;
        double r490525 = r490524 * r490524;
        double r490526 = r490523 + r490525;
        double r490527 = 3.0;
        double r490528 = r490526 / r490527;
        double r490529 = sqrt(r490528);
        return r490529;
}

double f(double x, double y, double z) {
        double r490530 = z;
        double r490531 = -3.017165361738838e+89;
        bool r490532 = r490530 <= r490531;
        double r490533 = -r490530;
        double r490534 = 1.0;
        double r490535 = 3.0;
        double r490536 = r490534 / r490535;
        double r490537 = sqrt(r490536);
        double r490538 = r490533 * r490537;
        double r490539 = 2.188536514136267e+121;
        bool r490540 = r490530 <= r490539;
        double r490541 = x;
        double r490542 = y;
        double r490543 = r490542 * r490542;
        double r490544 = fma(r490541, r490541, r490543);
        double r490545 = fma(r490530, r490530, r490544);
        double r490546 = sqrt(r490545);
        double r490547 = r490546 * r490537;
        double r490548 = sqrt(r490535);
        double r490549 = r490530 / r490548;
        double r490550 = -r490549;
        double r490551 = fabs(r490550);
        double r490552 = r490540 ? r490547 : r490551;
        double r490553 = r490532 ? r490538 : r490552;
        return r490553;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.4
Target24.9
Herbie25.2
\[\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.017165361738838e+89

    1. Initial program 53.7

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

      \[\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-inv53.7

      \[\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-prod53.8

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

      \[\leadsto \color{blue}{\left(-1 \cdot z\right)} \cdot \sqrt{\frac{1}{3}}\]
    7. Simplified20.3

      \[\leadsto \color{blue}{\left(-z\right)} \cdot \sqrt{\frac{1}{3}}\]

    if -3.017165361738838e+89 < z < 2.188536514136267e+121

    1. Initial program 28.5

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

      \[\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-prod28.6

      \[\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 2.188536514136267e+121 < z

    1. Initial program 56.9

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

      \[\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.9

      \[\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.9

      \[\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.9

      \[\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. Applied rem-sqrt-square56.9

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

      \[\leadsto \left|\color{blue}{-1 \cdot \frac{z}{\sqrt{3}}}\right|\]
    9. Simplified16.0

      \[\leadsto \left|\color{blue}{-\frac{z}{\sqrt{3}}}\right|\]
  3. Recombined 3 regimes into one program.
  4. Final simplification25.2

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

Reproduce

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