Average Error: 37.7 → 27.7
Time: 13.2s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -6.836071970171612531075510121178969720421 \cdot 10^{133}:\\ \;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le -5.765504231948259575482186669952739921884 \cdot 10^{-15}:\\ \;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le -4.179086225732637440942875789283836422505 \cdot 10^{-109}:\\ \;\;\;\;\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 1.886712735002693044800968649975463911769 \cdot 10^{-161}:\\ \;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 1.821326997004850929425227104683425764994 \cdot 10^{-27}:\\ \;\;\;\;\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 117.2954970072540703540653339587152004242:\\ \;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 1.819221690489510806875803122853784659239 \cdot 10^{132}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -6.836071970171612531075510121178969720421 \cdot 10^{133}:\\
\;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\

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

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

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

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

\mathbf{elif}\;z \le 117.2954970072540703540653339587152004242:\\
\;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\

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

\mathbf{else}:\\
\;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\

\end{array}
double f(double x, double y, double z) {
        double r559611 = x;
        double r559612 = r559611 * r559611;
        double r559613 = y;
        double r559614 = r559613 * r559613;
        double r559615 = r559612 + r559614;
        double r559616 = z;
        double r559617 = r559616 * r559616;
        double r559618 = r559615 + r559617;
        double r559619 = 3.0;
        double r559620 = r559618 / r559619;
        double r559621 = sqrt(r559620);
        return r559621;
}

double f(double x, double y, double z) {
        double r559622 = z;
        double r559623 = -6.836071970171613e+133;
        bool r559624 = r559622 <= r559623;
        double r559625 = 3.0;
        double r559626 = sqrt(r559625);
        double r559627 = r559622 / r559626;
        double r559628 = fabs(r559627);
        double r559629 = -5.7655042319482596e-15;
        bool r559630 = r559622 <= r559629;
        double r559631 = -1.0;
        double r559632 = x;
        double r559633 = r559631 * r559632;
        double r559634 = r559633 / r559626;
        double r559635 = fabs(r559634);
        double r559636 = -4.1790862257326374e-109;
        bool r559637 = r559622 <= r559636;
        double r559638 = r559632 * r559632;
        double r559639 = y;
        double r559640 = r559639 * r559639;
        double r559641 = r559638 + r559640;
        double r559642 = r559622 * r559622;
        double r559643 = r559641 + r559642;
        double r559644 = sqrt(r559643);
        double r559645 = r559644 / r559626;
        double r559646 = fabs(r559645);
        double r559647 = 1.886712735002693e-161;
        bool r559648 = r559622 <= r559647;
        double r559649 = 1.821326997004851e-27;
        bool r559650 = r559622 <= r559649;
        double r559651 = 1.0;
        double r559652 = r559651 / r559625;
        double r559653 = sqrt(r559652);
        double r559654 = r559644 * r559653;
        double r559655 = 117.29549700725407;
        bool r559656 = r559622 <= r559655;
        double r559657 = 1.8192216904895108e+132;
        bool r559658 = r559622 <= r559657;
        double r559659 = r559643 / r559625;
        double r559660 = sqrt(r559659);
        double r559661 = r559658 ? r559660 : r559628;
        double r559662 = r559656 ? r559635 : r559661;
        double r559663 = r559650 ? r559654 : r559662;
        double r559664 = r559648 ? r559635 : r559663;
        double r559665 = r559637 ? r559646 : r559664;
        double r559666 = r559630 ? r559635 : r559665;
        double r559667 = r559624 ? r559628 : r559666;
        return r559667;
}

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

Original37.7
Target26.0
Herbie27.7
\[\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 < -6.836071970171613e+133 or 1.8192216904895108e+132 < z

    1. Initial program 60.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt60.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-sqrt60.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-frac60.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-square60.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 16.6

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

    if -6.836071970171613e+133 < z < -5.7655042319482596e-15 or -4.1790862257326374e-109 < z < 1.886712735002693e-161 or 1.821326997004851e-27 < z < 117.29549700725407

    1. Initial program 30.3

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

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

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

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

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

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

    if -5.7655042319482596e-15 < z < -4.1790862257326374e-109

    1. Initial program 27.1

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

      \[\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-sqrt27.3

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

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

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

    if 1.886712735002693e-161 < z < 1.821326997004851e-27

    1. Initial program 29.8

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

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

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

    if 117.29549700725407 < z < 1.8192216904895108e+132

    1. Initial program 27.0

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification27.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -6.836071970171612531075510121178969720421 \cdot 10^{133}:\\ \;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le -5.765504231948259575482186669952739921884 \cdot 10^{-15}:\\ \;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le -4.179086225732637440942875789283836422505 \cdot 10^{-109}:\\ \;\;\;\;\left|\frac{\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 1.886712735002693044800968649975463911769 \cdot 10^{-161}:\\ \;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 1.821326997004850929425227104683425764994 \cdot 10^{-27}:\\ \;\;\;\;\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \cdot \sqrt{\frac{1}{3}}\\ \mathbf{elif}\;z \le 117.2954970072540703540653339587152004242:\\ \;\;\;\;\left|\frac{-1 \cdot x}{\sqrt{3}}\right|\\ \mathbf{elif}\;z \le 1.819221690489510806875803122853784659239 \cdot 10^{132}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;\left|\frac{z}{\sqrt{3}}\right|\\ \end{array}\]

Reproduce

herbie shell --seed 2019291 
(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.3964793941097758e136) (/ (- z) (sqrt 3)) (if (< z 7.3202936944041821e117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3)) (* (sqrt 0.333333333333333315) z)))

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