Average Error: 38.1 → 26.2
Time: 18.8s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.668643408655414716179248580097977706017 \cdot 10^{122}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le -2.672713142726911514272253621456748121583 \cdot 10^{-197}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -2.003356476000291454828377115726675914901 \cdot 10^{-248}:\\ \;\;\;\;\left(\sqrt[3]{\frac{1}{\sqrt[3]{3}}} \cdot y\right) \cdot \sqrt{\frac{1}{\sqrt[3]{\sqrt[3]{3}} \cdot {\left(\sqrt[3]{3}\right)}^{2}}}\\ \mathbf{elif}\;z \le 1.313897985272073697158734222693046230491 \cdot 10^{67}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;z \le -2.668643408655414716179248580097977706017 \cdot 10^{122}:\\
\;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

\mathbf{elif}\;z \le -2.672713142726911514272253621456748121583 \cdot 10^{-197}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\

\mathbf{elif}\;z \le -2.003356476000291454828377115726675914901 \cdot 10^{-248}:\\
\;\;\;\;\left(\sqrt[3]{\frac{1}{\sqrt[3]{3}}} \cdot y\right) \cdot \sqrt{\frac{1}{\sqrt[3]{\sqrt[3]{3}} \cdot {\left(\sqrt[3]{3}\right)}^{2}}}\\

\mathbf{elif}\;z \le 1.313897985272073697158734222693046230491 \cdot 10^{67}:\\
\;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

\end{array}
double f(double x, double y, double z) {
        double r1015627 = x;
        double r1015628 = r1015627 * r1015627;
        double r1015629 = y;
        double r1015630 = r1015629 * r1015629;
        double r1015631 = r1015628 + r1015630;
        double r1015632 = z;
        double r1015633 = r1015632 * r1015632;
        double r1015634 = r1015631 + r1015633;
        double r1015635 = 3.0;
        double r1015636 = r1015634 / r1015635;
        double r1015637 = sqrt(r1015636);
        return r1015637;
}

double f(double x, double y, double z) {
        double r1015638 = z;
        double r1015639 = -2.6686434086554147e+122;
        bool r1015640 = r1015638 <= r1015639;
        double r1015641 = 0.3333333333333333;
        double r1015642 = sqrt(r1015641);
        double r1015643 = r1015638 * r1015642;
        double r1015644 = -r1015643;
        double r1015645 = -2.6727131427269115e-197;
        bool r1015646 = r1015638 <= r1015645;
        double r1015647 = x;
        double r1015648 = y;
        double r1015649 = r1015648 * r1015648;
        double r1015650 = fma(r1015647, r1015647, r1015649);
        double r1015651 = fma(r1015638, r1015638, r1015650);
        double r1015652 = r1015641 * r1015651;
        double r1015653 = sqrt(r1015652);
        double r1015654 = -2.0033564760002915e-248;
        bool r1015655 = r1015638 <= r1015654;
        double r1015656 = 1.0;
        double r1015657 = 3.0;
        double r1015658 = cbrt(r1015657);
        double r1015659 = r1015656 / r1015658;
        double r1015660 = cbrt(r1015659);
        double r1015661 = r1015660 * r1015648;
        double r1015662 = cbrt(r1015658);
        double r1015663 = 2.0;
        double r1015664 = pow(r1015658, r1015663);
        double r1015665 = r1015662 * r1015664;
        double r1015666 = r1015656 / r1015665;
        double r1015667 = sqrt(r1015666);
        double r1015668 = r1015661 * r1015667;
        double r1015669 = 1.3138979852720737e+67;
        bool r1015670 = r1015638 <= r1015669;
        double r1015671 = r1015670 ? r1015653 : r1015643;
        double r1015672 = r1015655 ? r1015668 : r1015671;
        double r1015673 = r1015646 ? r1015653 : r1015672;
        double r1015674 = r1015640 ? r1015644 : r1015673;
        return r1015674;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.1
Target25.3
Herbie26.2
\[\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 4 regimes
  2. if z < -2.6686434086554147e+122

    1. Initial program 58.3

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(z \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    4. Simplified16.8

      \[\leadsto \color{blue}{-z \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]

    if -2.6686434086554147e+122 < z < -2.6727131427269115e-197 or -2.0033564760002915e-248 < z < 1.3138979852720737e+67

    1. Initial program 29.0

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{3}}}\]
    3. Taylor expanded around 0 29.0

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

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

    if -2.6727131427269115e-197 < z < -2.0033564760002915e-248

    1. Initial program 31.0

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{3}}}\]
    3. Using strategy rm
    4. Applied add-cube-cbrt31.0

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    5. Applied associate-/r*31.1

      \[\leadsto \sqrt{\color{blue}{\frac{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}{\sqrt[3]{3}}}}\]
    6. Using strategy rm
    7. Applied add-cube-cbrt31.1

      \[\leadsto \sqrt{\frac{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}{\sqrt[3]{\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}}\]
    8. Applied cbrt-prod31.1

      \[\leadsto \sqrt{\frac{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}{\color{blue}{\sqrt[3]{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \sqrt[3]{\sqrt[3]{3}}}}}\]
    9. Applied div-inv31.0

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

      \[\leadsto \sqrt{\color{blue}{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\sqrt[3]{\sqrt[3]{3} \cdot \sqrt[3]{3}}} \cdot \frac{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}{\sqrt[3]{\sqrt[3]{3}}}}}\]
    11. Applied sqrt-prod31.1

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\sqrt[3]{{\left(\sqrt[3]{3}\right)}^{2}}}}} \cdot \sqrt{\frac{\frac{1}{\sqrt[3]{3} \cdot \sqrt[3]{3}}}{\sqrt[3]{\sqrt[3]{3}}}}\]
    13. Simplified31.1

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}{\sqrt[3]{{\left(\sqrt[3]{3}\right)}^{2}}}} \cdot \color{blue}{\sqrt{\frac{1}{\sqrt[3]{\sqrt[3]{3}} \cdot {\left(\sqrt[3]{3}\right)}^{2}}}}\]
    14. Taylor expanded around 0 46.9

      \[\leadsto \color{blue}{\left({\left(\frac{1}{\sqrt[3]{3}}\right)}^{\frac{1}{3}} \cdot y\right)} \cdot \sqrt{\frac{1}{\sqrt[3]{\sqrt[3]{3}} \cdot {\left(\sqrt[3]{3}\right)}^{2}}}\]
    15. Simplified46.9

      \[\leadsto \color{blue}{\left(\sqrt[3]{\frac{1}{\sqrt[3]{3}}} \cdot y\right)} \cdot \sqrt{\frac{1}{\sqrt[3]{\sqrt[3]{3}} \cdot {\left(\sqrt[3]{3}\right)}^{2}}}\]

    if 1.3138979852720737e+67 < z

    1. Initial program 52.2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -2.668643408655414716179248580097977706017 \cdot 10^{122}:\\ \;\;\;\;-z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;z \le -2.672713142726911514272253621456748121583 \cdot 10^{-197}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -2.003356476000291454828377115726675914901 \cdot 10^{-248}:\\ \;\;\;\;\left(\sqrt[3]{\frac{1}{\sqrt[3]{3}}} \cdot y\right) \cdot \sqrt{\frac{1}{\sqrt[3]{\sqrt[3]{3}} \cdot {\left(\sqrt[3]{3}\right)}^{2}}}\\ \mathbf{elif}\;z \le 1.313897985272073697158734222693046230491 \cdot 10^{67}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, x, y \cdot y\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;z \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \end{array}\]

Reproduce

herbie shell --seed 2019305 +o rules:numerics
(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)))