Average Error: 38.5 → 12.1
Time: 5.1s
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 1755103.27134319046:\\ \;\;\;\;0.5 \cdot \left(\left(\left|\sqrt[3]{\sqrt{2}}\right| \cdot \sqrt{\sqrt{2}}\right) \cdot \sqrt{\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right) + \sqrt[3]{\sqrt{2}} \cdot \left(-re\right)}\right)\\ \mathbf{elif}\;re \le 3.9030929001675639 \cdot 10^{52}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{re + \mathsf{hypot}\left(re, im\right)}}\\ \mathbf{elif}\;re \le 5.6852916263112347 \cdot 10^{140}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{re + \mathsf{hypot}\left(re, im\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 1755103.27134319046:\\
\;\;\;\;0.5 \cdot \left(\left(\left|\sqrt[3]{\sqrt{2}}\right| \cdot \sqrt{\sqrt{2}}\right) \cdot \sqrt{\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right) + \sqrt[3]{\sqrt{2}} \cdot \left(-re\right)}\right)\\

\mathbf{elif}\;re \le 3.9030929001675639 \cdot 10^{52}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{re + \mathsf{hypot}\left(re, im\right)}}\\

\mathbf{elif}\;re \le 5.6852916263112347 \cdot 10^{140}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{re + \mathsf{hypot}\left(re, im\right)}}\\

