Average Error: 29.4 → 17.2
Time: 7.4s
Precision: 64
Internal Precision: 128
\[\sqrt{re \cdot re + im \cdot im}\]
\[\begin{array}{l} \mathbf{if}\;re \le -6.957358427304246 \cdot 10^{+117}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le -1.167103569755353 \cdot 10^{-292}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{elif}\;re \le 5.980270498509465 \cdot 10^{-183}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 1.393393615633757 \cdot 10^{+101}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Derivation

  1. Split input into 4 regimes
  2. if re < -6.957358427304246e+117

    1. Initial program 51.2

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

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

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

    if -6.957358427304246e+117 < re < -1.167103569755353e-292 or 5.980270498509465e-183 < re < 1.393393615633757e+101

    1. Initial program 17.8

      \[\sqrt{re \cdot re + im \cdot im}\]

    if -1.167103569755353e-292 < re < 5.980270498509465e-183

    1. Initial program 29.8

      \[\sqrt{re \cdot re + im \cdot im}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt29.8

      \[\leadsto \sqrt{\color{blue}{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im}}}\]
    4. Applied sqrt-prod30.1

      \[\leadsto \color{blue}{\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\sqrt{re \cdot re + im \cdot im}}}\]
    5. Taylor expanded around 0 33.3

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

    if 1.393393615633757e+101 < re

    1. Initial program 47.0

      \[\sqrt{re \cdot re + im \cdot im}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt47.0

      \[\leadsto \sqrt{\color{blue}{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im}}}\]
    4. Applied sqrt-prod47.1

      \[\leadsto \color{blue}{\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\sqrt{re \cdot re + im \cdot im}}}\]
    5. Taylor expanded around inf 11.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -6.957358427304246 \cdot 10^{+117}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le -1.167103569755353 \cdot 10^{-292}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{elif}\;re \le 5.980270498509465 \cdot 10^{-183}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 1.393393615633757 \cdot 10^{+101}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Reproduce

herbie shell --seed 2019089 
(FPCore (re im)
  :name "math.abs on complex"
  (sqrt (+ (* re re) (* im im))))