Average Error: 38.0 → 25.8
Time: 20.7s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -6.3156849696486501 \cdot 10^{142}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 5.86560349046651164 \cdot 10^{107}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.333333333333333315}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -6.3156849696486501 \cdot 10^{142}:\\
\;\;\;\;\frac{-z}{\sqrt{3}}\\

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

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{0.333333333333333315}\\

\end{array}
double f(double x, double y, double z) {
        double r585660 = x;
        double r585661 = r585660 * r585660;
        double r585662 = y;
        double r585663 = r585662 * r585662;
        double r585664 = r585661 + r585663;
        double r585665 = z;
        double r585666 = r585665 * r585665;
        double r585667 = r585664 + r585666;
        double r585668 = 3.0;
        double r585669 = r585667 / r585668;
        double r585670 = sqrt(r585669);
        return r585670;
}

double f(double x, double y, double z) {
        double r585671 = z;
        double r585672 = -6.31568496964865e+142;
        bool r585673 = r585671 <= r585672;
        double r585674 = -r585671;
        double r585675 = 3.0;
        double r585676 = sqrt(r585675);
        double r585677 = r585674 / r585676;
        double r585678 = 5.8656034904665116e+107;
        bool r585679 = r585671 <= r585678;
        double r585680 = x;
        double r585681 = y;
        double r585682 = r585681 * r585681;
        double r585683 = fma(r585680, r585680, r585682);
        double r585684 = fma(r585671, r585671, r585683);
        double r585685 = sqrt(r585684);
        double r585686 = 1.0;
        double r585687 = r585686 / r585675;
        double r585688 = sqrt(r585687);
        double r585689 = r585685 * r585688;
        double r585690 = 0.3333333333333333;
        double r585691 = sqrt(r585690);
        double r585692 = r585671 * r585691;
        double r585693 = r585679 ? r585689 : r585692;
        double r585694 = r585673 ? r585677 : r585693;
        return r585694;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.0
Target25.7
Herbie25.8
\[\begin{array}{l} \mathbf{if}\;z \lt -6.3964793941097758 \cdot 10^{136}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \lt 7.3202936944041821 \cdot 10^{117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.333333333333333315} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if z < -6.31568496964865e+142

    1. Initial program 62.0

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

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

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

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

    if -6.31568496964865e+142 < z < 5.8656034904665116e+107

    1. Initial program 29.2

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

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

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

      \[\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 5.8656034904665116e+107 < z

    1. Initial program 55.5

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -6.3156849696486501 \cdot 10^{142}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 5.86560349046651164 \cdot 10^{107}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Reproduce

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