Average Error: 34.8 → 24.4
Time: 16.9s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -2.9088365088923405 \cdot 10^{+141}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \le 1.2764865492717349 \cdot 10^{+143}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)} \cdot \sqrt{\frac{1}{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}
\begin{array}{l}
\mathbf{if}\;y \le -2.9088365088923405 \cdot 10^{+141}:\\
\;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333}\\

\mathbf{elif}\;y \le 1.2764865492717349 \cdot 10^{+143}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)} \cdot \sqrt{\frac{1}{3.0}}\\

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

\end{array}
double f(double x, double y, double z) {
        double r38246523 = x;
        double r38246524 = r38246523 * r38246523;
        double r38246525 = y;
        double r38246526 = r38246525 * r38246525;
        double r38246527 = r38246524 + r38246526;
        double r38246528 = z;
        double r38246529 = r38246528 * r38246528;
        double r38246530 = r38246527 + r38246529;
        double r38246531 = 3.0;
        double r38246532 = r38246530 / r38246531;
        double r38246533 = sqrt(r38246532);
        return r38246533;
}

double f(double x, double y, double z) {
        double r38246534 = y;
        double r38246535 = -2.9088365088923405e+141;
        bool r38246536 = r38246534 <= r38246535;
        double r38246537 = -r38246534;
        double r38246538 = 0.3333333333333333;
        double r38246539 = sqrt(r38246538);
        double r38246540 = r38246537 * r38246539;
        double r38246541 = 1.2764865492717349e+143;
        bool r38246542 = r38246534 <= r38246541;
        double r38246543 = x;
        double r38246544 = z;
        double r38246545 = r38246544 * r38246544;
        double r38246546 = fma(r38246543, r38246543, r38246545);
        double r38246547 = fma(r38246534, r38246534, r38246546);
        double r38246548 = sqrt(r38246547);
        double r38246549 = 1.0;
        double r38246550 = 3.0;
        double r38246551 = r38246549 / r38246550;
        double r38246552 = sqrt(r38246551);
        double r38246553 = r38246548 * r38246552;
        double r38246554 = r38246539 * r38246534;
        double r38246555 = r38246542 ? r38246553 : r38246554;
        double r38246556 = r38246536 ? r38246540 : r38246555;
        return r38246556;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original34.8
Target23.7
Herbie24.4
\[\begin{array}{l} \mathbf{if}\;z \lt -6.396479394109776 \cdot 10^{+136}:\\ \;\;\;\;\frac{-z}{\sqrt{3.0}}\\ \mathbf{elif}\;z \lt 7.320293694404182 \cdot 10^{+117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -2.9088365088923405e+141

    1. Initial program 56.7

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333}\right)}\]
    4. Simplified15.1

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

    if -2.9088365088923405e+141 < y < 1.2764865492717349e+143

    1. Initial program 27.3

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3.0}}}\]
    3. Using strategy rm
    4. Applied div-inv27.4

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

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

    if 1.2764865492717349e+143 < y

    1. Initial program 57.1

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
    2. Simplified57.1

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

      \[\leadsto \color{blue}{y \cdot \sqrt{0.3333333333333333}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification24.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.9088365088923405 \cdot 10^{+141}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333}\\ \mathbf{elif}\;y \le 1.2764865492717349 \cdot 10^{+143}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)} \cdot \sqrt{\frac{1}{3.0}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot y\\ \end{array}\]

Reproduce

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