Average Error: 38.1 → 26.0
Time: 15.7s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -8.482917924797483679712647688226368282521 \cdot 10^{119}:\\ \;\;\;\;-\sqrt{\frac{1}{3}} \cdot y\\ \mathbf{elif}\;y \le 3.902731621945318548033121962043139940674 \cdot 10^{93}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -8.482917924797483679712647688226368282521 \cdot 10^{119}:\\
\;\;\;\;-\sqrt{\frac{1}{3}} \cdot y\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r1238152 = x;
        double r1238153 = r1238152 * r1238152;
        double r1238154 = y;
        double r1238155 = r1238154 * r1238154;
        double r1238156 = r1238153 + r1238155;
        double r1238157 = z;
        double r1238158 = r1238157 * r1238157;
        double r1238159 = r1238156 + r1238158;
        double r1238160 = 3.0;
        double r1238161 = r1238159 / r1238160;
        double r1238162 = sqrt(r1238161);
        return r1238162;
}

double f(double x, double y, double z) {
        double r1238163 = y;
        double r1238164 = -8.482917924797484e+119;
        bool r1238165 = r1238163 <= r1238164;
        double r1238166 = 1.0;
        double r1238167 = 3.0;
        double r1238168 = r1238166 / r1238167;
        double r1238169 = sqrt(r1238168);
        double r1238170 = r1238169 * r1238163;
        double r1238171 = -r1238170;
        double r1238172 = 3.9027316219453185e+93;
        bool r1238173 = r1238163 <= r1238172;
        double r1238174 = x;
        double r1238175 = z;
        double r1238176 = r1238175 * r1238175;
        double r1238177 = fma(r1238174, r1238174, r1238176);
        double r1238178 = fma(r1238163, r1238163, r1238177);
        double r1238179 = r1238178 / r1238167;
        double r1238180 = sqrt(r1238179);
        double r1238181 = sqrt(r1238167);
        double r1238182 = r1238163 / r1238181;
        double r1238183 = r1238173 ? r1238180 : r1238182;
        double r1238184 = r1238165 ? r1238171 : r1238183;
        return r1238184;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.1
Target25.4
Herbie26.0
\[\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 < -8.482917924797484e+119

    1. Initial program 58.3

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

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

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

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

      \[\leadsto \color{blue}{\left(-1 \cdot y\right)} \cdot \sqrt{\frac{1}{3}}\]
    8. Simplified18.5

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

    if -8.482917924797484e+119 < y < 3.9027316219453185e+93

    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(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}}\]

    if 3.9027316219453185e+93 < y

    1. Initial program 53.8

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

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

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

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

      \[\leadsto \frac{\color{blue}{y}}{\sqrt{3}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.0

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

Reproduce

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