Average Error: 38.2 → 26.0
Time: 19.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.494510111118953004235362423272051710384 \cdot 10^{114}:\\ \;\;\;\;\left(\left(-\sqrt{\frac{1}{\sqrt[3]{3}}}\right) \cdot y\right) \cdot \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}\\ \mathbf{elif}\;y \le 9.809026589055475778410860542135506813445 \cdot 10^{72}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{3} \cdot \sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}\\ \mathbf{else}:\\ \;\;\;\;\left(y \cdot \sqrt{\frac{1}{\sqrt[3]{3}}}\right) \cdot \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -1.494510111118953004235362423272051710384 \cdot 10^{114}:\\
\;\;\;\;\left(\left(-\sqrt{\frac{1}{\sqrt[3]{3}}}\right) \cdot y\right) \cdot \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}\\

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

\mathbf{else}:\\
\;\;\;\;\left(y \cdot \sqrt{\frac{1}{\sqrt[3]{3}}}\right) \cdot \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}\\

\end{array}
double f(double x, double y, double z) {
        double r39742738 = x;
        double r39742739 = r39742738 * r39742738;
        double r39742740 = y;
        double r39742741 = r39742740 * r39742740;
        double r39742742 = r39742739 + r39742741;
        double r39742743 = z;
        double r39742744 = r39742743 * r39742743;
        double r39742745 = r39742742 + r39742744;
        double r39742746 = 3.0;
        double r39742747 = r39742745 / r39742746;
        double r39742748 = sqrt(r39742747);
        return r39742748;
}

double f(double x, double y, double z) {
        double r39742749 = y;
        double r39742750 = -1.494510111118953e+114;
        bool r39742751 = r39742749 <= r39742750;
        double r39742752 = 1.0;
        double r39742753 = 3.0;
        double r39742754 = cbrt(r39742753);
        double r39742755 = r39742752 / r39742754;
        double r39742756 = sqrt(r39742755);
        double r39742757 = -r39742756;
        double r39742758 = r39742757 * r39742749;
        double r39742759 = r39742754 * r39742754;
        double r39742760 = r39742752 / r39742759;
        double r39742761 = sqrt(r39742760);
        double r39742762 = r39742758 * r39742761;
        double r39742763 = 9.809026589055476e+72;
        bool r39742764 = r39742749 <= r39742763;
        double r39742765 = x;
        double r39742766 = z;
        double r39742767 = r39742766 * r39742766;
        double r39742768 = fma(r39742765, r39742765, r39742767);
        double r39742769 = fma(r39742749, r39742749, r39742768);
        double r39742770 = sqrt(r39742769);
        double r39742771 = r39742770 / r39742753;
        double r39742772 = r39742771 * r39742770;
        double r39742773 = sqrt(r39742772);
        double r39742774 = r39742749 * r39742756;
        double r39742775 = r39742774 * r39742761;
        double r39742776 = r39742764 ? r39742773 : r39742775;
        double r39742777 = r39742751 ? r39742762 : r39742776;
        return r39742777;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target26.1
Herbie26.0
\[\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 y < -1.494510111118953e+114

    1. Initial program 56.1

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

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

      \[\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 *-un-lft-identity56.1

      \[\leadsto \sqrt{\frac{\color{blue}{1 \cdot \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-frac56.1

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

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

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

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

    if -1.494510111118953e+114 < y < 9.809026589055476e+72

    1. Initial program 29.9

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

      \[\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 *-un-lft-identity29.9

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

      \[\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)}}}{1 \cdot 3}}\]
    6. Applied times-frac29.9

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

    if 9.809026589055476e+72 < y

    1. Initial program 51.0

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

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

      \[\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 *-un-lft-identity51.0

      \[\leadsto \sqrt{\frac{\color{blue}{1 \cdot \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-frac51.0

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

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

      \[\leadsto \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}} \cdot \color{blue}{\left(\sqrt{\frac{1}{\sqrt[3]{3}}} \cdot y\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.494510111118953004235362423272051710384 \cdot 10^{114}:\\ \;\;\;\;\left(\left(-\sqrt{\frac{1}{\sqrt[3]{3}}}\right) \cdot y\right) \cdot \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}\\ \mathbf{elif}\;y \le 9.809026589055475778410860542135506813445 \cdot 10^{72}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{3} \cdot \sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}\\ \mathbf{else}:\\ \;\;\;\;\left(y \cdot \sqrt{\frac{1}{\sqrt[3]{3}}}\right) \cdot \sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}\\ \end{array}\]

Reproduce

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