Average Error: 29.1 → 16.9
Time: 16.7s
Precision: 64
Internal Precision: 128
\[\sqrt{re \cdot re + im \cdot im}\]
\[\begin{array}{l} \mathbf{if}\;re \le -1.4704326174824006 \cdot 10^{+137}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le 3.4569172321739565 \cdot 10^{-295}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{elif}\;re \le 1.3867348397301926 \cdot 10^{-270}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 8.102344560024491 \cdot 10^{+124}:\\ \;\;\;\;\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 < -1.4704326174824006e+137

    1. Initial program 54.5

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

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

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

    if -1.4704326174824006e+137 < re < 3.4569172321739565e-295 or 1.3867348397301926e-270 < re < 8.102344560024491e+124

    1. Initial program 19.5

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

    if 3.4569172321739565e-295 < re < 1.3867348397301926e-270

    1. Initial program 33.4

      \[\sqrt{re \cdot re + im \cdot im}\]
    2. Taylor expanded around 0 32.6

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

    if 8.102344560024491e+124 < re

    1. Initial program 52.1

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

      \[\leadsto \color{blue}{e^{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}}\]
    4. Taylor expanded around inf 10.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -1.4704326174824006 \cdot 10^{+137}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le 3.4569172321739565 \cdot 10^{-295}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{elif}\;re \le 1.3867348397301926 \cdot 10^{-270}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 8.102344560024491 \cdot 10^{+124}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Reproduce

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