Average Error: 38.9 → 15.6
Time: 5.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 -4.588012152373333 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.754922286482946 \cdot 10^{-180}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.3178163098264688 \cdot 10^{-168} \lor \neg \left(re \leq 1.8325515919274365 \cdot 10^{-60}\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 im}\\ \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 -4.588012152373333 \cdot 10^{+34}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

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

\mathbf{elif}\;re \leq 1.3178163098264688 \cdot 10^{-168} \lor \neg \left(re \leq 1.8325515919274365 \cdot 10^{-60}\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 im}\\

\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 -4.588012152373333e+34)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 1.754922286482946e-180)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (if (or (<= re 1.3178163098264688e-168)
             (not (<= re 1.8325515919274365e-60)))
       (* 0.5 (* (* (sqrt 0.5) (* im (sqrt 2.0))) (sqrt (/ 1.0 re))))
       (* 0.5 (sqrt (* 2.0 im)))))))
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 <= -4.588012152373333e+34) {
		tmp = 0.5 * sqrt(2.0 * (re * -2.0));
	} else if (re <= 1.754922286482946e-180) {
		tmp = 0.5 * sqrt(2.0 * (im - re));
	} else if ((re <= 1.3178163098264688e-168) || !(re <= 1.8325515919274365e-60)) {
		tmp = 0.5 * ((sqrt(0.5) * (im * sqrt(2.0))) * sqrt(1.0 / re));
	} else {
		tmp = 0.5 * sqrt(2.0 * im);
	}
	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 4 regimes
  2. if re < -4.58801215237333315e34

    1. Initial program 43.4

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

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

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

    if -4.58801215237333315e34 < re < 1.754922286482946e-180

    1. Initial program 25.0

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

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

    if 1.754922286482946e-180 < re < 1.3178163098264688e-168 or 1.83255159192743653e-60 < re

    1. Initial program 53.6

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

      \[\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.3178163098264688e-168 < re < 1.83255159192743653e-60

    1. Initial program 35.7

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{im}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification15.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.588012152373333 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.754922286482946 \cdot 10^{-180}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.3178163098264688 \cdot 10^{-168} \lor \neg \left(re \leq 1.8325515919274365 \cdot 10^{-60}\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 im}\\ \end{array}\]

Reproduce

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