Average Error: 38.4 → 22.6
Time: 4.5s
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 -1.3380130268941683 \cdot 10^{154}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{2 \cdot {im}^{2}}}{\sqrt{-1 \cdot re - re}}\\ \mathbf{elif}\;re \le 7.2534218396275159 \cdot 10^{-289}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{2} \cdot \left|im\right|}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\\ \mathbf{elif}\;re \le 3.1852294886834028 \cdot 10^{-94}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im}{\frac{im - re}{im}}}\\ \mathbf{elif}\;re \le 3.21633745718991262 \cdot 10^{-26}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{\sqrt{{im}^{2}}}{\frac{\sqrt{re \cdot re + im \cdot im} - re}{\left|im\right|}}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + 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 -1.3380130268941683 \cdot 10^{154}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{2 \cdot {im}^{2}}}{\sqrt{-1 \cdot re - re}}\\

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

\mathbf{elif}\;re \le 3.1852294886834028 \cdot 10^{-94}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im}{\frac{im - re}{im}}}\\

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + re\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 VAR;
	if ((re <= -1.3380130268941683e+154)) {
		VAR = (0.5 * (sqrt((2.0 * pow(im, 2.0))) / sqrt(((-1.0 * re) - re))));
	} else {
		double VAR_1;
		if ((re <= 7.253421839627516e-289)) {
			VAR_1 = (0.5 * ((sqrt(2.0) * fabs(im)) / sqrt((sqrt(((re * re) + (im * im))) - re))));
		} else {
			double VAR_2;
			if ((re <= 3.185229488683403e-94)) {
				VAR_2 = (0.5 * sqrt((2.0 * (im / ((im - re) / im)))));
			} else {
				double VAR_3;
				if ((re <= 3.2163374571899126e-26)) {
					VAR_3 = (0.5 * sqrt((2.0 * (sqrt(pow(im, 2.0)) / ((sqrt(((re * re) + (im * im))) - re) / fabs(im))))));
				} else {
					VAR_3 = (0.5 * sqrt((2.0 * (re + re))));
				}
				VAR_2 = VAR_3;
			}
			VAR_1 = VAR_2;
		}
		VAR = VAR_1;
	}
	return VAR;
}

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.4
Target33.2
Herbie22.6
\[\begin{array}{l} \mathbf{if}\;re \lt 0.0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \end{array}\]

Derivation

  1. Split input into 5 regimes
  2. if re < -1.3380130268941683e+154

    1. Initial program 64.0

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

      \[\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. Simplified50.1

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{2}}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
    5. Using strategy rm
    6. Applied associate-*r/50.1

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2 \cdot {im}^{2}}{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    7. Applied sqrt-div50.1

      \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2 \cdot {im}^{2}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    8. Taylor expanded around -inf 20.8

      \[\leadsto 0.5 \cdot \frac{\sqrt{2 \cdot {im}^{2}}}{\sqrt{\color{blue}{-1 \cdot re} - re}}\]

    if -1.3380130268941683e+154 < re < 7.253421839627516e-289

    1. Initial program 39.1

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

      \[\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.2

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{2}}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
    5. Using strategy rm
    6. Applied associate-*r/30.3

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2 \cdot {im}^{2}}{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    7. Applied sqrt-div29.1

      \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2 \cdot {im}^{2}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    8. Using strategy rm
    9. Applied sqrt-prod29.1

      \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{2} \cdot \sqrt{{im}^{2}}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\]
    10. Simplified19.8

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

    if 7.253421839627516e-289 < re < 3.185229488683403e-94

    1. Initial program 24.9

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

      \[\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. Simplified31.2

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{2}}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
    5. Using strategy rm
    6. Applied unpow231.2

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{im \cdot im}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
    7. Applied associate-/l*31.0

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{im}{\frac{\sqrt{re \cdot re + im \cdot im} - re}{im}}}}\]
    8. Taylor expanded around 0 36.5

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

    if 3.185229488683403e-94 < re < 3.2163374571899126e-26

    1. Initial program 15.7

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

      \[\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. Simplified40.1

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{2}}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
    5. Using strategy rm
    6. Applied add-sqr-sqrt40.1

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{\sqrt{{im}^{2}} \cdot \sqrt{{im}^{2}}}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
    7. Applied associate-/l*40.1

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{\sqrt{{im}^{2}}}{\frac{\sqrt{re \cdot re + im \cdot im} - re}{\sqrt{{im}^{2}}}}}}\]
    8. Simplified40.1

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

    if 3.2163374571899126e-26 < re

    1. Initial program 37.9

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{re} + re\right)}\]
  3. Recombined 5 regimes into one program.
  4. Final simplification22.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -1.3380130268941683 \cdot 10^{154}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{2 \cdot {im}^{2}}}{\sqrt{-1 \cdot re - re}}\\ \mathbf{elif}\;re \le 7.2534218396275159 \cdot 10^{-289}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{2} \cdot \left|im\right|}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\\ \mathbf{elif}\;re \le 3.1852294886834028 \cdot 10^{-94}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im}{\frac{im - re}{im}}}\\ \mathbf{elif}\;re \le 3.21633745718991262 \cdot 10^{-26}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{\sqrt{{im}^{2}}}{\frac{\sqrt{re \cdot re + im \cdot im} - re}{\left|im\right|}}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + re\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2020106 
(FPCore (re im)
  :name "math.sqrt on complex, real part"
  :precision binary64

  :herbie-target
  (if (< re 0.0) (* 0.5 (* (sqrt 2) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2 (+ (sqrt (+ (* re re) (* im im))) re)))))

  (* 0.5 (sqrt (* 2 (+ (sqrt (+ (* re re) (* im im))) re)))))