Average Error: 38.3 → 8.5
Time: 3.4s
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 1.6248919274963702 \cdot 10^{-114}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\ \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 1.6248919274963702 \cdot 10^{-114}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\


\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 1.6248919274963702e-114)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (/ (* 0.5 im) (sqrt 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 <= 1.6248919274963702e-114) {
		tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
	} else {
		tmp = (0.5 * im) / sqrt(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 < 1.6248919274963702e-114

    1. Initial program 30.8

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

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

    if 1.6248919274963702e-114 < re

    1. Initial program 52.4

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    4. Applied associate-*r/_binary6438.9

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    5. Applied associate-*r/_binary6438.8

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2 \cdot \left(0.5 \cdot {im}^{2}\right)}{re}}} \]
    6. Applied sqrt-div_binary6433.3

      \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2 \cdot \left(0.5 \cdot {im}^{2}\right)}}{\sqrt{re}}} \]
    7. Applied associate-*r/_binary6433.3

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

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

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

Reproduce

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