Average Error: 38.5 → 17.9
Time: 5.1s
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.0699785804869837 \cdot 10^{+71}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -8.505474022050854 \cdot 10^{-135}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{elif}\;re \leq 5.24919610962508 \cdot 10^{+23}:\\ \;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\ \mathbf{elif}\;re \leq 2.2785101745041676 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im \cdot im}{re + \sqrt{re \cdot re + im \cdot im}}}\\ \mathbf{elif}\;re \leq 2.4827971193717914 \cdot 10^{+172}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \frac{re}{\frac{im}{re}}\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{im \cdot im}{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 -1.0699785804869837 \cdot 10^{+71}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

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

\mathbf{elif}\;re \leq 5.24919610962508 \cdot 10^{+23}:\\
\;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\

\mathbf{elif}\;re \leq 2.2785101745041676 \cdot 10^{+152}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im \cdot im}{re + \sqrt{re \cdot re + im \cdot im}}}\\

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{im \cdot im}{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 -1.0699785804869837e+71)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re -8.505474022050854e-135)
     (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))
     (if (<= re 5.24919610962508e+23)
       (* 0.5 (pow (* 2.0 im) 0.5))
       (if (<= re 2.2785101745041676e+152)
         (*
          0.5
          (sqrt (* 2.0 (/ (* im im) (+ re (sqrt (+ (* re re) (* im im))))))))
         (if (<= re 2.4827971193717914e+172)
           (* 0.5 (sqrt (* 2.0 (- (+ im (* 0.5 (/ re (/ im re)))) re))))
           (* 0.5 (sqrt (* 2.0 (* 0.5 (/ (* im 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 (re <= -1.0699785804869837e+71) {
		tmp = 0.5 * sqrt(2.0 * (re * -2.0));
	} else if (re <= -8.505474022050854e-135) {
		tmp = 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
	} else if (re <= 5.24919610962508e+23) {
		tmp = 0.5 * pow((2.0 * im), 0.5);
	} else if (re <= 2.2785101745041676e+152) {
		tmp = 0.5 * sqrt(2.0 * ((im * im) / (re + sqrt((re * re) + (im * im)))));
	} else if (re <= 2.4827971193717914e+172) {
		tmp = 0.5 * sqrt(2.0 * ((im + (0.5 * (re / (im / re)))) - re));
	} else {
		tmp = 0.5 * sqrt(2.0 * (0.5 * ((im * 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 6 regimes
  2. if re < -1.0699785804869837e71

    1. Initial program 48.1

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

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

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

    if -1.0699785804869837e71 < re < -8.5054740220508541e-135

    1. Initial program 15.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_binary6415.8

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - \color{blue}{1 \cdot re}\right)}\]
    4. Applied cancel-sign-sub-inv_binary6415.8

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

    if -8.5054740220508541e-135 < re < 5.24919610962508002e23

    1. Initial program 32.9

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(0.5 \cdot \frac{{re}^{2}}{im} + im\right)} - re\right)}\]
    3. Simplified13.7

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)} - re\right)}\]
    4. Taylor expanded around 0 14.1

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)}\]
    5. Simplified14.1

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)}\]
    6. Using strategy rm
    7. Applied pow1/2_binary6414.1

      \[\leadsto 0.5 \cdot \left(\sqrt{2} \cdot \color{blue}{{im}^{0.5}}\right)\]
    8. Applied pow1/2_binary6414.1

      \[\leadsto 0.5 \cdot \left(\color{blue}{{2}^{0.5}} \cdot {im}^{0.5}\right)\]
    9. Applied pow-prod-down_binary6413.8

      \[\leadsto 0.5 \cdot \color{blue}{{\left(2 \cdot im\right)}^{0.5}}\]
    10. Simplified13.8

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

    if 5.24919610962508002e23 < re < 2.27851017450416761e152

    1. Initial program 51.3

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Using strategy rm
    3. Applied flip--_binary6451.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re}{\sqrt{re \cdot re + im \cdot im} + re}}}\]
    4. Simplified30.8

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{im \cdot im}}{\sqrt{re \cdot re + im \cdot im} + re}}\]
    5. Simplified30.8

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{im \cdot im}{\color{blue}{re + \sqrt{re \cdot re + im \cdot im}}}}\]

    if 2.27851017450416761e152 < re < 2.4827971193717914e172

    1. Initial program 62.5

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)} - re\right)}\]
    4. Using strategy rm
    5. Applied associate-/l*_binary6445.6

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

    if 2.4827971193717914e172 < re

    1. Initial program 64.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.0699785804869837 \cdot 10^{+71}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -8.505474022050854 \cdot 10^{-135}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\ \mathbf{elif}\;re \leq 5.24919610962508 \cdot 10^{+23}:\\ \;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\ \mathbf{elif}\;re \leq 2.2785101745041676 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im \cdot im}{re + \sqrt{re \cdot re + im \cdot im}}}\\ \mathbf{elif}\;re \leq 2.4827971193717914 \cdot 10^{+172}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(im + 0.5 \cdot \frac{re}{\frac{im}{re}}\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{im \cdot im}{re}\right)}\\ \end{array}\]

Reproduce

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