Average Error: 38.4 → 26.4
Time: 11.1s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.35612836383285588 \cdot 10^{108}:\\ \;\;\;\;\frac{-x}{\sqrt{3}}\\ \mathbf{elif}\;x \le 1.04793530695557172 \cdot 10^{77}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt{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.35612836383285588 \cdot 10^{108}:\\
\;\;\;\;\frac{-x}{\sqrt{3}}\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r916738 = x;
        double r916739 = r916738 * r916738;
        double r916740 = y;
        double r916741 = r916740 * r916740;
        double r916742 = r916739 + r916741;
        double r916743 = z;
        double r916744 = r916743 * r916743;
        double r916745 = r916742 + r916744;
        double r916746 = 3.0;
        double r916747 = r916745 / r916746;
        double r916748 = sqrt(r916747);
        return r916748;
}

double f(double x, double y, double z) {
        double r916749 = x;
        double r916750 = -1.356128363832856e+108;
        bool r916751 = r916749 <= r916750;
        double r916752 = -r916749;
        double r916753 = 3.0;
        double r916754 = sqrt(r916753);
        double r916755 = r916752 / r916754;
        double r916756 = 1.0479353069555717e+77;
        bool r916757 = r916749 <= r916756;
        double r916758 = y;
        double r916759 = z;
        double r916760 = r916759 * r916759;
        double r916761 = fma(r916758, r916758, r916760);
        double r916762 = fma(r916749, r916749, r916761);
        double r916763 = sqrt(r916762);
        double r916764 = r916763 / r916754;
        double r916765 = 0.3333333333333333;
        double r916766 = sqrt(r916765);
        double r916767 = r916749 * r916766;
        double r916768 = r916757 ? r916764 : r916767;
        double r916769 = r916751 ? r916755 : r916768;
        return r916769;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.4
Target25.9
Herbie26.4
\[\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.356128363832856e+108

    1. Initial program 55.4

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

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

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

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

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

    if -1.356128363832856e+108 < x < 1.0479353069555717e+77

    1. Initial program 29.9

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

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

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

    if 1.0479353069555717e+77 < x

    1. Initial program 53.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -1.35612836383285588 \cdot 10^{108}:\\ \;\;\;\;\frac{-x}{\sqrt{3}}\\ \mathbf{elif}\;x \le 1.04793530695557172 \cdot 10^{77}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Reproduce

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