Average Error: 38.6 → 26.1
Time: 4.1s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -6.92240592229415833 \cdot 10^{130}:\\ \;\;\;\;-1 \cdot \frac{x}{\sqrt{3}}\\ \mathbf{elif}\;x \le 7.6477979095530901 \cdot 10^{93}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;x \le -6.92240592229415833 \cdot 10^{130}:\\
\;\;\;\;-1 \cdot \frac{x}{\sqrt{3}}\\

\mathbf{elif}\;x \le 7.6477979095530901 \cdot 10^{93}:\\
\;\;\;\;\sqrt{0.333333333333333315 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;x \cdot \sqrt{0.333333333333333315}\\

\end{array}
double f(double x, double y, double z) {
        double r873185 = x;
        double r873186 = r873185 * r873185;
        double r873187 = y;
        double r873188 = r873187 * r873187;
        double r873189 = r873186 + r873188;
        double r873190 = z;
        double r873191 = r873190 * r873190;
        double r873192 = r873189 + r873191;
        double r873193 = 3.0;
        double r873194 = r873192 / r873193;
        double r873195 = sqrt(r873194);
        return r873195;
}

double f(double x, double y, double z) {
        double r873196 = x;
        double r873197 = -6.922405922294158e+130;
        bool r873198 = r873196 <= r873197;
        double r873199 = -1.0;
        double r873200 = 3.0;
        double r873201 = sqrt(r873200);
        double r873202 = r873196 / r873201;
        double r873203 = r873199 * r873202;
        double r873204 = 7.64779790955309e+93;
        bool r873205 = r873196 <= r873204;
        double r873206 = 0.3333333333333333;
        double r873207 = y;
        double r873208 = z;
        double r873209 = r873208 * r873208;
        double r873210 = fma(r873207, r873207, r873209);
        double r873211 = fma(r873196, r873196, r873210);
        double r873212 = r873206 * r873211;
        double r873213 = sqrt(r873212);
        double r873214 = sqrt(r873206);
        double r873215 = r873196 * r873214;
        double r873216 = r873205 ? r873213 : r873215;
        double r873217 = r873198 ? r873203 : r873216;
        return r873217;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.6
Target26.3
Herbie26.1
\[\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 x < -6.922405922294158e+130

    1. Initial program 59.4

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

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}} \cdot \frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}}\]
    6. Taylor expanded around -inf 17.0

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{\sqrt{3}}}\]

    if -6.922405922294158e+130 < x < 7.64779790955309e+93

    1. Initial program 29.7

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

      \[\leadsto \sqrt{\color{blue}{0.333333333333333315 \cdot {x}^{2} + \left(0.333333333333333315 \cdot {y}^{2} + 0.333333333333333315 \cdot {z}^{2}\right)}}\]
    3. Simplified29.8

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

    if 7.64779790955309e+93 < x

    1. Initial program 55.4

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Taylor expanded around inf 19.6

      \[\leadsto \color{blue}{x \cdot \sqrt{0.333333333333333315}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -6.92240592229415833 \cdot 10^{130}:\\ \;\;\;\;-1 \cdot \frac{x}{\sqrt{3}}\\ \mathbf{elif}\;x \le 7.6477979095530901 \cdot 10^{93}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Reproduce

herbie shell --seed 2020020 +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.396479394109776e+136) (/ (- z) (sqrt 3)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3)) (* (sqrt 0.3333333333333333) z)))

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