Average Error: 29.6 → 16.3
Time: 2.9s
Precision: 64
Internal Precision: 128
\[\sqrt{re \cdot re + im \cdot im}\]
\[\begin{array}{l} \mathbf{if}\;re \le -6.6389542148905856 \cdot 10^{+153}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le 4.615949737328477 \cdot 10^{+116}:\\ \;\;\;\;\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 3 regimes
  2. if re < -6.6389542148905856e+153

    1. Initial program 59.4

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

      \[\leadsto \color{blue}{-1 \cdot re}\]
    3. Simplified7.0

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

    if -6.6389542148905856e+153 < re < 4.615949737328477e+116

    1. Initial program 19.6

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

    if 4.615949737328477e+116 < re

    1. Initial program 51.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -6.6389542148905856 \cdot 10^{+153}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le 4.615949737328477 \cdot 10^{+116}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Reproduce

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