Average Error: 34.8 → 24.7
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 -6.54063272250118 \cdot 10^{+150}:\\ \;\;\;\;-\frac{y}{\sqrt{3.0}}\\ \mathbf{elif}\;y \le 4.1448401758462134 \cdot 10^{+187}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\sqrt[3]{3.0}} \cdot \frac{1}{\sqrt[3]{3.0} \cdot \sqrt[3]{3.0}}}\\ \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 -6.54063272250118 \cdot 10^{+150}:\\
\;\;\;\;-\frac{y}{\sqrt{3.0}}\\

\mathbf{elif}\;y \le 4.1448401758462134 \cdot 10^{+187}:\\
\;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\sqrt[3]{3.0}} \cdot \frac{1}{\sqrt[3]{3.0} \cdot \sqrt[3]{3.0}}}\\

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

\end{array}
double f(double x, double y, double z) {
        double r36554003 = x;
        double r36554004 = r36554003 * r36554003;
        double r36554005 = y;
        double r36554006 = r36554005 * r36554005;
        double r36554007 = r36554004 + r36554006;
        double r36554008 = z;
        double r36554009 = r36554008 * r36554008;
        double r36554010 = r36554007 + r36554009;
        double r36554011 = 3.0;
        double r36554012 = r36554010 / r36554011;
        double r36554013 = sqrt(r36554012);
        return r36554013;
}

double f(double x, double y, double z) {
        double r36554014 = y;
        double r36554015 = -6.54063272250118e+150;
        bool r36554016 = r36554014 <= r36554015;
        double r36554017 = 3.0;
        double r36554018 = sqrt(r36554017);
        double r36554019 = r36554014 / r36554018;
        double r36554020 = -r36554019;
        double r36554021 = 4.1448401758462134e+187;
        bool r36554022 = r36554014 <= r36554021;
        double r36554023 = x;
        double r36554024 = z;
        double r36554025 = r36554024 * r36554024;
        double r36554026 = fma(r36554023, r36554023, r36554025);
        double r36554027 = fma(r36554014, r36554014, r36554026);
        double r36554028 = cbrt(r36554017);
        double r36554029 = r36554027 / r36554028;
        double r36554030 = 1.0;
        double r36554031 = r36554028 * r36554028;
        double r36554032 = r36554030 / r36554031;
        double r36554033 = r36554029 * r36554032;
        double r36554034 = sqrt(r36554033);
        double r36554035 = r36554022 ? r36554034 : r36554019;
        double r36554036 = r36554016 ? r36554020 : r36554035;
        return r36554036;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original34.8
Target23.7
Herbie24.7
\[\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 < -6.54063272250118e+150

    1. Initial program 58.4

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

      \[\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-div58.4

      \[\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.2

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

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

    if -6.54063272250118e+150 < y < 4.1448401758462134e+187

    1. Initial program 28.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3.0}}\]
    2. Simplified28.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 add-cube-cbrt28.0

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\color{blue}{\left(\sqrt[3]{3.0} \cdot \sqrt[3]{3.0}\right) \cdot \sqrt[3]{3.0}}}}\]
    5. Applied *-un-lft-identity28.0

      \[\leadsto \sqrt{\frac{\color{blue}{1 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}{\left(\sqrt[3]{3.0} \cdot \sqrt[3]{3.0}\right) \cdot \sqrt[3]{3.0}}}\]
    6. Applied times-frac28.0

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

    if 4.1448401758462134e+187 < y

    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 11.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -6.54063272250118 \cdot 10^{+150}:\\ \;\;\;\;-\frac{y}{\sqrt{3.0}}\\ \mathbf{elif}\;y \le 4.1448401758462134 \cdot 10^{+187}:\\ \;\;\;\;\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{\sqrt[3]{3.0}} \cdot \frac{1}{\sqrt[3]{3.0} \cdot \sqrt[3]{3.0}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3.0}}\\ \end{array}\]

Reproduce

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