Average Error: 37.8 → 26.5
Time: 3.7s
Precision: binary64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;x \le -1.62541230777024819 \cdot 10^{124}:\\ \;\;\;\;x \cdot \left(-\sqrt{0.333333333333333315}\right)\\ \mathbf{elif}\;x \le 1.9270970714388626 \cdot 10^{-244}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}\\ \mathbf{elif}\;x \le 7.533467640903301 \cdot 10^{-175}:\\ \;\;\;\;\sqrt{z} \cdot \frac{\sqrt{z}}{\sqrt{3}}\\ \mathbf{elif}\;x \le 3.7246790297724069 \cdot 10^{84}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}\\ \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.8
Herbie26.5
\[\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 < -1.62541230777024819e124

    1. Initial program 57.7

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \sqrt{0.333333333333333315}\right)}\]
    3. Simplified17.7

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

    if -1.62541230777024819e124 < x < 1.9270970714388626e-244 or 7.533467640903301e-175 < x < 3.7246790297724069e84

    1. Initial program 28.7

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

      \[\leadsto \sqrt{\color{blue}{0.333333333333333315 \cdot {x}^{2} + \left(0.333333333333333315 \cdot {y}^{2} + 0.333333333333333315 \cdot {z}^{2}\right)}}\]
    3. Simplified28.8

      \[\leadsto \sqrt{\color{blue}{\left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right) \cdot 0.333333333333333315}}\]

    if 1.9270970714388626e-244 < x < 7.533467640903301e-175

    1. Initial program 33.2

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

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

      \[\leadsto \frac{\color{blue}{\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}}}{\sqrt{3}}\]
    5. Taylor expanded around 0 47.9

      \[\leadsto \frac{\color{blue}{z}}{\sqrt{3}}\]
    6. Using strategy rm
    7. Applied *-un-lft-identity47.9

      \[\leadsto \frac{z}{\sqrt{\color{blue}{1 \cdot 3}}}\]
    8. Applied sqrt-prod47.9

      \[\leadsto \frac{z}{\color{blue}{\sqrt{1} \cdot \sqrt{3}}}\]
    9. Applied add-sqr-sqrt48.4

      \[\leadsto \frac{\color{blue}{\sqrt{z} \cdot \sqrt{z}}}{\sqrt{1} \cdot \sqrt{3}}\]
    10. Applied times-frac48.4

      \[\leadsto \color{blue}{\frac{\sqrt{z}}{\sqrt{1}} \cdot \frac{\sqrt{z}}{\sqrt{3}}}\]
    11. Simplified48.4

      \[\leadsto \color{blue}{\sqrt{z}} \cdot \frac{\sqrt{z}}{\sqrt{3}}\]

    if 3.7246790297724069e84 < x

    1. Initial program 53.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -1.62541230777024819 \cdot 10^{124}:\\ \;\;\;\;x \cdot \left(-\sqrt{0.333333333333333315}\right)\\ \mathbf{elif}\;x \le 1.9270970714388626 \cdot 10^{-244}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}\\ \mathbf{elif}\;x \le 7.533467640903301 \cdot 10^{-175}:\\ \;\;\;\;\sqrt{z} \cdot \frac{\sqrt{z}}{\sqrt{3}}\\ \mathbf{elif}\;x \le 3.7246790297724069 \cdot 10^{84}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(x \cdot x + \left(y \cdot y + z \cdot z\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \sqrt{0.333333333333333315}\\ \end{array}\]

Reproduce

herbie shell --seed 2020179 
(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)))