Average Error: 37.2 → 25.3
Time: 46.0s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -2.224655211931350154467905423564222198286 \cdot 10^{105}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 1.675593236197967767813497815885587492334 \cdot 10^{62}:\\ \;\;\;\;\sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\sqrt[3]{3}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -2.224655211931350154467905423564222198286 \cdot 10^{105}:\\
\;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r37070257 = x;
        double r37070258 = r37070257 * r37070257;
        double r37070259 = y;
        double r37070260 = r37070259 * r37070259;
        double r37070261 = r37070258 + r37070260;
        double r37070262 = z;
        double r37070263 = r37070262 * r37070262;
        double r37070264 = r37070261 + r37070263;
        double r37070265 = 3.0;
        double r37070266 = r37070264 / r37070265;
        double r37070267 = sqrt(r37070266);
        return r37070267;
}

double f(double x, double y, double z) {
        double r37070268 = y;
        double r37070269 = -2.2246552119313502e+105;
        bool r37070270 = r37070268 <= r37070269;
        double r37070271 = -r37070268;
        double r37070272 = 0.3333333333333333;
        double r37070273 = sqrt(r37070272);
        double r37070274 = r37070271 * r37070273;
        double r37070275 = 1.6755932361979678e+62;
        bool r37070276 = r37070268 <= r37070275;
        double r37070277 = 1.0;
        double r37070278 = 3.0;
        double r37070279 = cbrt(r37070278);
        double r37070280 = r37070279 * r37070279;
        double r37070281 = r37070277 / r37070280;
        double r37070282 = x;
        double r37070283 = z;
        double r37070284 = r37070283 * r37070283;
        double r37070285 = fma(r37070282, r37070282, r37070284);
        double r37070286 = fma(r37070268, r37070268, r37070285);
        double r37070287 = r37070286 / r37070279;
        double r37070288 = r37070281 * r37070287;
        double r37070289 = sqrt(r37070288);
        double r37070290 = r37070273 * r37070268;
        double r37070291 = r37070276 ? r37070289 : r37070290;
        double r37070292 = r37070270 ? r37070274 : r37070291;
        return r37070292;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.2
Target25.0
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 < -2.2246552119313502e+105

    1. Initial program 54.8

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified54.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 add-cube-cbrt54.8

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    5. Applied *-un-lft-identity54.8

      \[\leadsto \sqrt{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}\]
    6. Applied times-frac54.8

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    8. Simplified16.6

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

    if -2.2246552119313502e+105 < y < 1.6755932361979678e+62

    1. Initial program 28.7

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

      \[\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 add-cube-cbrt28.7

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    5. Applied *-un-lft-identity28.7

      \[\leadsto \sqrt{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}\]
    6. Applied times-frac28.8

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

    if 1.6755932361979678e+62 < y

    1. Initial program 50.0

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

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

      \[\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 -2.224655211931350154467905423564222198286 \cdot 10^{105}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 1.675593236197967767813497815885587492334 \cdot 10^{62}:\\ \;\;\;\;\sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\sqrt[3]{3}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]

Reproduce

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