Average Error: 37.8 → 25.5
Time: 16.4s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -6.589438735556140022290845502003800140082 \cdot 10^{117}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 1.196300017523969104797561870702778807544 \cdot 10^{127}:\\ \;\;\;\;\sqrt{\frac{1}{\frac{3}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -6.589438735556140022290845502003800140082 \cdot 10^{117}:\\
\;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\

\mathbf{elif}\;y \le 1.196300017523969104797561870702778807544 \cdot 10^{127}:\\
\;\;\;\;\sqrt{\frac{1}{\frac{3}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}}\\

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

\end{array}
double f(double x, double y, double z) {
        double r39215564 = x;
        double r39215565 = r39215564 * r39215564;
        double r39215566 = y;
        double r39215567 = r39215566 * r39215566;
        double r39215568 = r39215565 + r39215567;
        double r39215569 = z;
        double r39215570 = r39215569 * r39215569;
        double r39215571 = r39215568 + r39215570;
        double r39215572 = 3.0;
        double r39215573 = r39215571 / r39215572;
        double r39215574 = sqrt(r39215573);
        return r39215574;
}

double f(double x, double y, double z) {
        double r39215575 = y;
        double r39215576 = -6.58943873555614e+117;
        bool r39215577 = r39215575 <= r39215576;
        double r39215578 = -r39215575;
        double r39215579 = 0.3333333333333333;
        double r39215580 = sqrt(r39215579);
        double r39215581 = r39215578 * r39215580;
        double r39215582 = 1.196300017523969e+127;
        bool r39215583 = r39215575 <= r39215582;
        double r39215584 = 1.0;
        double r39215585 = 3.0;
        double r39215586 = x;
        double r39215587 = z;
        double r39215588 = r39215587 * r39215587;
        double r39215589 = fma(r39215586, r39215586, r39215588);
        double r39215590 = fma(r39215575, r39215575, r39215589);
        double r39215591 = r39215585 / r39215590;
        double r39215592 = r39215584 / r39215591;
        double r39215593 = sqrt(r39215592);
        double r39215594 = r39215580 * r39215575;
        double r39215595 = r39215583 ? r39215593 : r39215594;
        double r39215596 = r39215577 ? r39215581 : r39215595;
        return r39215596;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.8
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 y < -6.58943873555614e+117

    1. Initial program 56.8

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

      \[\leadsto \color{blue}{\sqrt{\frac{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}{3}}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity56.8

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \sqrt{0.3333333333333333148296162562473909929395}\right)}\]
    10. Simplified17.4

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

    if -6.58943873555614e+117 < y < 1.196300017523969e+127

    1. Initial program 29.1

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

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

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

    if 1.196300017523969e+127 < y

    1. Initial program 58.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -6.589438735556140022290845502003800140082 \cdot 10^{117}:\\ \;\;\;\;\left(-y\right) \cdot \sqrt{0.3333333333333333148296162562473909929395}\\ \mathbf{elif}\;y \le 1.196300017523969104797561870702778807544 \cdot 10^{127}:\\ \;\;\;\;\sqrt{\frac{1}{\frac{3}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, z \cdot z\right)\right)}}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395} \cdot y\\ \end{array}\]

Reproduce

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