Average Error: 37.7 → 30.3
Time: 17.5s
Precision: 64
Internal Precision: 128
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[\begin{array}{l} \mathbf{if}\;re \le -8.634895542931288 \cdot 10^{+145}:\\ \;\;\;\;0\\ \mathbf{elif}\;re \le -1.3030772170163809 \cdot 10^{-151}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + im\right)} \cdot 0.5\\ \mathbf{elif}\;re \le 2.6485360946684548 \cdot 10^{+103}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + \sqrt{im \cdot im + re \cdot re}\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + re\right)} \cdot 0.5\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Target

Original37.7
Target32.6
Herbie30.3
\[\begin{array}{l} \mathbf{if}\;re \lt 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if re < -8.634895542931288e+145

    1. Initial program 61.8

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{0}}\]

    if -8.634895542931288e+145 < re < -1.3030772170163809e-151

    1. Initial program 43.7

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.7

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{\color{blue}{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im}}} + re\right)}\]
    4. Applied sqrt-prod44.7

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\sqrt{re \cdot re + im \cdot im}}} + re\right)}\]
    5. Taylor expanded around 0 46.2

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{im} + re\right)}\]

    if -1.3030772170163809e-151 < re < 2.6485360946684548e+103

    1. Initial program 23.9

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

    if 2.6485360946684548e+103 < re

    1. Initial program 48.7

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{re} + re\right)}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification30.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -8.634895542931288 \cdot 10^{+145}:\\ \;\;\;\;0\\ \mathbf{elif}\;re \le -1.3030772170163809 \cdot 10^{-151}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + im\right)} \cdot 0.5\\ \mathbf{elif}\;re \le 2.6485360946684548 \cdot 10^{+103}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + \sqrt{im \cdot im + re \cdot re}\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + re\right)} \cdot 0.5\\ \end{array}\]

Reproduce

herbie shell --seed 2019050 
(FPCore (re im)
  :name "math.sqrt on complex, real part"

  :herbie-target
  (if (< re 0) (* 0.5 (* (sqrt 2) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))