Average Error: 37.5 → 25.0
Time: 6.3s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.6174687293654252 \cdot 10^{118}:\\ \;\;\;\;\sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}} \cdot \left(-1 \cdot \left(\sqrt{\frac{1}{\sqrt[3]{3}}} \cdot x\right)\right)\\ \mathbf{elif}\;x \le 1.3566371815103393 \cdot 10^{105}:\\ \;\;\;\;\sqrt{\frac{-\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}{-3}}\\ \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 -1.6174687293654252 \cdot 10^{118}:\\
\;\;\;\;\sqrt{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}} \cdot \left(-1 \cdot \left(\sqrt{\frac{1}{\sqrt[3]{3}}} \cdot x\right)\right)\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r836381 = x;
        double r836382 = r836381 * r836381;
        double r836383 = y;
        double r836384 = r836383 * r836383;
        double r836385 = r836382 + r836384;
        double r836386 = z;
        double r836387 = r836386 * r836386;
        double r836388 = r836385 + r836387;
        double r836389 = 3.0;
        double r836390 = r836388 / r836389;
        double r836391 = sqrt(r836390);
        return r836391;
}

double f(double x, double y, double z) {
        double r836392 = x;
        double r836393 = -1.6174687293654252e+118;
        bool r836394 = r836392 <= r836393;
        double r836395 = 1.0;
        double r836396 = 3.0;
        double r836397 = cbrt(r836396);
        double r836398 = r836397 * r836397;
        double r836399 = r836395 / r836398;
        double r836400 = sqrt(r836399);
        double r836401 = -1.0;
        double r836402 = r836395 / r836397;
        double r836403 = sqrt(r836402);
        double r836404 = r836403 * r836392;
        double r836405 = r836401 * r836404;
        double r836406 = r836400 * r836405;
        double r836407 = 1.3566371815103393e+105;
        bool r836408 = r836392 <= r836407;
        double r836409 = y;
        double r836410 = z;
        double r836411 = r836410 * r836410;
        double r836412 = fma(r836409, r836409, r836411);
        double r836413 = fma(r836392, r836392, r836412);
        double r836414 = -r836413;
        double r836415 = -r836396;
        double r836416 = r836414 / r836415;
        double r836417 = sqrt(r836416);
        double r836418 = 0.3333333333333333;
        double r836419 = sqrt(r836418);
        double r836420 = r836392 * r836419;
        double r836421 = r836408 ? r836417 : r836420;
        double r836422 = r836394 ? r836406 : r836421;
        return r836422;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.5
Target25.2
Herbie25.0
\[\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 < -1.6174687293654252e+118

    1. Initial program 57.3

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

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

      \[\leadsto \sqrt{\frac{\color{blue}{-\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{-3}}\]
    5. Using strategy rm
    6. Applied add-cube-cbrt57.3

      \[\leadsto \sqrt{\frac{-\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}{-\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    7. Applied distribute-lft-neg-in57.3

      \[\leadsto \sqrt{\frac{-\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}{\color{blue}{\left(-\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    8. Applied neg-mul-157.3

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

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

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

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

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

    if -1.6174687293654252e+118 < x < 1.3566371815103393e+105

    1. Initial program 28.6

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

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

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

    if 1.3566371815103393e+105 < x

    1. Initial program 55.5

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

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

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

Reproduce

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