Average Error: 38.1 → 25.7
Time: 38.9s
Precision: 64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;y \le -3.128400752833148256453905231462512587051 \cdot 10^{97}:\\ \;\;\;\;\frac{-y}{\sqrt{3}}\\ \mathbf{elif}\;y \le 6.108124318137310592207377361360645180152 \cdot 10^{107}:\\ \;\;\;\;\sqrt{0.3333333333333333148296162562473909929395 \cdot \mathsf{fma}\left(y, y, \mathsf{fma}\left(z, z, x \cdot x\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{\sqrt{3}}\\ \end{array}\]
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\begin{array}{l}
\mathbf{if}\;y \le -3.128400752833148256453905231462512587051 \cdot 10^{97}:\\
\;\;\;\;\frac{-y}{\sqrt{3}}\\

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

\mathbf{else}:\\
\;\;\;\;\frac{y}{\sqrt{3}}\\

\end{array}
double f(double x, double y, double z) {
        double r27108465 = x;
        double r27108466 = r27108465 * r27108465;
        double r27108467 = y;
        double r27108468 = r27108467 * r27108467;
        double r27108469 = r27108466 + r27108468;
        double r27108470 = z;
        double r27108471 = r27108470 * r27108470;
        double r27108472 = r27108469 + r27108471;
        double r27108473 = 3.0;
        double r27108474 = r27108472 / r27108473;
        double r27108475 = sqrt(r27108474);
        return r27108475;
}

double f(double x, double y, double z) {
        double r27108476 = y;
        double r27108477 = -3.1284007528331483e+97;
        bool r27108478 = r27108476 <= r27108477;
        double r27108479 = -r27108476;
        double r27108480 = 3.0;
        double r27108481 = sqrt(r27108480);
        double r27108482 = r27108479 / r27108481;
        double r27108483 = 6.10812431813731e+107;
        bool r27108484 = r27108476 <= r27108483;
        double r27108485 = 0.3333333333333333;
        double r27108486 = z;
        double r27108487 = x;
        double r27108488 = r27108487 * r27108487;
        double r27108489 = fma(r27108486, r27108486, r27108488);
        double r27108490 = fma(r27108476, r27108476, r27108489);
        double r27108491 = r27108485 * r27108490;
        double r27108492 = sqrt(r27108491);
        double r27108493 = r27108476 / r27108481;
        double r27108494 = r27108484 ? r27108492 : r27108493;
        double r27108495 = r27108478 ? r27108482 : r27108494;
        return r27108495;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.1
Target26.0
Herbie25.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 3 regimes
  2. if y < -3.1284007528331483e+97

    1. Initial program 54.4

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

      \[\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 sqrt-div54.5

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

      \[\leadsto \color{blue}{-1 \cdot \frac{y}{\sqrt{3}}}\]
    6. Simplified18.2

      \[\leadsto \color{blue}{-\frac{y}{\sqrt{3}}}\]

    if -3.1284007528331483e+97 < y < 6.10812431813731e+107

    1. Initial program 29.4

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

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

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

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

    if 6.10812431813731e+107 < y

    1. Initial program 55.8

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Simplified55.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 sqrt-div55.8

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

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

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

Reproduce

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