Average Error: 37.3 → 8.3
Time: 4.4s
Precision: binary64
\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\begin{array}{l} t_0 := \sqrt{\sqrt{2}}\\ \mathbf{if}\;re \leq 2.1698433257847657 \cdot 10^{-35}:\\ \;\;\;\;\begin{array}{l} t_1 := \sqrt{t_0}\\ 0.5 \cdot \left(t_0 \cdot \left(t_1 \cdot \left(t_1 \cdot \sqrt{\mathsf{hypot}\left(re, im\right) - re}\right)\right)\right) \end{array}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_2 := \left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\\ \mathbf{if}\;re \leq 1.8652271911593446 \cdot 10^{+22}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot t_2\right)\\ \mathbf{elif}\;re \leq 6.98244041967603 \cdot 10^{+68}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - \sqrt[3]{re} \cdot \left(\sqrt[3]{re} \cdot \sqrt[3]{re}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(t_0 \cdot \left(t_0 \cdot t_2\right)\right)\\ \end{array}\\ \end{array} \]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
t_0 := \sqrt{\sqrt{2}}\\
\mathbf{if}\;re \leq 2.1698433257847657 \cdot 10^{-35}:\\
\;\;\;\;\begin{array}{l}
t_1 := \sqrt{t_0}\\
0.5 \cdot \left(t_0 \cdot \left(t_1 \cdot \left(t_1 \cdot \sqrt{\mathsf{hypot}\left(re, im\right) - re}\right)\right)\right)
\end{array}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_2 := \left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\\
\mathbf{if}\;re \leq 1.8652271911593446 \cdot 10^{+22}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot t_2\right)\\

\mathbf{elif}\;re \leq 6.98244041967603 \cdot 10^{+68}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - \sqrt[3]{re} \cdot \left(\sqrt[3]{re} \cdot \sqrt[3]{re}\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(t_0 \cdot \left(t_0 \cdot t_2\right)\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
 (let* ((t_0 (sqrt (sqrt 2.0))))
   (if (<= re 2.1698433257847657e-35)
     (let* ((t_1 (sqrt t_0)))
       (* 0.5 (* t_0 (* t_1 (* t_1 (sqrt (- (hypot re im) re)))))))
     (let* ((t_2 (* (* im (sqrt 0.5)) (sqrt (/ 1.0 re)))))
       (if (<= re 1.8652271911593446e+22)
         (* 0.5 (* (sqrt 2.0) t_2))
         (if (<= re 6.98244041967603e+68)
           (*
            0.5
            (sqrt
             (* 2.0 (- (hypot re im) (* (cbrt re) (* (cbrt re) (cbrt re)))))))
           (* 0.5 (* t_0 (* t_0 t_2)))))))))
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 t_0 = sqrt(sqrt(2.0));
	double tmp;
	if (re <= 2.1698433257847657e-35) {
		double t_1_1 = sqrt(t_0);
		tmp = 0.5 * (t_0 * (t_1_1 * (t_1_1 * sqrt(hypot(re, im) - re))));
	} else {
		double t_2 = (im * sqrt(0.5)) * sqrt(1.0 / re);
		double tmp_2;
		if (re <= 1.8652271911593446e+22) {
			tmp_2 = 0.5 * (sqrt(2.0) * t_2);
		} else if (re <= 6.98244041967603e+68) {
			tmp_2 = 0.5 * sqrt(2.0 * (hypot(re, im) - (cbrt(re) * (cbrt(re) * cbrt(re)))));
		} else {
			tmp_2 = 0.5 * (t_0 * (t_0 * t_2));
		}
		tmp = tmp_2;
	}
	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 < 2.16984332578476565e-35

    1. Initial program 30.0

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\mathsf{hypot}\left(re, im\right) - re}\right)\right)} \]
    6. Applied add-sqr-sqrt_binary644.5

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

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

    if 2.16984332578476565e-35 < re < 1.86522719115934465e22

    1. Initial program 46.8

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

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

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

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

    if 1.86522719115934465e22 < re < 6.98244041967603038e68

    1. Initial program 46.6

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

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

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

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

    if 6.98244041967603038e68 < re

    1. Initial program 58.4

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 2.1698433257847657 \cdot 10^{-35}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{\sqrt{2}}} \cdot \left(\sqrt{\sqrt{\sqrt{2}}} \cdot \sqrt{\mathsf{hypot}\left(re, im\right) - re}\right)\right)\right)\\ \mathbf{elif}\;re \leq 1.8652271911593446 \cdot 10^{+22}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)\right)\\ \mathbf{elif}\;re \leq 6.98244041967603 \cdot 10^{+68}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - \sqrt[3]{re} \cdot \left(\sqrt[3]{re} \cdot \sqrt[3]{re}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)\right)\right)\\ \end{array} \]

Reproduce

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