Average Error: 38.2 → 26.7
Time: 20.7s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.532659711124672793051277661217489965066 \cdot 10^{87}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-y\right)\\ \mathbf{elif}\;y \le 1.862635880027250573731989417485099125046 \cdot 10^{138}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -1.532659711124672793051277661217489965066 \cdot 10^{87}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-y\right)\\

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

\mathbf{else}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\

\end{array}
double f(double x, double y, double z) {
        double r39734020 = x;
        double r39734021 = r39734020 * r39734020;
        double r39734022 = y;
        double r39734023 = r39734022 * r39734022;
        double r39734024 = r39734021 + r39734023;
        double r39734025 = z;
        double r39734026 = r39734025 * r39734025;
        double r39734027 = r39734024 + r39734026;
        double r39734028 = 3.0;
        double r39734029 = r39734027 / r39734028;
        double r39734030 = sqrt(r39734029);
        return r39734030;
}

double f(double x, double y, double z) {
        double r39734031 = y;
        double r39734032 = -1.5326597111246728e+87;
        bool r39734033 = r39734031 <= r39734032;
        double r39734034 = 0.3333333333333333;
        double r39734035 = sqrt(r39734034);
        double r39734036 = -r39734031;
        double r39734037 = r39734035 * r39734036;
        double r39734038 = 1.8626358800272506e+138;
        bool r39734039 = r39734031 <= r39734038;
        double r39734040 = z;
        double r39734041 = x;
        double r39734042 = r39734041 * r39734041;
        double r39734043 = fma(r39734040, r39734040, r39734042);
        double r39734044 = fma(r39734031, r39734031, r39734043);
        double r39734045 = sqrt(r39734044);
        double r39734046 = 3.0;
        double r39734047 = sqrt(r39734046);
        double r39734048 = r39734045 / r39734047;
        double r39734049 = r39734035 * r39734031;
        double r39734050 = r39734039 ? r39734048 : r39734049;
        double r39734051 = r39734033 ? r39734037 : r39734050;
        return r39734051;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.9
Herbie26.7
\[\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.5326597111246728e+87

    1. Initial program 53.2

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    4. Simplified21.7

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

    if -1.5326597111246728e+87 < y < 1.8626358800272506e+138

    1. Initial program 30.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified30.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 div-inv30.0

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

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

      \[\leadsto \sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)} \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{3}}}\]
    8. Applied associate-*r/30.1

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

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

    if 1.8626358800272506e+138 < y

    1. Initial program 60.0

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

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

      \[\leadsto \color{blue}{y \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.7

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

Reproduce

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