Average Error: 38.1 → 16.2
Time: 5.6s
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 -2.8433089322704343 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -2.1472970862594244 \cdot 10^{+52}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -7.774423883754869 \cdot 10^{-47}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{elif}\;re \leq 2.729025710975122 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - 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}
\mathbf{if}\;re \leq -2.8433089322704343 \cdot 10^{+152}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq -2.1472970862594244 \cdot 10^{+52}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\

\mathbf{elif}\;re \leq -7.774423883754869 \cdot 10^{-47}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\

\mathbf{elif}\;re \leq 2.729025710975122 \cdot 10^{-96}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - 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
 (if (<= re -2.8433089322704343e+152)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re -2.1472970862594244e+52)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (if (<= re -7.774423883754869e-47)
       (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))
       (if (<= re 2.729025710975122e-96)
         (* 0.5 (sqrt (* 2.0 (- 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 tmp;
	if (re <= -2.8433089322704343e+152) {
		tmp = 0.5 * sqrt(2.0 * (re * -2.0));
	} else if (re <= -2.1472970862594244e+52) {
		tmp = 0.5 * sqrt(2.0 * (im - re));
	} else if (re <= -7.774423883754869e-47) {
		tmp = 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
	} else if (re <= 2.729025710975122e-96) {
		tmp = 0.5 * sqrt(2.0 * (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 4 regimes
  2. if re < -2.84330893227043434e152

    1. Initial program 62.9

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Taylor expanded around -inf 8.0

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-2 \cdot re\right)}}\]
    3. Simplified8.0

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

    if -2.84330893227043434e152 < re < -2.14729708625942438e52 or -7.77442388375486904e-47 < re < 2.72902571097512218e-96

    1. Initial program 24.3

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Taylor expanded around 0 15.8

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

    if -2.14729708625942438e52 < re < -7.77442388375486904e-47

    1. Initial program 16.8

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity_binary6416.8

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

    if 2.72902571097512218e-96 < re

    1. Initial program 53.1

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Taylor expanded around 0 19.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.8433089322704343 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -2.1472970862594244 \cdot 10^{+52}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -7.774423883754869 \cdot 10^{-47}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{elif}\;re \leq 2.729025710975122 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - 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 2021175 
(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)))))