Average Error: 38.5 → 14.2
Time: 4.1s
Precision: binary64
\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
\[\begin{array}{l} \mathbf{if}\;re \leq -5.935554961352542 \cdot 10^{+109}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -3.990259720622686 \cdot 10^{-110}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{elif}\;re \leq 3.229385886304382 \cdot 10^{-86}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 1.1214730224820804 \cdot 10^{-28} \lor \neg \left(re \leq 1.5600440620400777 \cdot 10^{+41}\right):\\ \;\;\;\;0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot e^{\left(\log re + \log re\right) - \log im}\right) - re\right)}\\ \end{array}\]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq -5.935554961352542 \cdot 10^{+109}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq -3.990259720622686 \cdot 10^{-110}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\

\mathbf{elif}\;re \leq 3.229385886304382 \cdot 10^{-86}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{elif}\;re \leq 1.1214730224820804 \cdot 10^{-28} \lor \neg \left(re \leq 1.5600440620400777 \cdot 10^{+41}\right):\\
\;\;\;\;0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot e^{\left(\log re + \log re\right) - \log im}\right) - re\right)}\\

\end{array}
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (if (<= re -5.935554961352542e+109)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re -3.990259720622686e-110)
     (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))
     (if (<= re 3.229385886304382e-86)
       (* 0.5 (sqrt (* 2.0 im)))
       (if (or (<= re 1.1214730224820804e-28)
               (not (<= re 1.5600440620400777e+41)))
         (* 0.5 (* (* (sqrt 0.5) (* im (sqrt 2.0))) (sqrt (/ 1.0 re))))
         (*
          0.5
          (sqrt
           (*
            2.0
            (-
             (+ im (* 0.5 (exp (- (+ (log re) (log re)) (log im)))))
             re)))))))))
double code(double re, double im) {
	return 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
}
double code(double re, double im) {
	double tmp;
	if (re <= -5.935554961352542e+109) {
		tmp = 0.5 * sqrt(2.0 * (re * -2.0));
	} else if (re <= -3.990259720622686e-110) {
		tmp = 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
	} else if (re <= 3.229385886304382e-86) {
		tmp = 0.5 * sqrt(2.0 * im);
	} else if ((re <= 1.1214730224820804e-28) || !(re <= 1.5600440620400777e+41)) {
		tmp = 0.5 * ((sqrt(0.5) * (im * sqrt(2.0))) * sqrt(1.0 / re));
	} else {
		tmp = 0.5 * sqrt(2.0 * ((im + (0.5 * exp((log(re) + log(re)) - log(im)))) - re));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 5 regimes
  2. if re < -5.93555496135254183e109

    1. Initial program 53.2

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-2 \cdot re\right)}}\]
    3. Simplified9.0

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

    if -5.93555496135254183e109 < re < -3.99025972062268588e-110

    1. Initial program 16.3

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

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\right)}^{1}}\]

    if -3.99025972062268588e-110 < re < 3.22938588630438209e-86

    1. Initial program 28.8

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(0.5 \cdot \frac{{re}^{2}}{im} + im\right)} - re\right)}\]
    3. Simplified10.6

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)} - re\right)}\]
    4. Taylor expanded around 0 11.2

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)}\]
    5. Simplified11.2

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)}\]
    6. Using strategy rm
    7. Applied sqrt-unprod_binary6410.9

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt{2 \cdot im}}\]
    8. Simplified10.9

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}}\]

    if 3.22938588630438209e-86 < re < 1.12147302248208e-28 or 1.5600440620400777e41 < re

    1. Initial program 55.7

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \left(im \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)}\]

    if 1.12147302248208e-28 < re < 1.5600440620400777e41

    1. Initial program 45.3

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(0.5 \cdot \frac{{re}^{2}}{im} + im\right)} - re\right)}\]
    3. Simplified30.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)} - re\right)}\]
    4. Using strategy rm
    5. Applied add-exp-log_binary6430.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \frac{re \cdot re}{\color{blue}{e^{\log im}}}\right) - re\right)}\]
    6. Applied add-exp-log_binary6430.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \frac{re \cdot \color{blue}{e^{\log re}}}{e^{\log im}}\right) - re\right)}\]
    7. Applied add-exp-log_binary6430.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \frac{\color{blue}{e^{\log re}} \cdot e^{\log re}}{e^{\log im}}\right) - re\right)}\]
    8. Applied prod-exp_binary6430.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \frac{\color{blue}{e^{\log re + \log re}}}{e^{\log im}}\right) - re\right)}\]
    9. Applied div-exp_binary6430.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \color{blue}{e^{\left(\log re + \log re\right) - \log im}}\right) - re\right)}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification14.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5.935554961352542 \cdot 10^{+109}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -3.990259720622686 \cdot 10^{-110}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{elif}\;re \leq 3.229385886304382 \cdot 10^{-86}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 1.1214730224820804 \cdot 10^{-28} \lor \neg \left(re \leq 1.5600440620400777 \cdot 10^{+41}\right):\\ \;\;\;\;0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot e^{\left(\log re + \log re\right) - \log im}\right) - re\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2021131 
(FPCore (re im)
  :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
  :precision binary64
  :pre (> im 0.0)
  (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))