Average Error: 38.6 → 18.6
Time: 12.4s
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}:\\ \;\;\;\;\frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}} \cdot 0.5\\ \mathbf{elif}\;re \le 4.944794961514355427000939408494111733969 \cdot 10^{128}:\\ \;\;\;\;\sqrt{\left(\sqrt{re \cdot re + im \cdot im} + re\right) \cdot 2} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(re + re\right) \cdot 2}\\ \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}:\\
\;\;\;\;\frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}} \cdot 0.5\\

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

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

\end{array}
double f(double re, double im) {
double r7871055 = 0.5;
double r7871056 = 2.0;
double r7871057 = re;
double r7871058 = r7871057 * r7871057;
double r7871059 = im;
double r7871060 = r7871059 * r7871059;
double r7871061 = r7871058 + r7871060;
double r7871062 = sqrt(r7871061);
double r7871063 = r7871062 + r7871057;
double r7871064 = r7871056 * r7871063;
double r7871065 = sqrt(r7871064);
double r7871066 = r7871055 * r7871065;
return r7871066;
}


double f(double re, double im) {
double r7871067 = re;
double r7871068 = -5.369633668174499e+137;
bool r7871069 = r7871067 <= r7871068;
double r7871070 = im;
double r7871071 = r7871070 * r7871070;
double r7871072 = 2.0;
double r7871073 = r7871071 * r7871072;
double r7871074 = sqrt(r7871073);
double r7871075 = -2.0;
double r7871076 = r7871075 * r7871067;
double r7871077 = sqrt(r7871076);
double r7871078 = r7871074 / r7871077;
double r7871079 = 0.5;
double r7871080 = r7871078 * r7871079;
double r7871081 = -3.532234512109791e-296;
bool r7871082 = r7871067 <= r7871081;
double r7871083 = sqrt(r7871072);
double r7871084 = r7871067 * r7871067;
double r7871085 = r7871084 + r7871071;
double r7871086 = sqrt(r7871085);
double r7871087 = r7871086 - r7871067;
double r7871088 = sqrt(r7871087);
double r7871089 = fabs(r7871070);
double r7871090 = r7871088 / r7871089;
double r7871091 = r7871083 / r7871090;
double r7871092 = r7871091 * r7871079;
double r7871093 = 4.9447949615143554e+128;
bool r7871094 = r7871067 <= r7871093;
double r7871095 = r7871086 + r7871067;
double r7871096 = r7871095 * r7871072;
double r7871097 = sqrt(r7871096);
double r7871098 = r7871097 * r7871079;
double r7871099 = r7871067 + r7871067;
double r7871100 = r7871099 * r7871072;
double r7871101 = sqrt(r7871100);
double r7871102 = r7871079 * r7871101;
double r7871103 = r7871094 ? r7871098 : r7871102;
double r7871104 = r7871082 ? r7871092 : r7871103;
double r7871105 = r7871069 ? r7871080 : r7871104;
return r7871105;
}



# 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{im \cdot im + re \cdot re} - 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 \left(\color{blue}{re} + 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}:\\ \;\;\;\;\frac{\sqrt{2}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\left|im\right|}} \cdot 0.5\\ \mathbf{elif}\;re \le 4.944794961514355427000939408494111733969 \cdot 10^{128}:\\ \;\;\;\;\sqrt{\left(\sqrt{re \cdot re + im \cdot im} + re\right) \cdot 2} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(re + re\right) \cdot 2}\\ \end{array}$

# Reproduce

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