Average Error: 38.6 → 18.6
Time: 10.3s
Precision: 64
$0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}$
$\begin{array}{l} \mathbf{if}\;re \le -5.369633668174498803459642934039108441214 \cdot 10^{137}:\\ \;\;\;\;\frac{\sqrt{\left(im \cdot im\right) \cdot 2}}{\sqrt{-2 \cdot re}} \cdot 0.5\\ \mathbf{elif}\;re \le -3.532234512109790750132422957206188388696 \cdot 10^{-296}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}}\\ \mathbf{elif}\;re \le 4.944794961514355427000939408494111733969 \cdot 10^{128}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot 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 \le -5.369633668174498803459642934039108441214 \cdot 10^{137}:\\
\;\;\;\;\frac{\sqrt{\left(im \cdot im\right) \cdot 2}}{\sqrt{-2 \cdot re}} \cdot 0.5\\

\mathbf{elif}\;re \le -3.532234512109790750132422957206188388696 \cdot 10^{-296}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}}\\

\mathbf{elif}\;re \le 4.944794961514355427000939408494111733969 \cdot 10^{128}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\

\end{array}
double f(double re, double im) {
double r83732 = 0.5;
double r83733 = 2.0;
double r83734 = re;
double r83735 = r83734 * r83734;
double r83736 = im;
double r83737 = r83736 * r83736;
double r83738 = r83735 + r83737;
double r83739 = sqrt(r83738);
double r83740 = r83739 + r83734;
double r83741 = r83733 * r83740;
double r83742 = sqrt(r83741);
double r83743 = r83732 * r83742;
return r83743;
}


double f(double re, double im) {
double r83744 = re;
double r83745 = -5.369633668174499e+137;
bool r83746 = r83744 <= r83745;
double r83747 = im;
double r83748 = r83747 * r83747;
double r83749 = 2.0;
double r83750 = r83748 * r83749;
double r83751 = sqrt(r83750);
double r83752 = -2.0;
double r83753 = r83752 * r83744;
double r83754 = sqrt(r83753);
double r83755 = r83751 / r83754;
double r83756 = 0.5;
double r83757 = r83755 * r83756;
double r83758 = -3.532234512109791e-296;
bool r83759 = r83744 <= r83758;
double r83760 = sqrt(r83749);
double r83761 = r83744 * r83744;
double r83762 = r83761 + r83748;
double r83763 = sqrt(r83762);
double r83764 = r83763 - r83744;
double r83765 = sqrt(r83764);
double r83766 = fabs(r83747);
double r83767 = r83765 / r83766;
double r83768 = r83760 / r83767;
double r83769 = r83756 * r83768;
double r83770 = 4.9447949615143554e+128;
bool r83771 = r83744 <= r83770;
double r83772 = r83763 + r83744;
double r83773 = r83749 * r83772;
double r83774 = sqrt(r83773);
double r83775 = r83756 * r83774;
double r83776 = 2.0;
double r83777 = r83776 * r83744;
double r83778 = r83749 * r83777;
double r83779 = sqrt(r83778);
double r83780 = r83756 * r83779;
double r83781 = r83771 ? r83775 : r83780;
double r83782 = r83759 ? r83769 : r83781;
double r83783 = r83746 ? r83757 : r83782;
return r83783;
}



# Try it out

Results

 In Out
Enter valid numbers for all inputs

# Derivation

1. Split input into 4 regimes
2. ## if re < -5.369633668174499e+137

1. Initial program 62.9

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

$\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. Applied associate-*r/62.9

$\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}{\sqrt{re \cdot re + im \cdot im} - re}}}$
5. Applied sqrt-div62.9

$\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}}$
6. Simplified47.1

$\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{2 \cdot \left(im \cdot im + 0\right)}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}$
7. Taylor expanded around -inf 21.1

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

## if -5.369633668174499e+137 < re < -3.532234512109791e-296

1. Initial program 39.7

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

$\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. Applied associate-*r/39.4

$\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}{\sqrt{re \cdot re + im \cdot im} - re}}}$
5. Applied sqrt-div39.6

$\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}}$
6. Simplified29.2

$\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{2 \cdot \left(im \cdot im + 0\right)}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}$
7. Using strategy rm
8. Applied sqrt-prod29.2

$\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{2} \cdot \sqrt{im \cdot im + 0}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}$
9. Applied associate-/l*29.2

$\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\sqrt{im \cdot im + 0}}}}$
10. Simplified19.7

$\leadsto 0.5 \cdot \frac{\sqrt{2}}{\color{blue}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}}}$

## if -3.532234512109791e-296 < re < 4.9447949615143554e+128

1. Initial program 20.8

$0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}$

## if 4.9447949615143554e+128 < re

1. Initial program 57.2

$0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}$
2. Taylor expanded around inf 8.3

$\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(2 \cdot re\right)}}$
3. Recombined 4 regimes into one program.
4. Final simplification18.6

$\leadsto \begin{array}{l} \mathbf{if}\;re \le -5.369633668174498803459642934039108441214 \cdot 10^{137}:\\ \;\;\;\;\frac{\sqrt{\left(im \cdot im\right) \cdot 2}}{\sqrt{-2 \cdot re}} \cdot 0.5\\ \mathbf{elif}\;re \le -3.532234512109790750132422957206188388696 \cdot 10^{-296}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}}\\ \mathbf{elif}\;re \le 4.944794961514355427000939408494111733969 \cdot 10^{128}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \end{array}$

# Reproduce

herbie shell --seed 1
(FPCore (re im)
:name "Complex square root"
:precision binary64
(* 0.5 (sqrt (* 2 (+ (sqrt (+ (* re re) (* im im))) re)))))