Average Error: 37.3 → 22.4
Time: 16.1s
Precision: 64
Internal Precision: 3392
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[\begin{array}{l} \mathbf{if}\;im \le -3.754574615182041 \cdot 10^{+122}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le -2.3872624570469995 \cdot 10^{-82}:\\ \;\;\;\;\frac{\sqrt{2.0 \cdot \left(im \cdot im\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{elif}\;im \le 2.7535012867156775 \cdot 10^{-139}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\ \mathbf{elif}\;im \le 4.817082733543284 \cdot 10^{+45}:\\ \;\;\;\;\frac{\sqrt{2.0 \cdot \left(im \cdot im\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(re + im\right) \cdot 2.0}\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original37.3
Target32.4
Herbie22.4
\[\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 im < -3.754574615182041e+122

    1. Initial program 54.0

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{\color{blue}{im \cdot im + re \cdot re}} + re\right)}\]
    4. Taylor expanded around -inf 8.2

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

    if -3.754574615182041e+122 < im < -2.3872624570469995e-82 or 2.7535012867156775e-139 < im < 4.817082733543284e+45

    1. Initial program 23.9

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{\color{blue}{im \cdot im + re \cdot re}} + re\right)}\]
    4. Using strategy rm
    5. Applied flip-+31.2

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{\frac{\sqrt{im \cdot im + re \cdot re} \cdot \sqrt{im \cdot im + re \cdot re} - re \cdot re}{\sqrt{im \cdot im + re \cdot re} - re}}}\]
    6. Applied associate-*r/31.2

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

      \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2.0 \cdot \left(\sqrt{im \cdot im + re \cdot re} \cdot \sqrt{im \cdot im + re \cdot re} - re \cdot re\right)}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}}}\]
    8. Simplified23.1

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

    if -2.3872624570469995e-82 < im < 2.7535012867156775e-139

    1. Initial program 39.1

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

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

    if 4.817082733543284e+45 < im

    1. Initial program 42.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \le -3.754574615182041 \cdot 10^{+122}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le -2.3872624570469995 \cdot 10^{-82}:\\ \;\;\;\;\frac{\sqrt{2.0 \cdot \left(im \cdot im\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{elif}\;im \le 2.7535012867156775 \cdot 10^{-139}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\ \mathbf{elif}\;im \le 4.817082733543284 \cdot 10^{+45}:\\ \;\;\;\;\frac{\sqrt{2.0 \cdot \left(im \cdot im\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(re + im\right) \cdot 2.0}\\ \end{array}\]

Runtime

Time bar (total: 16.1s)Debug logProfile

herbie shell --seed 2018230 
(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)))))