Average Error: 39.0 → 26.5
Time: 5.1s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -5.86376436625527096038425827064555139232 \cdot 10^{94}:\\ \;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 5.479034299166120282419679868368698216953 \cdot 10^{-62}:\\ \;\;\;\;\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 1.43555553142026435386156201253954068939 \cdot 10^{-23}:\\ \;\;\;\;\left|-1 \cdot \frac{x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 3.353544466640978225465484641144922282532 \cdot 10^{127}:\\ \;\;\;\;\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -5.86376436625527096038425827064555139232 \cdot 10^{94}:\\
\;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\

\mathbf{elif}\;z \le 5.479034299166120282419679868368698216953 \cdot 10^{-62}:\\
\;\;\;\;\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\frac{1}{3}}\\

\mathbf{elif}\;z \le 1.43555553142026435386156201253954068939 \cdot 10^{-23}:\\
\;\;\;\;\left|-1 \cdot \frac{x}{\sqrt{3}}\right|\\

\mathbf{elif}\;z \le 3.353544466640978225465484641144922282532 \cdot 10^{127}:\\
\;\;\;\;\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|\\

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\

\end{array}
double f(double x, double y, double z) {
        double r889662 = x;
        double r889663 = r889662 * r889662;
        double r889664 = y;
        double r889665 = r889664 * r889664;
        double r889666 = r889663 + r889665;
        double r889667 = z;
        double r889668 = r889667 * r889667;
        double r889669 = r889666 + r889668;
        double r889670 = 3.0;
        double r889671 = r889669 / r889670;
        double r889672 = sqrt(r889671);
        return r889672;
}

double f(double x, double y, double z) {
        double r889673 = z;
        double r889674 = -5.863764366255271e+94;
        bool r889675 = r889673 <= r889674;
        double r889676 = 3.0;
        double r889677 = sqrt(r889676);
        double r889678 = r889673 / r889677;
        double r889679 = fabs(r889678);
        double r889680 = 5.47903429916612e-62;
        bool r889681 = r889673 <= r889680;
        double r889682 = x;
        double r889683 = r889682 * r889682;
        double r889684 = y;
        double r889685 = r889684 * r889684;
        double r889686 = r889683 + r889685;
        double r889687 = r889673 * r889673;
        double r889688 = r889686 + r889687;
        double r889689 = sqrt(r889688);
        double r889690 = 1.0;
        double r889691 = r889690 / r889676;
        double r889692 = sqrt(r889691);
        double r889693 = r889689 * r889692;
        double r889694 = 1.4355555314202644e-23;
        bool r889695 = r889673 <= r889694;
        double r889696 = -1.0;
        double r889697 = r889682 / r889677;
        double r889698 = r889696 * r889697;
        double r889699 = fabs(r889698);
        double r889700 = 3.353544466640978e+127;
        bool r889701 = r889673 <= r889700;
        double r889702 = r889689 / r889677;
        double r889703 = fabs(r889702);
        double r889704 = r889673 * r889692;
        double r889705 = r889701 ? r889703 : r889704;
        double r889706 = r889695 ? r889699 : r889705;
        double r889707 = r889681 ? r889693 : r889706;
        double r889708 = r889675 ? r889679 : r889707;
        return r889708;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original39.0
Target26.3
Herbie26.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 5 regimes
  2. if z < -5.863764366255271e+94

    1. Initial program 55.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt55.0

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied add-sqr-sqrt55.0

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    5. Applied times-frac55.0

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}} \cdot \frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}}\]
    6. Applied rem-sqrt-square55.0

      \[\leadsto \color{blue}{\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|}\]
    7. Taylor expanded around 0 19.7

      \[\leadsto \left|\frac{\color{blue}{z}}{\sqrt{3}}\right|\]

    if -5.863764366255271e+94 < z < 5.47903429916612e-62

    1. Initial program 30.7

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied div-inv30.7

      \[\leadsto \sqrt{\color{blue}{\left(\left(x \cdot x + y \cdot y\right) + z \cdot z\right) \cdot \frac{1}{3}}}\]
    4. Applied sqrt-prod30.8

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

    if 5.47903429916612e-62 < z < 1.4355555314202644e-23

    1. Initial program 30.8

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt30.9

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied add-sqr-sqrt30.9

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    5. Applied times-frac30.9

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}} \cdot \frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}}\]
    6. Applied rem-sqrt-square30.9

      \[\leadsto \color{blue}{\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|}\]
    7. Using strategy rm
    8. Applied add-exp-log30.9

      \[\leadsto \left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\color{blue}{e^{\log \left(\sqrt{3}\right)}}}\right|\]
    9. Applied add-exp-log33.1

      \[\leadsto \left|\frac{\color{blue}{e^{\log \left(\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\right)}}}{e^{\log \left(\sqrt{3}\right)}}\right|\]
    10. Applied div-exp33.2

      \[\leadsto \left|\color{blue}{e^{\log \left(\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}\right) - \log \left(\sqrt{3}\right)}}\right|\]
    11. Simplified33.1

      \[\leadsto \left|e^{\color{blue}{\log \left(\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right)}}\right|\]
    12. Taylor expanded around -inf 34.2

      \[\leadsto \left|\color{blue}{-1 \cdot \frac{x}{\sqrt{3}}}\right|\]

    if 1.4355555314202644e-23 < z < 3.353544466640978e+127

    1. Initial program 28.9

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt29.1

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied add-sqr-sqrt29.1

      \[\leadsto \sqrt{\frac{\color{blue}{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}}{\sqrt{3} \cdot \sqrt{3}}}\]
    5. Applied times-frac29.0

      \[\leadsto \sqrt{\color{blue}{\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}} \cdot \frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}}}\]
    6. Applied rem-sqrt-square29.0

      \[\leadsto \color{blue}{\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|}\]

    if 3.353544466640978e+127 < z

    1. Initial program 58.6

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied div-inv58.6

      \[\leadsto \sqrt{\color{blue}{\left(\left(x \cdot x + y \cdot y\right) + z \cdot z\right) \cdot \frac{1}{3}}}\]
    4. Applied sqrt-prod58.6

      \[\leadsto \color{blue}{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\frac{1}{3}}}\]
    5. Taylor expanded around 0 15.9

      \[\leadsto \color{blue}{z} \cdot \sqrt{\frac{1}{3}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification26.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -5.86376436625527096038425827064555139232 \cdot 10^{94}:\\ \;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 5.479034299166120282419679868368698216953 \cdot 10^{-62}:\\ \;\;\;\;\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 1.43555553142026435386156201253954068939 \cdot 10^{-23}:\\ \;\;\;\;\left|-1 \cdot \frac{x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 3.353544466640978225465484641144922282532 \cdot 10^{127}:\\ \;\;\;\;\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{\frac{1}{3}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020002 
(FPCore (x y z)
  :name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"
  :precision binary64

  :herbie-target
  (if (< z -6.396479394109776e+136) (/ (- z) (sqrt 3)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3)) (* (sqrt 0.3333333333333333) z)))

  (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3)))