Average Error: 38.3 → 7.4
Time: 5.0s
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 0.007479677862419169:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := \sqrt{\sqrt{2}}\\ 0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \left(t_0 \cdot t_0\right)\right)\right) \cdot \sqrt{\frac{1}{re}}\right) \end{array}\\ \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 0.007479677862419169:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := \sqrt{\sqrt{2}}\\
0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \left(t_0 \cdot t_0\right)\right)\right) \cdot \sqrt{\frac{1}{re}}\right)
\end{array}\\


\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 0.007479677862419169)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (let* ((t_0 (sqrt (sqrt 2.0))))
     (* 0.5 (* (* (sqrt 0.5) (* im (* t_0 t_0))) (sqrt (/ 1.0 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 <= 0.007479677862419169) {
		tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
	} else {
		double t_0 = sqrt(sqrt(2.0));
		tmp = 0.5 * ((sqrt(0.5) * (im * (t_0 * t_0))) * sqrt(1.0 / 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 2 regimes
  2. if re < 0.00747967786241916874

    1. Initial program 32.1

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Simplified4.9

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

    if 0.00747967786241916874 < re

    1. Initial program 57.0

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Simplified38.4

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    3. Taylor expanded in im around 0 15.5

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \left(\sqrt{2} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    4. Applied add-sqr-sqrt_binary6415.2

      \[\leadsto 0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(\color{blue}{\left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2}}\right)} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right) \]
    5. Applied associate-*l*_binary6415.2

      \[\leadsto 0.5 \cdot \left(\left(\sqrt{0.5} \cdot \color{blue}{\left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{2}} \cdot im\right)\right)}\right) \cdot \sqrt{\frac{1}{re}}\right) \]
    6. Applied associate-*r*_binary6415.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 0.007479677862419169:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2}}\right)\right)\right) \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \]

Reproduce

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