Average Error: 37.4 → 25.3
Time: 19.1s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\ \;\;\;\;\frac{-y}{\sqrt{3}}\\ \mathbf{elif}\;y \le 4.581660079438061697636144959988270294902 \cdot 10^{111}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;y \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -7.465878436556341499171603430091390177527 \cdot 10^{142}:\\
\;\;\;\;\frac{-y}{\sqrt{3}}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r30860001 = x;
        double r30860002 = r30860001 * r30860001;
        double r30860003 = y;
        double r30860004 = r30860003 * r30860003;
        double r30860005 = r30860002 + r30860004;
        double r30860006 = z;
        double r30860007 = r30860006 * r30860006;
        double r30860008 = r30860005 + r30860007;
        double r30860009 = 3.0;
        double r30860010 = r30860008 / r30860009;
        double r30860011 = sqrt(r30860010);
        return r30860011;
}

double f(double x, double y, double z) {
        double r30860012 = y;
        double r30860013 = -7.465878436556341e+142;
        bool r30860014 = r30860012 <= r30860013;
        double r30860015 = -r30860012;
        double r30860016 = 3.0;
        double r30860017 = sqrt(r30860016);
        double r30860018 = r30860015 / r30860017;
        double r30860019 = 4.5816600794380617e+111;
        bool r30860020 = r30860012 <= r30860019;
        double r30860021 = x;
        double r30860022 = z;
        double r30860023 = r30860022 * r30860022;
        double r30860024 = fma(r30860021, r30860021, r30860023);
        double r30860025 = fma(r30860012, r30860012, r30860024);
        double r30860026 = sqrt(r30860025);
        double r30860027 = r30860026 / r30860017;
        double r30860028 = 0.3333333333333333;
        double r30860029 = sqrt(r30860028);
        double r30860030 = r30860012 * r30860029;
        double r30860031 = r30860020 ? r30860027 : r30860030;
        double r30860032 = r30860014 ? r30860018 : r30860031;
        return r30860032;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.4
Target25.2
Herbie25.3
\[\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 < -7.465878436556341e+142

    1. Initial program 61.3

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

      \[\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 sqrt-div61.3

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

      \[\leadsto \color{blue}{-1 \cdot \frac{y}{\sqrt{3}}}\]
    6. Simplified16.7

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

    if -7.465878436556341e+142 < y < 4.5816600794380617e+111

    1. Initial program 28.4

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

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

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

    if 4.5816600794380617e+111 < y

    1. Initial program 56.2

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

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

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

Reproduce

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