Average Error: 30.5 → 17.1
Time: 3.4s
Precision: 64
Internal Precision: 128
\[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
\[\begin{array}{l} \mathbf{if}\;re \le -4.8902885864967844 \cdot 10^{+70}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{elif}\;re \le -8.325291793016466 \cdot 10^{-294}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{elif}\;re \le 5.980270498509465 \cdot 10^{-183}:\\ \;\;\;\;\log im\\ \mathbf{elif}\;re \le 5.694600262124617 \cdot 10^{+100}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{else}:\\ \;\;\;\;\log re\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Derivation

  1. Split input into 4 regimes
  2. if re < -4.8902885864967844e+70

    1. Initial program 46.0

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
    2. Taylor expanded around -inf 9.8

      \[\leadsto \log \color{blue}{\left(-1 \cdot re\right)}\]
    3. Simplified9.8

      \[\leadsto \log \color{blue}{\left(-re\right)}\]

    if -4.8902885864967844e+70 < re < -8.325291793016466e-294 or 5.980270498509465e-183 < re < 5.694600262124617e+100

    1. Initial program 18.6

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]

    if -8.325291793016466e-294 < re < 5.980270498509465e-183

    1. Initial program 30.9

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
    2. Taylor expanded around 0 32.7

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

    if 5.694600262124617e+100 < re

    1. Initial program 49.0

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
    2. Taylor expanded around inf 9.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -4.8902885864967844 \cdot 10^{+70}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{elif}\;re \le -8.325291793016466 \cdot 10^{-294}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{elif}\;re \le 5.980270498509465 \cdot 10^{-183}:\\ \;\;\;\;\log im\\ \mathbf{elif}\;re \le 5.694600262124617 \cdot 10^{+100}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{else}:\\ \;\;\;\;\log re\\ \end{array}\]

Reproduce

herbie shell --seed 2019089 
(FPCore (re im)
  :name "math.log/1 on complex, real part"
  (log (sqrt (+ (* re re) (* im im)))))