Average Error: 37.7 → 25.5
Time: 1.1m
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -2.114597052343943905521456364925785524812 \cdot 10^{96}:\\ \;\;\;\;\left(-\sqrt{\sqrt{0.3333333333333333148296162562473909929395}}\right) \cdot \left(\sqrt{\sqrt{0.3333333333333333148296162562473909929395}} \cdot x\right)\\ \mathbf{elif}\;x \le 9.445373192608023995102518569819932542768 \cdot 10^{93}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt[3]{3}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot x\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;x \le -2.114597052343943905521456364925785524812 \cdot 10^{96}:\\
\;\;\;\;\left(-\sqrt{\sqrt{0.3333333333333333148296162562473909929395}}\right) \cdot \left(\sqrt{\sqrt{0.3333333333333333148296162562473909929395}} \cdot x\right)\\

\mathbf{elif}\;x \le 9.445373192608023995102518569819932542768 \cdot 10^{93}:\\
\;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt[3]{3}}}\\

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

\end{array}
double f(double x, double y, double z) {
        double r39488579 = x;
        double r39488580 = r39488579 * r39488579;
        double r39488581 = y;
        double r39488582 = r39488581 * r39488581;
        double r39488583 = r39488580 + r39488582;
        double r39488584 = z;
        double r39488585 = r39488584 * r39488584;
        double r39488586 = r39488583 + r39488585;
        double r39488587 = 3.0;
        double r39488588 = r39488586 / r39488587;
        double r39488589 = sqrt(r39488588);
        return r39488589;
}

double f(double x, double y, double z) {
        double r39488590 = x;
        double r39488591 = -2.114597052343944e+96;
        bool r39488592 = r39488590 <= r39488591;
        double r39488593 = 0.3333333333333333;
        double r39488594 = sqrt(r39488593);
        double r39488595 = sqrt(r39488594);
        double r39488596 = -r39488595;
        double r39488597 = r39488595 * r39488590;
        double r39488598 = r39488596 * r39488597;
        double r39488599 = 9.445373192608024e+93;
        bool r39488600 = r39488590 <= r39488599;
        double r39488601 = y;
        double r39488602 = z;
        double r39488603 = r39488602 * r39488602;
        double r39488604 = fma(r39488601, r39488601, r39488603);
        double r39488605 = fma(r39488590, r39488590, r39488604);
        double r39488606 = sqrt(r39488605);
        double r39488607 = 3.0;
        double r39488608 = cbrt(r39488607);
        double r39488609 = r39488608 * r39488608;
        double r39488610 = r39488606 / r39488609;
        double r39488611 = r39488606 / r39488608;
        double r39488612 = r39488610 * r39488611;
        double r39488613 = sqrt(r39488612);
        double r39488614 = r39488594 * r39488590;
        double r39488615 = r39488600 ? r39488613 : r39488614;
        double r39488616 = r39488592 ? r39488598 : r39488615;
        return r39488616;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.7
Target26.0
Herbie25.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 3 regimes
  2. if x < -2.114597052343944e+96

    1. Initial program 54.0

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    7. Simplified18.1

      \[\leadsto \color{blue}{-\sqrt{0.3333333333333333148296162562473909929395} \cdot x}\]
    8. Using strategy rm
    9. Applied add-sqr-sqrt18.1

      \[\leadsto -\sqrt{\color{blue}{\sqrt{0.3333333333333333148296162562473909929395} \cdot \sqrt{0.3333333333333333148296162562473909929395}}} \cdot x\]
    10. Applied sqrt-prod18.1

      \[\leadsto -\color{blue}{\left(\sqrt{\sqrt{0.3333333333333333148296162562473909929395}} \cdot \sqrt{\sqrt{0.3333333333333333148296162562473909929395}}\right)} \cdot x\]
    11. Applied associate-*l*18.0

      \[\leadsto -\color{blue}{\sqrt{\sqrt{0.3333333333333333148296162562473909929395}} \cdot \left(\sqrt{\sqrt{0.3333333333333333148296162562473909929395}} \cdot x\right)}\]

    if -2.114597052343944e+96 < x < 9.445373192608024e+93

    1. Initial program 29.3

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

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

      \[\leadsto \sqrt{\frac{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}{\color{blue}{\left(\sqrt[3]{3} \cdot \sqrt[3]{3}\right) \cdot \sqrt[3]{3}}}}\]
    5. Applied add-sqr-sqrt29.3

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

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

    if 9.445373192608024e+93 < x

    1. Initial program 53.4

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

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

      \[\leadsto \color{blue}{x \cdot \sqrt{0.3333333333333333148296162562473909929395}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification25.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -2.114597052343943905521456364925785524812 \cdot 10^{96}:\\ \;\;\;\;\left(-\sqrt{\sqrt{0.3333333333333333148296162562473909929395}}\right) \cdot \left(\sqrt{\sqrt{0.3333333333333333148296162562473909929395}} \cdot x\right)\\ \mathbf{elif}\;x \le 9.445373192608023995102518569819932542768 \cdot 10^{93}:\\ \;\;\;\;\sqrt{\frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt[3]{3} \cdot \sqrt[3]{3}} \cdot \frac{\sqrt{\mathsf{fma}\left(x, x, \mathsf{fma}\left(y, y, z \cdot z\right)\right)}}{\sqrt[3]{3}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot x\\ \end{array}\]

Reproduce

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