Average Error: 37.7 → 25.3
Time: 15.1s
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 4.940656458412465441765687928682213723651 \cdot 10^{-324}:\\ \;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 2.796727373188044154649432399903811497242 \cdot 10^{-15}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;y \cdot y \le 3837990851.153699398040771484375:\\ \;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 1.10478303491952550487979066013675689444 \cdot 10^{208}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \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 4.940656458412465441765687928682213723651 \cdot 10^{-324}:\\
\;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\

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

\mathbf{elif}\;y \cdot y \le 3837990851.153699398040771484375:\\
\;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r560637 = x;
        double r560638 = r560637 * r560637;
        double r560639 = y;
        double r560640 = r560639 * r560639;
        double r560641 = r560638 + r560640;
        double r560642 = z;
        double r560643 = r560642 * r560642;
        double r560644 = r560641 + r560643;
        double r560645 = 3.0;
        double r560646 = r560644 / r560645;
        double r560647 = sqrt(r560646);
        return r560647;
}

double f(double x, double y, double z) {
        double r560648 = y;
        double r560649 = r560648 * r560648;
        double r560650 = 4.9406564584125e-324;
        bool r560651 = r560649 <= r560650;
        double r560652 = z;
        double r560653 = 3.0;
        double r560654 = sqrt(r560653);
        double r560655 = r560652 / r560654;
        double r560656 = -r560655;
        double r560657 = fabs(r560656);
        double r560658 = 2.796727373188044e-15;
        bool r560659 = r560649 <= r560658;
        double r560660 = x;
        double r560661 = fma(r560660, r560660, r560649);
        double r560662 = fma(r560652, r560652, r560661);
        double r560663 = sqrt(r560662);
        double r560664 = 1.0;
        double r560665 = r560664 / r560653;
        double r560666 = sqrt(r560665);
        double r560667 = r560663 * r560666;
        double r560668 = 3837990851.1536994;
        bool r560669 = r560649 <= r560668;
        double r560670 = 1.1047830349195255e+208;
        bool r560671 = r560649 <= r560670;
        double r560672 = r560648 / r560654;
        double r560673 = fabs(r560672);
        double r560674 = r560671 ? r560667 : r560673;
        double r560675 = r560669 ? r560657 : r560674;
        double r560676 = r560659 ? r560667 : r560675;
        double r560677 = r560651 ? r560657 : r560676;
        return r560677;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target25.6
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 y) < 4.9406564584125e-324 or 2.796727373188044e-15 < (* y y) < 3837990851.1536994

    1. Initial program 30.7

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

      \[\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-sqrt30.9

      \[\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-sqrt30.9

      \[\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-frac30.9

      \[\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-square30.8

      \[\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 -inf 31.3

      \[\leadsto \left|\color{blue}{-1 \cdot \frac{z}{\sqrt{3}}}\right|\]
    9. Simplified31.3

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

    if 4.9406564584125e-324 < (* y y) < 2.796727373188044e-15 or 3837990851.1536994 < (* y y) < 1.1047830349195255e+208

    1. Initial program 27.5

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

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

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

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

    if 1.1047830349195255e+208 < (* y y)

    1. Initial program 55.4

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified55.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 add-sqr-sqrt55.4

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

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

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

      \[\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 18.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \le 4.940656458412465441765687928682213723651 \cdot 10^{-324}:\\ \;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 2.796727373188044154649432399903811497242 \cdot 10^{-15}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;y \cdot y \le 3837990851.153699398040771484375:\\ \;\;\;\;\left|-\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;y \cdot y \le 1.10478303491952550487979066013675689444 \cdot 10^{208}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{y}{\sqrt{3}}\right|\\ \end{array}\]

Reproduce

herbie shell --seed 2019235 +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.3964793941097758e136) (/ (- z) (sqrt 3)) (if (< z 7.3202936944041821e117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3)) (* (sqrt 0.333333333333333315) z)))

  (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3)))