Average Error: 38.4 → 13.5
Time: 6.0s
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.1320242768716358 \cdot 10^{+145}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -1.4186350356885884 \cdot 10^{-103}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{im \cdot im + re \cdot re} - re\right)}\\ \mathbf{elif}\;re \leq -5.178839982241449 \cdot 10^{-111}:\\ \;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\ \mathbf{elif}\;re \leq -1.2571986496294196 \cdot 10^{-136}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{im \cdot im + re \cdot re} - re\right)}\\ \mathbf{elif}\;re \leq 24805927178.950237:\\ \;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\ \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.1320242768716358 \cdot 10^{+145}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

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

\mathbf{elif}\;re \leq -5.178839982241449 \cdot 10^{-111}:\\
\;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\

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

\mathbf{elif}\;re \leq 24805927178.950237:\\
\;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\

\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.1320242768716358e+145)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re -1.4186350356885884e-103)
     (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* im im) (* re re))) re))))
     (if (<= re -5.178839982241449e-111)
       (* 0.5 (pow (* 2.0 im) 0.5))
       (if (<= re -1.2571986496294196e-136)
         (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* im im) (* re re))) re))))
         (if (<= re 24805927178.950237)
           (* 0.5 (pow (* 2.0 im) 0.5))
           (* 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.1320242768716358e+145) {
		tmp = 0.5 * sqrt(2.0 * (re * -2.0));
	} else if (re <= -1.4186350356885884e-103) {
		tmp = 0.5 * sqrt(2.0 * (sqrt((im * im) + (re * re)) - re));
	} else if (re <= -5.178839982241449e-111) {
		tmp = 0.5 * pow((2.0 * im), 0.5);
	} else if (re <= -1.2571986496294196e-136) {
		tmp = 0.5 * sqrt(2.0 * (sqrt((im * im) + (re * re)) - re));
	} else if (re <= 24805927178.950237) {
		tmp = 0.5 * pow((2.0 * im), 0.5);
	} 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.1320242768716358e145

    1. Initial program 61.9

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

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

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

    if -2.1320242768716358e145 < re < -1.418635035688588e-103 or -5.178839982241449e-111 < re < -1.25719864962941956e-136

    1. Initial program 15.4

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

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

    if -1.418635035688588e-103 < re < -5.178839982241449e-111 or -1.25719864962941956e-136 < re < 24805927178.9502373

    1. Initial program 31.8

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)}\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt_binary6413.5

      \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2}}\right)} \cdot \sqrt{im}\right)\]
    8. Applied associate-*l*_binary6413.4

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{im}\right)\right)}\]
    9. Simplified13.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{\sqrt{2}}\right)}\right)\]
    10. Using strategy rm
    11. Applied pow1_binary6413.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{im} \cdot \sqrt{\color{blue}{{\left(\sqrt{2}\right)}^{1}}}\right)\right)\]
    12. Applied sqrt-pow1_binary6413.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{im} \cdot \color{blue}{{\left(\sqrt{2}\right)}^{\left(\frac{1}{2}\right)}}\right)\right)\]
    13. Applied pow1_binary6413.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\color{blue}{{im}^{1}}} \cdot {\left(\sqrt{2}\right)}^{\left(\frac{1}{2}\right)}\right)\right)\]
    14. Applied sqrt-pow1_binary6413.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\color{blue}{{im}^{\left(\frac{1}{2}\right)}} \cdot {\left(\sqrt{2}\right)}^{\left(\frac{1}{2}\right)}\right)\right)\]
    15. Applied pow-prod-down_binary6413.3

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \color{blue}{{\left(im \cdot \sqrt{2}\right)}^{\left(\frac{1}{2}\right)}}\right)\]
    16. Applied pow1_binary6413.3

      \[\leadsto 0.5 \cdot \left(\sqrt{\color{blue}{{\left(\sqrt{2}\right)}^{1}}} \cdot {\left(im \cdot \sqrt{2}\right)}^{\left(\frac{1}{2}\right)}\right)\]
    17. Applied sqrt-pow1_binary6413.3

      \[\leadsto 0.5 \cdot \left(\color{blue}{{\left(\sqrt{2}\right)}^{\left(\frac{1}{2}\right)}} \cdot {\left(im \cdot \sqrt{2}\right)}^{\left(\frac{1}{2}\right)}\right)\]
    18. Applied pow-prod-down_binary6413.5

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt{2} \cdot \left(im \cdot \sqrt{2}\right)\right)}^{\left(\frac{1}{2}\right)}}\]
    19. Simplified13.1

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

    if 24805927178.9502373 < re

    1. Initial program 57.3

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

      \[\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 simplification13.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.1320242768716358 \cdot 10^{+145}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -1.4186350356885884 \cdot 10^{-103}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{im \cdot im + re \cdot re} - re\right)}\\ \mathbf{elif}\;re \leq -5.178839982241449 \cdot 10^{-111}:\\ \;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\ \mathbf{elif}\;re \leq -1.2571986496294196 \cdot 10^{-136}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{im \cdot im + re \cdot re} - re\right)}\\ \mathbf{elif}\;re \leq 24805927178.950237:\\ \;\;\;\;0.5 \cdot {\left(2 \cdot im\right)}^{0.5}\\ \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}\]

Alternatives

Reproduce

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