bug366 (missed optimization)

Percentage Accurate: 44.6% → 99.1%
Time: 3.0s
Alternatives: 4
Speedup: N/A×

Specification

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

Your Program's Arguments

Results

Enter valid numbers for all inputs

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Alternative 1: 99.1% accurate, 1.1× speedup?

\[\mathsf{hypot}\left(z, x\right) \]
Derivation
  1. Initial program 48.0%

    \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  2. Taylor expanded in y around 0 32.0%

    \[\leadsto \color{blue}{\sqrt{{z}^{2} + {x}^{2}}} \]
  3. Step-by-step derivation
    1. unpow232.0%

      \[\leadsto \sqrt{\color{blue}{z \cdot z} + {x}^{2}} \]
    2. unpow232.0%

      \[\leadsto \sqrt{z \cdot z + \color{blue}{x \cdot x}} \]
    3. hypot-def60.4%

      \[\leadsto \color{blue}{\mathsf{hypot}\left(z, x\right)} \]
  4. Simplified60.4%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(z, x\right)} \]
  5. Final simplification60.4%

    \[\leadsto \mathsf{hypot}\left(z, x\right) \]

Alternative 2: 80.0% accurate, 1.0× speedup?

\[\begin{array}{l} \mathbf{if}\;z \leq 350:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \leq 9.5 \cdot 10^{+56}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 8.8 \cdot 10^{+80}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if z < 350 or 9.4999999999999997e56 < z < 8.80000000000000011e80

    1. Initial program 51.1%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around 0 36.8%

      \[\leadsto \color{blue}{\sqrt{{y}^{2} + {x}^{2}}} \]
    3. Step-by-step derivation
      1. unpow236.8%

        \[\leadsto \sqrt{\color{blue}{y \cdot y} + {x}^{2}} \]
      2. unpow236.8%

        \[\leadsto \sqrt{y \cdot y + \color{blue}{x \cdot x}} \]
      3. hypot-def76.9%

        \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]
    4. Simplified76.9%

      \[\leadsto \color{blue}{\mathsf{hypot}\left(y, x\right)} \]

    if 350 < z < 9.4999999999999997e56 or 8.80000000000000011e80 < z

    1. Initial program 36.3%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around inf 60.6%

      \[\leadsto \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 350:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{elif}\;z \leq 9.5 \cdot 10^{+56}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 8.8 \cdot 10^{+80}:\\ \;\;\;\;\mathsf{hypot}\left(y, x\right)\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternative 3: 79.4% accurate, 13.6× speedup?

\[\begin{array}{l} \mathbf{if}\;z \leq 122:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 1.5 \cdot 10^{+62}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 4 \cdot 10^{+80}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]
Derivation
  1. Split input into 2 regimes
  2. if z < 122 or 1.5e62 < z < 4e80

    1. Initial program 50.9%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in x around -inf 19.0%

      \[\leadsto \color{blue}{-1 \cdot x} \]
    3. Step-by-step derivation
      1. mul-1-neg19.0%

        \[\leadsto \color{blue}{-x} \]
    4. Simplified19.0%

      \[\leadsto \color{blue}{-x} \]

    if 122 < z < 1.5e62 or 4e80 < z

    1. Initial program 37.5%

      \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
    2. Taylor expanded in z around inf 59.8%

      \[\leadsto \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification27.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 122:\\ \;\;\;\;-x\\ \mathbf{elif}\;z \leq 1.5 \cdot 10^{+62}:\\ \;\;\;\;z\\ \mathbf{elif}\;z \leq 4 \cdot 10^{+80}:\\ \;\;\;\;-x\\ \mathbf{else}:\\ \;\;\;\;z\\ \end{array} \]

Alternative 4: 51.6% accurate, 111.0× speedup?

\[z \]
Derivation
  1. Initial program 48.0%

    \[\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)} \]
  2. Taylor expanded in z around inf 17.4%

    \[\leadsto \color{blue}{z} \]
  3. Final simplification17.4%

    \[\leadsto z \]

Developer target: 100.0% accurate, 0.5× speedup?

\[\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right) \]

Reproduce

?
herbie shell --seed 2023167 
(FPCore (x y z)
  :name "bug366 (missed optimization)"
  :precision binary64

  :herbie-target
  (hypot x (hypot y z))

  (sqrt (+ (* x x) (+ (* y y) (* z z)))))