Average Error: 29.9 → 17.7
Time: 7.2s
Precision: 64
Internal Precision: 128
\[\sqrt{re \cdot re + im \cdot im}\]
\[\begin{array}{l} \mathbf{if}\;re \le -2.211477174607664 \cdot 10^{+132}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le -6.1172800801992716 \cdot 10^{-180}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{elif}\;re \le 1.448219358583589 \cdot 10^{-241}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 1.2587878943940152 \cdot 10^{+113}:\\ \;\;\;\;\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 < -2.211477174607664e+132

    1. Initial program 54.8

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

      \[\leadsto \color{blue}{-1 \cdot re}\]
    3. Simplified8.7

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

    if -2.211477174607664e+132 < re < -6.1172800801992716e-180 or 1.448219358583589e-241 < re < 1.2587878943940152e+113

    1. Initial program 17.3

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

    if -6.1172800801992716e-180 < re < 1.448219358583589e-241

    1. Initial program 30.2

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

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

      \[\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 35.8

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

    if 1.2587878943940152e+113 < re

    1. Initial program 50.1

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

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

      \[\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 9.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -2.211477174607664 \cdot 10^{+132}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le -6.1172800801992716 \cdot 10^{-180}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{elif}\;re \le 1.448219358583589 \cdot 10^{-241}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 1.2587878943940152 \cdot 10^{+113}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Reproduce

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