Average Error: 37.4 → 25.2
Time: 22.0s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -3.017165361738837830382572779149406364537 \cdot 10^{89}:\\ \;\;\;\;\left(-z\right) \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 2.188536514136267218025323125256231373834 \cdot 10^{121}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, {y}^{2}\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -3.017165361738837830382572779149406364537 \cdot 10^{89}:\\
\;\;\;\;\left(-z\right) \cdot \sqrt{\frac{1}{3}}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r635004 = x;
        double r635005 = r635004 * r635004;
        double r635006 = y;
        double r635007 = r635006 * r635006;
        double r635008 = r635005 + r635007;
        double r635009 = z;
        double r635010 = r635009 * r635009;
        double r635011 = r635008 + r635010;
        double r635012 = 3.0;
        double r635013 = r635011 / r635012;
        double r635014 = sqrt(r635013);
        return r635014;
}

double f(double x, double y, double z) {
        double r635015 = z;
        double r635016 = -3.017165361738838e+89;
        bool r635017 = r635015 <= r635016;
        double r635018 = -r635015;
        double r635019 = 1.0;
        double r635020 = 3.0;
        double r635021 = r635019 / r635020;
        double r635022 = sqrt(r635021);
        double r635023 = r635018 * r635022;
        double r635024 = 2.188536514136267e+121;
        bool r635025 = r635015 <= r635024;
        double r635026 = x;
        double r635027 = y;
        double r635028 = 2.0;
        double r635029 = pow(r635027, r635028);
        double r635030 = fma(r635026, r635026, r635029);
        double r635031 = fma(r635015, r635015, r635030);
        double r635032 = sqrt(r635031);
        double r635033 = r635032 * r635022;
        double r635034 = sqrt(r635020);
        double r635035 = r635015 / r635034;
        double r635036 = r635025 ? r635033 : r635035;
        double r635037 = r635017 ? r635023 : r635036;
        return r635037;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.4
Target24.9
Herbie25.2
\[\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 z < -3.017165361738838e+89

    1. Initial program 53.7

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied div-inv53.7

      \[\leadsto \sqrt{\color{blue}{\left(\left(x \cdot x + y \cdot y\right) + z \cdot z\right) \cdot \frac{1}{3}}}\]
    4. Applied sqrt-prod53.8

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

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

      \[\leadsto \color{blue}{\left(-1 \cdot z\right)} \cdot \sqrt{\frac{1}{3}}\]
    7. Simplified20.3

      \[\leadsto \color{blue}{\left(-z\right)} \cdot \sqrt{\frac{1}{3}}\]

    if -3.017165361738838e+89 < z < 2.188536514136267e+121

    1. Initial program 28.5

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied div-inv28.5

      \[\leadsto \sqrt{\color{blue}{\left(\left(x \cdot x + y \cdot y\right) + z \cdot z\right) \cdot \frac{1}{3}}}\]
    4. Applied sqrt-prod28.6

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

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

    if 2.188536514136267e+121 < z

    1. Initial program 56.9

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied sqrt-div56.9

      \[\leadsto \color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}\]
    4. Simplified56.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -3.017165361738837830382572779149406364537 \cdot 10^{89}:\\ \;\;\;\;\left(-z\right) \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 2.188536514136267218025323125256231373834 \cdot 10^{121}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, {y}^{2}\right)\right)} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]

Reproduce

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