Average Error: 35.4 → 24.1
Time: 16.2s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -3.397008026592871 \cdot 10^{+153}:\\ \;\;\;\;\frac{-y}{\sqrt{3.0}}\\ \mathbf{elif}\;y \le 6.86482775038648 \cdot 10^{+127}:\\ \;\;\;\;\sqrt{0.3333333333333333 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3.0}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}
\begin{array}{l}
\mathbf{if}\;y \le -3.397008026592871 \cdot 10^{+153}:\\
\;\;\;\;\frac{-y}{\sqrt{3.0}}\\

\mathbf{elif}\;y \le 6.86482775038648 \cdot 10^{+127}:\\
\;\;\;\;\sqrt{0.3333333333333333 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{y}{\sqrt{3.0}}\\

\end{array}
double f(double x, double y, double z) {
        double r38356792 = x;
        double r38356793 = r38356792 * r38356792;
        double r38356794 = y;
        double r38356795 = r38356794 * r38356794;
        double r38356796 = r38356793 + r38356795;
        double r38356797 = z;
        double r38356798 = r38356797 * r38356797;
        double r38356799 = r38356796 + r38356798;
        double r38356800 = 3.0;
        double r38356801 = r38356799 / r38356800;
        double r38356802 = sqrt(r38356801);
        return r38356802;
}

double f(double x, double y, double z) {
        double r38356803 = y;
        double r38356804 = -3.397008026592871e+153;
        bool r38356805 = r38356803 <= r38356804;
        double r38356806 = -r38356803;
        double r38356807 = 3.0;
        double r38356808 = sqrt(r38356807);
        double r38356809 = r38356806 / r38356808;
        double r38356810 = 6.86482775038648e+127;
        bool r38356811 = r38356803 <= r38356810;
        double r38356812 = 0.3333333333333333;
        double r38356813 = z;
        double r38356814 = x;
        double r38356815 = r38356814 * r38356814;
        double r38356816 = fma(r38356813, r38356813, r38356815);
        double r38356817 = fma(r38356803, r38356803, r38356816);
        double r38356818 = r38356812 * r38356817;
        double r38356819 = sqrt(r38356818);
        double r38356820 = r38356803 / r38356808;
        double r38356821 = r38356811 ? r38356819 : r38356820;
        double r38356822 = r38356805 ? r38356809 : r38356821;
        return r38356822;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original35.4
Target24.5
Herbie24.1
\[\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 < -3.397008026592871e+153

    1. Initial program 59.0

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

      \[\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 sqrt-div59.0

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

      \[\leadsto \frac{\color{blue}{-1 \cdot y}}{\sqrt{3.0}}\]
    6. Simplified14.7

      \[\leadsto \frac{\color{blue}{-y}}{\sqrt{3.0}}\]

    if -3.397008026592871e+153 < y < 6.86482775038648e+127

    1. Initial program 27.2

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

      \[\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 0 27.2

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

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

    if 6.86482775038648e+127 < y

    1. Initial program 54.6

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

      \[\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 sqrt-div54.6

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

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

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

Reproduce

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