Average Error: 37.6 → 25.5
Time: 22.0s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \cdot y \le 2.708783180923696964124530140074069148178 \cdot 10^{257}:\\ \;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{y}{\sqrt{3}}\right|\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \cdot y \le 2.708783180923696964124530140074069148178 \cdot 10^{257}:\\
\;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\

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

\end{array}
double f(double x, double y, double z) {
        double r597824 = x;
        double r597825 = r597824 * r597824;
        double r597826 = y;
        double r597827 = r597826 * r597826;
        double r597828 = r597825 + r597827;
        double r597829 = z;
        double r597830 = r597829 * r597829;
        double r597831 = r597828 + r597830;
        double r597832 = 3.0;
        double r597833 = r597831 / r597832;
        double r597834 = sqrt(r597833);
        return r597834;
}

double f(double x, double y, double z) {
        double r597835 = y;
        double r597836 = r597835 * r597835;
        double r597837 = 2.708783180923697e+257;
        bool r597838 = r597836 <= r597837;
        double r597839 = z;
        double r597840 = x;
        double r597841 = fma(r597840, r597840, r597836);
        double r597842 = fma(r597839, r597839, r597841);
        double r597843 = sqrt(r597842);
        double r597844 = 3.0;
        double r597845 = sqrt(r597844);
        double r597846 = r597843 / r597845;
        double r597847 = fabs(r597846);
        double r597848 = r597835 / r597845;
        double r597849 = fabs(r597848);
        double r597850 = r597838 ? r597847 : r597849;
        return r597850;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.6
Target25.5
Herbie25.5
\[\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 2 regimes
  2. if (* y y) < 2.708783180923697e+257

    1. Initial program 29.0

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

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

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

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

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

    if 2.708783180923697e+257 < (* y y)

    1. Initial program 58.6

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

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

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

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

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

      \[\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 0 16.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \le 2.708783180923696964124530140074069148178 \cdot 10^{257}:\\ \;\;\;\;\left|\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}}{\sqrt{3}}\right|\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{y}{\sqrt{3}}\right|\\ \end{array}\]

Reproduce

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