Average Error: 38.5 → 6.1
Time: 3.8s
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}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\ \;\;\;\;\begin{array}{l} t_0 := \sqrt[3]{\sqrt{2}}\\ 0.5 \cdot \left(\left(t_0 \cdot t_0\right) \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \left(t_0 \cdot \sqrt{\frac{1}{re}}\right)\right)\right) \end{array}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, 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}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\begin{array}{l}
t_0 := \sqrt[3]{\sqrt{2}}\\
0.5 \cdot \left(\left(t_0 \cdot t_0\right) \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \left(t_0 \cdot \sqrt{\frac{1}{re}}\right)\right)\right)
\end{array}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, 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 (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0)
   (let* ((t_0 (cbrt (sqrt 2.0))))
     (* 0.5 (* (* t_0 t_0) (* (* im (sqrt 0.5)) (* t_0 (sqrt (/ 1.0 re)))))))
   (* 0.5 (sqrt (* 2.0 (- (hypot re 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 ((sqrt((re * re) + (im * im)) - re) <= 0.0) {
		double t_0_1 = cbrt(sqrt(2.0));
		tmp = 0.5 * ((t_0_1 * t_0_1) * ((im * sqrt(0.5)) * (t_0_1 * sqrt(1.0 / re))));
	} else {
		tmp = 0.5 * sqrt(2.0 * (hypot(re, 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 2 regimes
  2. if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 58.5

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{\mathsf{hypot}\left(re, im\right) - re}\right)} \]
    4. Applied add-cube-cbrt_binary6452.4

      \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \sqrt[3]{\sqrt{2}}\right)} \cdot \sqrt{\mathsf{hypot}\left(re, im\right) - re}\right) \]
    5. Applied associate-*l*_binary6452.4

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

      \[\leadsto 0.5 \cdot \left(\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot im\right) \cdot \left({\left(\sqrt{2}\right)}^{0.3333333333333333} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right) \]
    7. Simplified5.9

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

    if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

    1. Initial program 34.8

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

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

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

Reproduce

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