Average Error: 37.9 → 25.5
Time: 14.5s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.1005165876387523 \cdot 10^{94}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \le 2.7166609535198792 \cdot 10^{98}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(y, y, x \cdot x\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -1.1005165876387523 \cdot 10^{94}:\\
\;\;\;\;\frac{-z}{\sqrt{3}}\\

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

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\

\end{array}
double f(double x, double y, double z) {
        double r612081 = x;
        double r612082 = r612081 * r612081;
        double r612083 = y;
        double r612084 = r612083 * r612083;
        double r612085 = r612082 + r612084;
        double r612086 = z;
        double r612087 = r612086 * r612086;
        double r612088 = r612085 + r612087;
        double r612089 = 3.0;
        double r612090 = r612088 / r612089;
        double r612091 = sqrt(r612090);
        return r612091;
}

double f(double x, double y, double z) {
        double r612092 = z;
        double r612093 = -1.1005165876387523e+94;
        bool r612094 = r612092 <= r612093;
        double r612095 = -r612092;
        double r612096 = 3.0;
        double r612097 = sqrt(r612096);
        double r612098 = r612095 / r612097;
        double r612099 = 2.7166609535198792e+98;
        bool r612100 = r612092 <= r612099;
        double r612101 = y;
        double r612102 = x;
        double r612103 = r612102 * r612102;
        double r612104 = fma(r612101, r612101, r612103);
        double r612105 = fma(r612092, r612092, r612104);
        double r612106 = sqrt(r612105);
        double r612107 = r612106 / r612097;
        double r612108 = 1.0;
        double r612109 = r612108 / r612096;
        double r612110 = sqrt(r612109);
        double r612111 = r612092 * r612110;
        double r612112 = r612100 ? r612107 : r612111;
        double r612113 = r612094 ? r612098 : r612112;
        return r612113;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.9
Target25.2
Herbie25.5
\[\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 < -1.1005165876387523e+94

    1. Initial program 54.0

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{-1 \cdot z}}{\sqrt{3}}\]
    11. Simplified17.8

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

    if -1.1005165876387523e+94 < z < 2.7166609535198792e+98

    1. Initial program 29.4

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

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

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

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

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

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

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

    if 2.7166609535198792e+98 < z

    1. Initial program 53.6

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

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

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

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

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

Reproduce

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