Average Error: 38.7 → 5.6
Time: 3.6s
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.3940086170973382 \cdot 10^{-278}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{1}}{\frac{\mathsf{hypot}\left(re, im\right) - re}{im}}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\mathsf{hypot}\left(im, re\right) + 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.3940086170973382 \cdot 10^{-278}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{1}}{\frac{\mathsf{hypot}\left(re, im\right) - re}{im}}}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\mathsf{hypot}\left(im, re\right) + 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.3940086170973382e-278)) {
		VAR = (0.5 * sqrt((2.0 * (pow(im, 1.0) / ((hypot(re, im) - re) / im)))));
	} else {
		VAR = (0.5 * (sqrt(2.0) * sqrt((hypot(im, re) + re))));
	}
	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.7
Target33.7
Herbie5.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 2 regimes
  2. if re < 1.3940086170973382e-278

    1. Initial program 44.8

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

      \[\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. Simplified35.0

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2}}{\color{blue}{\mathsf{hypot}\left(re, im\right) - re}}}\]
    6. Using strategy rm
    7. Applied sqr-pow30.3

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)} \cdot {im}^{\left(\frac{2}{2}\right)}}}{\mathsf{hypot}\left(re, im\right) - re}}\]
    8. Applied associate-/l*10.3

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

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

    if 1.3940086170973382e-278 < re

    1. Initial program 32.2

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(im, re\right)} + re\right)}\]
    5. Using strategy rm
    6. Applied sqrt-prod0.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le 1.3940086170973382 \cdot 10^{-278}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{1}}{\frac{\mathsf{hypot}\left(re, im\right) - re}{im}}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\mathsf{hypot}\left(im, re\right) + re}\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2020078 +o rules:numerics
(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)))))