Average Error: 38.2 → 6.4
Time: 4.9s
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{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{if}\;t_0 \leq 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(im \cdot {\left(\frac{0.25}{re \cdot re}\right)}^{0.25}\right)\right)\\ \mathbf{elif}\;t_0 \leq \infty:\\ \;\;\;\;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 \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
t_0 := \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\
\mathbf{if}\;t_0 \leq 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(im \cdot {\left(\frac{0.25}{re \cdot re}\right)}^{0.25}\right)\right)\\

\mathbf{elif}\;t_0 \leq \infty:\\
\;\;\;\;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 \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{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
 (let* ((t_0 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
   (if (<= t_0 0.0)
     (* 0.5 (* (sqrt 2.0) (* im (pow (/ 0.25 (* re re)) 0.25))))
     (if (<= t_0 INFINITY)
       (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
       (* 0.5 (* (* (sqrt 0.5) (* im (sqrt 2.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 t_0 = sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
	double tmp;
	if (t_0 <= 0.0) {
		tmp = 0.5 * (sqrt(2.0) * (im * pow((0.25 / (re * re)), 0.25)));
	} else if (t_0 <= ((double) INFINITY)) {
		tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
	} else {
		tmp = 0.5 * ((sqrt(0.5) * (im * sqrt(2.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 3 regimes
  2. if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 57.9

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot e^{0.25 \cdot \left(4 \cdot \log im + \log \left(\frac{0.25}{{re}^{2}}\right)\right)}\right)} \]
    7. Simplified0.5

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

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

    1. Initial program 34.4

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

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

    if +inf.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 64.0

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

      \[\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 28.2

      \[\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)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification6.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(im \cdot {\left(\frac{0.25}{re \cdot re}\right)}^{0.25}\right)\right)\\ \mathbf{elif}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq \infty:\\ \;\;\;\;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 \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \]

Reproduce

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