Average Error: 37.8 → 25.7
Time: 4.3s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -4.24117253240271209 \cdot 10^{119}:\\ \;\;\;\;-1 \cdot \frac{x}{\sqrt{3}}\\ \mathbf{elif}\;x \le 6.6019422836665007 \cdot 10^{109}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}\\ \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 -4.24117253240271209 \cdot 10^{119}:\\
\;\;\;\;-1 \cdot \frac{x}{\sqrt{3}}\\

\mathbf{elif}\;x \le 6.6019422836665007 \cdot 10^{109}:\\
\;\;\;\;\sqrt{0.333333333333333315 \cdot \mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}\\

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

\end{array}
double f(double x, double y, double z) {
        double r946622 = x;
        double r946623 = r946622 * r946622;
        double r946624 = y;
        double r946625 = r946624 * r946624;
        double r946626 = r946623 + r946625;
        double r946627 = z;
        double r946628 = r946627 * r946627;
        double r946629 = r946626 + r946628;
        double r946630 = 3.0;
        double r946631 = r946629 / r946630;
        double r946632 = sqrt(r946631);
        return r946632;
}

double f(double x, double y, double z) {
        double r946633 = x;
        double r946634 = -4.241172532402712e+119;
        bool r946635 = r946633 <= r946634;
        double r946636 = -1.0;
        double r946637 = 3.0;
        double r946638 = sqrt(r946637);
        double r946639 = r946633 / r946638;
        double r946640 = r946636 * r946639;
        double r946641 = 6.601942283666501e+109;
        bool r946642 = r946633 <= r946641;
        double r946643 = 0.3333333333333333;
        double r946644 = y;
        double r946645 = z;
        double r946646 = r946645 * r946645;
        double r946647 = fma(r946644, r946644, r946646);
        double r946648 = fma(r946633, r946633, r946647);
        double r946649 = r946643 * r946648;
        double r946650 = sqrt(r946649);
        double r946651 = sqrt(r946643);
        double r946652 = r946633 * r946651;
        double r946653 = r946642 ? r946650 : r946652;
        double r946654 = r946635 ? r946640 : r946653;
        return r946654;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.5
Herbie25.7
\[\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 < -4.241172532402712e+119

    1. Initial program 56.8

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

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied add-sqr-sqrt56.8

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    5. Applied times-frac56.8

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}} \cdot \frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}}\]
    6. Taylor expanded around -inf 18.1

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{\sqrt{3}}}\]

    if -4.241172532402712e+119 < x < 6.601942283666501e+109

    1. Initial program 29.3

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

      \[\leadsto \sqrt{\color{blue}{0.333333333333333315 \cdot {x}^{2} + \left(0.333333333333333315 \cdot {y}^{2} + 0.333333333333333315 \cdot {z}^{2}\right)}}\]
    3. Simplified29.3

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

    if 6.601942283666501e+109 < x

    1. Initial program 55.9

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

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

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

Reproduce

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