Average Error: 31.2 → 17.3
Time: 2.2s
Precision: binary64
\[\sqrt{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;x \le -7.90225451676893219 \cdot 10^{121}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \le -7.40575213623674744 \cdot 10^{-278}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{elif}\;x \le 4.3980769884343018 \cdot 10^{-199}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \le 1.6129701241769684 \cdot 10^{115}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Target

Original31.2
Target17.1
Herbie17.3
\[\begin{array}{l} \mathbf{if}\;x \lt -1.123695082659983 \cdot 10^{145}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \lt 1.11655762118336204 \cdot 10^{93}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if x < -7.90225451676893219e121

    1. Initial program 55.9

      \[\sqrt{x \cdot x + y \cdot y}\]
    2. Taylor expanded around -inf 9.2

      \[\leadsto \color{blue}{-1 \cdot x}\]
    3. Simplified9.2

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

    if -7.90225451676893219e121 < x < -7.40575213623674744e-278 or 4.3980769884343018e-199 < x < 1.6129701241769684e115

    1. Initial program 18.6

      \[\sqrt{x \cdot x + y \cdot y}\]

    if -7.40575213623674744e-278 < x < 4.3980769884343018e-199

    1. Initial program 30.3

      \[\sqrt{x \cdot x + y \cdot y}\]
    2. Taylor expanded around 0 32.9

      \[\leadsto \color{blue}{y}\]

    if 1.6129701241769684e115 < x

    1. Initial program 54.3

      \[\sqrt{x \cdot x + y \cdot y}\]
    2. Taylor expanded around inf 9.3

      \[\leadsto \color{blue}{x}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification17.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -7.90225451676893219 \cdot 10^{121}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \le -7.40575213623674744 \cdot 10^{-278}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{elif}\;x \le 4.3980769884343018 \cdot 10^{-199}:\\ \;\;\;\;y\\ \mathbf{elif}\;x \le 1.6129701241769684 \cdot 10^{115}:\\ \;\;\;\;\sqrt{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array}\]

Reproduce

herbie shell --seed 2020184 
(FPCore (x y)
  :name "Data.Octree.Internal:octantDistance  from Octree-0.5.4.2"
  :precision binary64

  :herbie-target
  (if (< x -1.1236950826599826e+145) (neg x) (if (< x 1.116557621183362e+93) (sqrt (+ (* x x) (* y y))) x))

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