Average Error: 38.6 → 26.9
Time: 8.1s
Precision: binary64
\[\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\]
\[\begin{array}{l} \mathbf{if}\;z \le -6.4832562982794285 \cdot 10^{109}:\\ \;\;\;\;-\sqrt{\sqrt{0.333333333333333315}} \cdot \left(\sqrt{\sqrt{0.333333333333333315}} \cdot z\right)\\ \mathbf{elif}\;z \le -6.76711637978129667 \cdot 10^{-39}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -2.75293092782325262 \cdot 10^{-72}:\\ \;\;\;\;\frac{x}{\sqrt{3}}\\ \mathbf{elif}\;z \le 4.5250489584728597 \cdot 10^{-142}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le 3.2709223867076652 \cdot 10^{-84}:\\ \;\;\;\;\frac{-x}{\sqrt{3}}\\ \mathbf{elif}\;z \le 6.48185526099689738 \cdot 10^{118}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original38.6
Target25.5
Herbie26.9
\[\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 5 regimes
  2. if z < -6.4832562982794285e109

    1. Initial program 55.9

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

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

      \[\leadsto \sqrt{\color{blue}{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}}\]
    4. Taylor expanded around -inf 16.9

      \[\leadsto \color{blue}{-1 \cdot \left(\sqrt{0.333333333333333315} \cdot z\right)}\]
    5. Simplified16.9

      \[\leadsto \color{blue}{-\sqrt{0.333333333333333315} \cdot z}\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt16.9

      \[\leadsto -\sqrt{\color{blue}{\sqrt{0.333333333333333315} \cdot \sqrt{0.333333333333333315}}} \cdot z\]
    8. Applied sqrt-prod16.9

      \[\leadsto -\color{blue}{\left(\sqrt{\sqrt{0.333333333333333315}} \cdot \sqrt{\sqrt{0.333333333333333315}}\right)} \cdot z\]
    9. Applied associate-*l*16.8

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

    if -6.4832562982794285e109 < z < -6.76711637978129667e-39 or -2.75293092782325262e-72 < z < 4.5250489584728597e-142 or 3.2709223867076652e-84 < z < 6.48185526099689738e118

    1. Initial program 29.7

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

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

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

    if -6.76711637978129667e-39 < z < -2.75293092782325262e-72

    1. Initial program 29.9

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

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

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

    if 4.5250489584728597e-142 < z < 3.2709223867076652e-84

    1. Initial program 28.5

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

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

      \[\leadsto \frac{\color{blue}{-1 \cdot x}}{\sqrt{3}}\]
    5. Simplified46.4

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

    if 6.48185526099689738e118 < z

    1. Initial program 57.8

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

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

      \[\leadsto \frac{\color{blue}{z}}{\sqrt{3}}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification26.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \le -6.4832562982794285 \cdot 10^{109}:\\ \;\;\;\;-\sqrt{\sqrt{0.333333333333333315}} \cdot \left(\sqrt{\sqrt{0.333333333333333315}} \cdot z\right)\\ \mathbf{elif}\;z \le -6.76711637978129667 \cdot 10^{-39}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le -2.75293092782325262 \cdot 10^{-72}:\\ \;\;\;\;\frac{x}{\sqrt{3}}\\ \mathbf{elif}\;z \le 4.5250489584728597 \cdot 10^{-142}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}\\ \mathbf{elif}\;z \le 3.2709223867076652 \cdot 10^{-84}:\\ \;\;\;\;\frac{-x}{\sqrt{3}}\\ \mathbf{elif}\;z \le 6.48185526099689738 \cdot 10^{118}:\\ \;\;\;\;\sqrt{0.333333333333333315 \cdot \left(z \cdot z + \left(x \cdot x + y \cdot y\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{z}{\sqrt{3}}\\ \end{array}\]

Reproduce

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