Average Error: 38.0 → 23.1
Time: 17.6s
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 -1.854573052548613 \cdot 10^{+56}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le -1.7827762828361535 \cdot 10^{-159}:\\ \;\;\;\;\frac{\sqrt{\left(2.0 \cdot im\right) \cdot im}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{elif}\;im \le 3.9669270495057156 \cdot 10^{-112}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 4.0}\\ \mathbf{elif}\;im \le 4.415762985858351 \cdot 10^{+29}:\\ \;\;\;\;\frac{\sqrt{\left(2.0 \cdot im\right) \cdot im}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + im\right)}\\ \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

Original38.0
Target32.9
Herbie23.1
\[\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 < -1.854573052548613e+56

    1. Initial program 45.1

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2.0 \cdot re - 2.0 \cdot im}}\]
    4. Simplified12.1

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

    if -1.854573052548613e+56 < im < -1.7827762828361535e-159 or 3.9669270495057156e-112 < im < 4.415762985858351e+29

    1. Initial program 26.1

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

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

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

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

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

    if -1.7827762828361535e-159 < im < 3.9669270495057156e-112

    1. Initial program 41.6

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{4.0 \cdot re}}\]

    if 4.415762985858351e+29 < im

    1. Initial program 42.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \le -1.854573052548613 \cdot 10^{+56}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le -1.7827762828361535 \cdot 10^{-159}:\\ \;\;\;\;\frac{\sqrt{\left(2.0 \cdot im\right) \cdot im}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{elif}\;im \le 3.9669270495057156 \cdot 10^{-112}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 4.0}\\ \mathbf{elif}\;im \le 4.415762985858351 \cdot 10^{+29}:\\ \;\;\;\;\frac{\sqrt{\left(2.0 \cdot im\right) \cdot im}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + im\right)}\\ \end{array}\]

Runtime

Time bar (total: 17.6s)Debug logProfile

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