Average Error: 35.0 → 25.6
Time: 18.1s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -6.208442960829875 \cdot 10^{+137}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{elif}\;x \le -1.9111814054112648 \cdot 10^{-98}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}{3.0}}\\ \mathbf{elif}\;x \le -8.598460374864875 \cdot 10^{-202}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \mathbf{elif}\;x \le 9.794009169674087 \cdot 10^{+109}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot x\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}
\begin{array}{l}
\mathbf{if}\;x \le -6.208442960829875 \cdot 10^{+137}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\

\mathbf{elif}\;x \le -1.9111814054112648 \cdot 10^{-98}:\\
\;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}{3.0}}\\

\mathbf{elif}\;x \le -8.598460374864875 \cdot 10^{-202}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot z\\

\mathbf{elif}\;x \le 9.794009169674087 \cdot 10^{+109}:\\
\;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}{3.0}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot x\\

\end{array}
double f(double x, double y, double z) {
        double r42367042 = x;
        double r42367043 = r42367042 * r42367042;
        double r42367044 = y;
        double r42367045 = r42367044 * r42367044;
        double r42367046 = r42367043 + r42367045;
        double r42367047 = z;
        double r42367048 = r42367047 * r42367047;
        double r42367049 = r42367046 + r42367048;
        double r42367050 = 3.0;
        double r42367051 = r42367049 / r42367050;
        double r42367052 = sqrt(r42367051);
        return r42367052;
}

double f(double x, double y, double z) {
        double r42367053 = x;
        double r42367054 = -6.208442960829875e+137;
        bool r42367055 = r42367053 <= r42367054;
        double r42367056 = 0.3333333333333333;
        double r42367057 = sqrt(r42367056);
        double r42367058 = -r42367053;
        double r42367059 = r42367057 * r42367058;
        double r42367060 = -1.9111814054112648e-98;
        bool r42367061 = r42367053 <= r42367060;
        double r42367062 = z;
        double r42367063 = y;
        double r42367064 = r42367053 * r42367053;
        double r42367065 = fma(r42367063, r42367063, r42367064);
        double r42367066 = fma(r42367062, r42367062, r42367065);
        double r42367067 = 3.0;
        double r42367068 = r42367066 / r42367067;
        double r42367069 = sqrt(r42367068);
        double r42367070 = -8.598460374864875e-202;
        bool r42367071 = r42367053 <= r42367070;
        double r42367072 = r42367057 * r42367062;
        double r42367073 = 9.794009169674087e+109;
        bool r42367074 = r42367053 <= r42367073;
        double r42367075 = r42367057 * r42367053;
        double r42367076 = r42367074 ? r42367069 : r42367075;
        double r42367077 = r42367071 ? r42367072 : r42367076;
        double r42367078 = r42367061 ? r42367069 : r42367077;
        double r42367079 = r42367055 ? r42367059 : r42367078;
        return r42367079;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original35.0
Target23.5
Herbie25.6
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;\frac{-z}{\sqrt{3.0}}\\ \mathbf{elif}\;z \lt 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if x < -6.208442960829875e+137

    1. Initial program 56.2

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
    2. Taylor expanded around -inf 15.3

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{0.3333333333333333}\right)}\]
    3. Simplified15.3

      \[\leadsto \color{blue}{-x \cdot \sqrt{0.3333333333333333}}\]

    if -6.208442960829875e+137 < x < -1.9111814054112648e-98 or -8.598460374864875e-202 < x < 9.794009169674087e+109

    1. Initial program 27.1

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity27.1

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{1 \cdot 3.0}}}\]
    4. Applied associate-/r*27.1

      \[\leadsto \sqrt{\color{blue}{\frac{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{1}}{3.0}}}\]
    5. Simplified27.1

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

    if -1.9111814054112648e-98 < x < -8.598460374864875e-202

    1. Initial program 28.2

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
    2. Taylor expanded around 0 46.7

      \[\leadsto \color{blue}{z \cdot \sqrt{0.3333333333333333}}\]

    if 9.794009169674087e+109 < x

    1. Initial program 52.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
    2. Taylor expanded around inf 16.8

      \[\leadsto \color{blue}{x \cdot \sqrt{0.3333333333333333}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification25.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -6.208442960829875 \cdot 10^{+137}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-x\right)\\ \mathbf{elif}\;x \le -1.9111814054112648 \cdot 10^{-98}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}{3.0}}\\ \mathbf{elif}\;x \le -8.598460374864875 \cdot 10^{-202}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \mathbf{elif}\;x \le 9.794009169674087 \cdot 10^{+109}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot x\\ \end{array}\]

Reproduce

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