Average Error: 38.2 → 25.8
Time: 18.0s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.032360807104304222767006935185600686132 \cdot 10^{150}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 9.721182782519791859145578731742130725489 \cdot 10^{117}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \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 -1.032360807104304222767006935185600686132 \cdot 10^{150}:\\
\;\;\;\;\frac{-z}{\sqrt{3}}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r44355512 = x;
        double r44355513 = r44355512 * r44355512;
        double r44355514 = y;
        double r44355515 = r44355514 * r44355514;
        double r44355516 = r44355513 + r44355515;
        double r44355517 = z;
        double r44355518 = r44355517 * r44355517;
        double r44355519 = r44355516 + r44355518;
        double r44355520 = 3.0;
        double r44355521 = r44355519 / r44355520;
        double r44355522 = sqrt(r44355521);
        return r44355522;
}

double f(double x, double y, double z) {
        double r44355523 = z;
        double r44355524 = -1.0323608071043042e+150;
        bool r44355525 = r44355523 <= r44355524;
        double r44355526 = -r44355523;
        double r44355527 = 3.0;
        double r44355528 = sqrt(r44355527);
        double r44355529 = r44355526 / r44355528;
        double r44355530 = 9.721182782519792e+117;
        bool r44355531 = r44355523 <= r44355530;
        double r44355532 = 0.3333333333333333;
        double r44355533 = x;
        double r44355534 = y;
        double r44355535 = r44355534 * r44355534;
        double r44355536 = fma(r44355533, r44355533, r44355535);
        double r44355537 = fma(r44355523, r44355523, r44355536);
        double r44355538 = r44355532 * r44355537;
        double r44355539 = sqrt(r44355538);
        double r44355540 = r44355523 / r44355528;
        double r44355541 = r44355531 ? r44355539 : r44355540;
        double r44355542 = r44355525 ? r44355529 : r44355541;
        return r44355542;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.9
Herbie25.8
\[\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 < -1.0323608071043042e+150

    1. Initial program 62.9

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified62.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 sqrt-div62.9

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

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

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

    if -1.0323608071043042e+150 < z < 9.721182782519792e+117

    1. Initial program 29.6

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

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

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

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

    if 9.721182782519792e+117 < z

    1. Initial program 56.4

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified56.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 sqrt-div56.4

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

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

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

Reproduce

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