Average Error: 38.2 → 25.5
Time: 13.3s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -2.942680527553483193495594945411872245454 \cdot 10^{151}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-y\right)\\ \mathbf{elif}\;y \le 6.557174003264763770996525885584065399587 \cdot 10^{116}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -2.942680527553483193495594945411872245454 \cdot 10^{151}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-y\right)\\

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

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

\end{array}
double f(double x, double y, double z) {
        double r604804 = x;
        double r604805 = r604804 * r604804;
        double r604806 = y;
        double r604807 = r604806 * r604806;
        double r604808 = r604805 + r604807;
        double r604809 = z;
        double r604810 = r604809 * r604809;
        double r604811 = r604808 + r604810;
        double r604812 = 3.0;
        double r604813 = r604811 / r604812;
        double r604814 = sqrt(r604813);
        return r604814;
}

double f(double x, double y, double z) {
        double r604815 = y;
        double r604816 = -2.942680527553483e+151;
        bool r604817 = r604815 <= r604816;
        double r604818 = 0.3333333333333333;
        double r604819 = sqrt(r604818);
        double r604820 = -r604815;
        double r604821 = r604819 * r604820;
        double r604822 = 6.557174003264764e+116;
        bool r604823 = r604815 <= r604822;
        double r604824 = x;
        double r604825 = z;
        double r604826 = r604825 * r604825;
        double r604827 = fma(r604824, r604824, r604826);
        double r604828 = fma(r604815, r604815, r604827);
        double r604829 = r604818 * r604828;
        double r604830 = sqrt(r604829);
        double r604831 = r604819 * r604815;
        double r604832 = r604823 ? r604830 : r604831;
        double r604833 = r604817 ? r604821 : r604832;
        return r604833;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.2
Target25.9
Herbie25.5
\[\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 y < -2.942680527553483e+151

    1. Initial program 63.1

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    6. Simplified13.6

      \[\leadsto \color{blue}{-y \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]

    if -2.942680527553483e+151 < y < 6.557174003264764e+116

    1. Initial program 29.7

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

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

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

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

    if 6.557174003264764e+116 < y

    1. Initial program 56.2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.942680527553483193495594945411872245454 \cdot 10^{151}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot \left(-y\right)\\ \mathbf{elif}\;y \le 6.557174003264763770996525885584065399587 \cdot 10^{116}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]

Reproduce

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