Average Error: 37.5 → 25.1
Time: 12.7s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -3.655717002048407242527182258465352811621 \cdot 10^{123}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-x\right)\\ \mathbf{elif}\;x \le 1.32214930301390480212609639206850133243 \cdot 10^{51}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot x\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;x \le -3.655717002048407242527182258465352811621 \cdot 10^{123}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-x\right)\\

\mathbf{elif}\;x \le 1.32214930301390480212609639206850133243 \cdot 10^{51}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}\\

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

\end{array}
double f(double x, double y, double z) {
        double r953483 = x;
        double r953484 = r953483 * r953483;
        double r953485 = y;
        double r953486 = r953485 * r953485;
        double r953487 = r953484 + r953486;
        double r953488 = z;
        double r953489 = r953488 * r953488;
        double r953490 = r953487 + r953489;
        double r953491 = 3.0;
        double r953492 = r953490 / r953491;
        double r953493 = sqrt(r953492);
        return r953493;
}

double f(double x, double y, double z) {
        double r953494 = x;
        double r953495 = -3.655717002048407e+123;
        bool r953496 = r953494 <= r953495;
        double r953497 = 0.3333333333333333;
        double r953498 = sqrt(r953497);
        double r953499 = -r953494;
        double r953500 = r953498 * r953499;
        double r953501 = 1.3221493030139048e+51;
        bool r953502 = r953494 <= r953501;
        double r953503 = y;
        double r953504 = z;
        double r953505 = 2.0;
        double r953506 = pow(r953504, r953505);
        double r953507 = fma(r953503, r953503, r953506);
        double r953508 = fma(r953494, r953494, r953507);
        double r953509 = r953497 * r953508;
        double r953510 = sqrt(r953509);
        double r953511 = r953498 * r953494;
        double r953512 = r953502 ? r953510 : r953511;
        double r953513 = r953496 ? r953500 : r953512;
        return r953513;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.5
Target25.7
Herbie25.1
\[\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 x < -3.655717002048407e+123

    1. Initial program 58.2

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

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

      \[\leadsto \sqrt{\color{blue}{0.3333333333333333148296162562473909929395 \cdot {x}^{2} + \left(0.3333333333333333148296162562473909929395 \cdot {y}^{2} + 0.3333333333333333148296162562473909929395 \cdot {z}^{2}\right)}}\]
    4. Simplified58.2

      \[\leadsto \sqrt{\color{blue}{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}}\]
    5. Using strategy rm
    6. Applied sqrt-prod58.2

      \[\leadsto \color{blue}{\sqrt{0.3333333333333333148296162562473909929395} \cdot \sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}}\]
    7. Taylor expanded around -inf 15.8

      \[\leadsto \sqrt{0.3333333333333333148296162562473909929395} \cdot \color{blue}{\left(-1 \cdot x\right)}\]
    8. Simplified15.8

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

    if -3.655717002048407e+123 < x < 1.3221493030139048e+51

    1. Initial program 28.5

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

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

      \[\leadsto \sqrt{\color{blue}{0.3333333333333333148296162562473909929395 \cdot {x}^{2} + \left(0.3333333333333333148296162562473909929395 \cdot {y}^{2} + 0.3333333333333333148296162562473909929395 \cdot {z}^{2}\right)}}\]
    4. Simplified28.5

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

    if 1.3221493030139048e+51 < x

    1. Initial program 50.4

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

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

      \[\leadsto \sqrt{\color{blue}{0.3333333333333333148296162562473909929395 \cdot {x}^{2} + \left(0.3333333333333333148296162562473909929395 \cdot {y}^{2} + 0.3333333333333333148296162562473909929395 \cdot {z}^{2}\right)}}\]
    4. Simplified50.4

      \[\leadsto \sqrt{\color{blue}{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}}\]
    5. Using strategy rm
    6. Applied sqrt-prod50.4

      \[\leadsto \color{blue}{\sqrt{0.3333333333333333148296162562473909929395} \cdot \sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}}\]
    7. Taylor expanded around inf 21.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -3.655717002048407242527182258465352811621 \cdot 10^{123}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-x\right)\\ \mathbf{elif}\;x \le 1.32214930301390480212609639206850133243 \cdot 10^{51}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, {z}^{2}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot x\\ \end{array}\]

Reproduce

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