Average Error: 37.8 → 26.3
Time: 5.3s
Precision: binary64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -5.1288480872812052 \cdot 10^{147}:\\ \;\;\;\;\frac{-x}{\sqrt{3}}\\ \mathbf{elif}\;x \le -1.69267376453504879 \cdot 10^{-304}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{elif}\;x \le 6.2830035489969078 \cdot 10^{-285}:\\ \;\;\;\;z \cdot \sqrt{0.333333333333333315}\\ \mathbf{elif}\;x \le 4.2028759973580324 \cdot 10^{40}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original37.8
Target25.3
Herbie26.3
\[\begin{array}{l} \mathbf{if}\;z \lt -6.3964793941097758 \cdot 10^{136}:\\ \;\;\;\;\frac{-z}{\sqrt{3}}\\ \mathbf{elif}\;z \lt 7.3202936944041821 \cdot 10^{117}:\\ \;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.333333333333333315} \cdot z\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if x < -5.1288480872812052e147

    1. Initial program 62.8

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

      \[\leadsto \sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{\color{blue}{\sqrt{3} \cdot \sqrt{3}}}}\]
    4. Applied associate-/r*62.8

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

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

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{\sqrt{3}}}\]
    7. Simplified16.7

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

    if -5.1288480872812052e147 < x < -1.69267376453504879e-304 or 6.2830035489969078e-285 < x < 4.2028759973580324e40

    1. Initial program 28.9

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

    if -1.69267376453504879e-304 < x < 6.2830035489969078e-285

    1. Initial program 32.6

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

      \[\leadsto \color{blue}{z \cdot \sqrt{0.333333333333333315}}\]

    if 4.2028759973580324e40 < x

    1. Initial program 48.3

      \[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
    2. Taylor expanded around inf 22.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -5.1288480872812052 \cdot 10^{147}:\\ \;\;\;\;\frac{-x}{\sqrt{3}}\\ \mathbf{elif}\;x \le -1.69267376453504879 \cdot 10^{-304}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{elif}\;x \le 6.2830035489969078 \cdot 10^{-285}:\\ \;\;\;\;z \cdot \sqrt{0.333333333333333315}\\ \mathbf{elif}\;x \le 4.2028759973580324 \cdot 10^{40}:\\ \;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Reproduce

herbie shell --seed 2020185 
(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.396479394109776e+136) (/ (neg 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)))