Average Error: 35.7 → 24.3
Time: 19.1s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.3282248930815427 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-y\right)\\ \mathbf{elif}\;y \le 8.243533173233274 \cdot 10^{+154}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{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 -1.3282248930815427 \cdot 10^{+154}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot \left(-y\right)\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r31126812 = x;
        double r31126813 = r31126812 * r31126812;
        double r31126814 = y;
        double r31126815 = r31126814 * r31126814;
        double r31126816 = r31126813 + r31126815;
        double r31126817 = z;
        double r31126818 = r31126817 * r31126817;
        double r31126819 = r31126816 + r31126818;
        double r31126820 = 3.0;
        double r31126821 = r31126819 / r31126820;
        double r31126822 = sqrt(r31126821);
        return r31126822;
}

double f(double x, double y, double z) {
        double r31126823 = y;
        double r31126824 = -1.3282248930815427e+154;
        bool r31126825 = r31126823 <= r31126824;
        double r31126826 = 0.3333333333333333;
        double r31126827 = sqrt(r31126826);
        double r31126828 = -r31126823;
        double r31126829 = r31126827 * r31126828;
        double r31126830 = 8.243533173233274e+154;
        bool r31126831 = r31126823 <= r31126830;
        double r31126832 = x;
        double r31126833 = z;
        double r31126834 = r31126833 * r31126833;
        double r31126835 = fma(r31126832, r31126832, r31126834);
        double r31126836 = fma(r31126823, r31126823, r31126835);
        double r31126837 = 3.0;
        double r31126838 = r31126836 / r31126837;
        double r31126839 = sqrt(r31126838);
        double r31126840 = r31126827 * r31126823;
        double r31126841 = r31126831 ? r31126839 : r31126840;
        double r31126842 = r31126825 ? r31126829 : r31126841;
        return r31126842;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original35.7
Target24.6
Herbie24.3
\[\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 < -1.3282248930815427e+154

    1. Initial program 59.3

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

      \[\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 14.2

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

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

    if -1.3282248930815427e+154 < y < 8.243533173233274e+154

    1. Initial program 27.7

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

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

    if 8.243533173233274e+154 < y

    1. Initial program 59.3

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

      \[\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 14.4

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

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

Reproduce

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