Average Error: 29.4 → 17.5
Time: 3.2s
Precision: 64
Internal Precision: 128
\[\sqrt{re \cdot re + im \cdot im}\]
\[\begin{array}{l} \mathbf{if}\;re \le -3.7441275622093686 \cdot 10^{+125}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le -1.864857452516667 \cdot 10^{+92}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 8.892143633694647 \cdot 10^{+146}:\\ \;\;\;\;\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 < -3.7441275622093686e+125

    1. Initial program 52.2

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

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

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

    if -3.7441275622093686e+125 < re < -1.864857452516667e+92

    1. Initial program 14.0

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

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

    if -1.864857452516667e+92 < re < 8.892143633694647e+146

    1. Initial program 19.9

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

    if 8.892143633694647e+146 < re

    1. Initial program 57.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -3.7441275622093686 \cdot 10^{+125}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \le -1.864857452516667 \cdot 10^{+92}:\\ \;\;\;\;im\\ \mathbf{elif}\;re \le 8.892143633694647 \cdot 10^{+146}:\\ \;\;\;\;\sqrt{im \cdot im + re \cdot re}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Reproduce

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