Average Error: 35.7 → 24.3
Time: 20.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 r43130995 = x;
        double r43130996 = r43130995 * r43130995;
        double r43130997 = y;
        double r43130998 = r43130997 * r43130997;
        double r43130999 = r43130996 + r43130998;
        double r43131000 = z;
        double r43131001 = r43131000 * r43131000;
        double r43131002 = r43130999 + r43131001;
        double r43131003 = 3.0;
        double r43131004 = r43131002 / r43131003;
        double r43131005 = sqrt(r43131004);
        return r43131005;
}

double f(double x, double y, double z) {
        double r43131006 = y;
        double r43131007 = -1.3282248930815427e+154;
        bool r43131008 = r43131006 <= r43131007;
        double r43131009 = 0.3333333333333333;
        double r43131010 = sqrt(r43131009);
        double r43131011 = -r43131006;
        double r43131012 = r43131010 * r43131011;
        double r43131013 = 8.243533173233274e+154;
        bool r43131014 = r43131006 <= r43131013;
        double r43131015 = x;
        double r43131016 = z;
        double r43131017 = r43131016 * r43131016;
        double r43131018 = fma(r43131015, r43131015, r43131017);
        double r43131019 = fma(r43131006, r43131006, r43131018);
        double r43131020 = 3.0;
        double r43131021 = r43131019 / r43131020;
        double r43131022 = sqrt(r43131021);
        double r43131023 = r43131010 * r43131006;
        double r43131024 = r43131014 ? r43131022 : r43131023;
        double r43131025 = r43131008 ? r43131012 : r43131024;
        return r43131025;
}

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)))