\end{array}
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 temp;
	if ((re <= 1755103.2713431905)) {
		temp = (0.5 * ((fabs(cbrt(sqrt(2.0))) * sqrt(sqrt(2.0))) * sqrt(((cbrt(sqrt(2.0)) * hypot(re, im)) + (cbrt(sqrt(2.0)) * -re)))));
	} else {
		double temp_1;
		if ((re <= 3.903092900167564e+52)) {
			temp_1 = (0.5 * sqrt((2.0 * ((pow(im, 2.0) + 0.0) / (re + hypot(re, im))))));
		} else {
			double temp_2;
			if ((re <= 5.685291626311235e+140)) {
				temp_2 = (0.5 * sqrt((2.0 * (1.0 * (hypot(re, im) - re)))));
			} else {
				temp_2 = (0.5 * sqrt((2.0 * ((pow(im, 2.0) + 0.0) / (re + hypot(re, im))))));
			}
			temp_1 = temp_2;
		}
		temp = temp_1;
	}
	return temp;
}

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 3 regimes
  2. if re < 1755103.2713431905

    1. Initial program 32.4

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - \color{blue}{1 \cdot re}\right)}\]
    4. Applied *-un-lft-identity32.4

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1 \cdot \sqrt{re \cdot re + im \cdot im}} - 1 \cdot re\right)}\]
    5. Applied distribute-lft-out--32.4

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \color{blue}{\left(\mathsf{hypot}\left(re, im\right) - re\right)}\right)}\]
    7. Using strategy rm
    8. Applied add-sqr-sqrt5.8

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \left(1 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}\]
    9. Applied associate-*l*5.6

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\sqrt{2} \cdot \left(\sqrt{2} \cdot \left(1 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)\right)}}\]
    10. Applied sqrt-prod5.4

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2} \cdot \left(1 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}\right)}\]
    11. Using strategy rm
    12. Applied sub-neg5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2} \cdot \left(1 \cdot \color{blue}{\left(\mathsf{hypot}\left(re, im\right) + \left(-re\right)\right)}\right)}\right)\]
    13. Applied distribute-lft-in5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2} \cdot \color{blue}{\left(1 \cdot \mathsf{hypot}\left(re, im\right) + 1 \cdot \left(-re\right)\right)}}\right)\]
    14. Applied distribute-lft-in5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\color{blue}{\sqrt{2} \cdot \left(1 \cdot \mathsf{hypot}\left(re, im\right)\right) + \sqrt{2} \cdot \left(1 \cdot \left(-re\right)\right)}}\right)\]
    15. Simplified5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\color{blue}{\sqrt{2} \cdot \mathsf{hypot}\left(re, im\right)} + \sqrt{2} \cdot \left(1 \cdot \left(-re\right)\right)}\right)\]
    16. Simplified5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2} \cdot \mathsf{hypot}\left(re, im\right) + \color{blue}{\sqrt{2} \cdot \left(-re\right)}}\right)\]
    17. Using strategy rm
    18. Applied add-cube-cbrt5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2} \cdot \mathsf{hypot}\left(re, im\right) + \color{blue}{\left(\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \sqrt[3]{\sqrt{2}}\right)} \cdot \left(-re\right)}\right)\]
    19. Applied associate-*l*5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt{2} \cdot \mathsf{hypot}\left(re, im\right) + \color{blue}{\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \left(\sqrt[3]{\sqrt{2}} \cdot \left(-re\right)\right)}}\right)\]
    20. Applied add-cube-cbrt5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\color{blue}{\left(\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \sqrt[3]{\sqrt{2}}\right)} \cdot \mathsf{hypot}\left(re, im\right) + \left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \left(\sqrt[3]{\sqrt{2}} \cdot \left(-re\right)\right)}\right)\]
    21. Applied associate-*l*5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\color{blue}{\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \left(\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right)\right)} + \left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \left(\sqrt[3]{\sqrt{2}} \cdot \left(-re\right)\right)}\right)\]
    22. Applied distribute-lft-out5.4

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \sqrt{\color{blue}{\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \left(\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right) + \sqrt[3]{\sqrt{2}} \cdot \left(-re\right)\right)}}\right)\]
    23. Applied sqrt-prod5.5

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \color{blue}{\left(\sqrt{\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}} \cdot \sqrt{\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right) + \sqrt[3]{\sqrt{2}} \cdot \left(-re\right)}\right)}\right)\]
    24. Applied associate-*r*5.4

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(\sqrt{\sqrt{2}} \cdot \sqrt{\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}}\right) \cdot \sqrt{\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right) + \sqrt[3]{\sqrt{2}} \cdot \left(-re\right)}\right)}\]
    25. Simplified5.4

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

    if 1755103.2713431905 < re < 3.903092900167564e+52 or 5.685291626311235e+140 < re

    1. Initial program 59.9

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Using strategy rm
    3. Applied flip--59.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. Simplified44.9

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{2} + 0}}{\sqrt{re \cdot re + im \cdot im} + re}}\]
    5. Simplified31.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{\color{blue}{re + \mathsf{hypot}\left(re, im\right)}}}\]

    if 3.903092900167564e+52 < re < 5.685291626311235e+140

    1. Initial program 51.1

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - \color{blue}{1 \cdot re}\right)}\]
    4. Applied *-un-lft-identity51.1

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1 \cdot \sqrt{re \cdot re + im \cdot im}} - 1 \cdot re\right)}\]
    5. Applied distribute-lft-out--51.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le 1755103.27134319046:\\ \;\;\;\;0.5 \cdot \left(\left(\left|\sqrt[3]{\sqrt{2}}\right| \cdot \sqrt{\sqrt{2}}\right) \cdot \sqrt{\sqrt[3]{\sqrt{2}} \cdot \mathsf{hypot}\left(re, im\right) + \sqrt[3]{\sqrt{2}} \cdot \left(-re\right)}\right)\\ \mathbf{elif}\;re \le 3.9030929001675639 \cdot 10^{52}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{re + \mathsf{hypot}\left(re, im\right)}}\\ \mathbf{elif}\;re \le 5.6852916263112347 \cdot 10^{140}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(1 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} + 0}{re + \mathsf{hypot}\left(re, im\right)}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020066 +o rules:numerics
(FPCore (re im)
  :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
  :precision binary64
  (* 0.5 (sqrt (* 2 (- (sqrt (+ (* re re) (* im im))) re)))))