Average Error: 38.7 → 12.5
Time: 11.0s
Precision: binary64
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -3.9425870866783413 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \mathsf{fma}\left({\left(\sqrt[3]{re}\right)}^{2}, \sqrt[3]{re}, \mathsf{hypot}\left(re, im\right)\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 \leq -3.9425870866783413 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \mathsf{fma}\left({\left(\sqrt[3]{re}\right)}^{2}, \sqrt[3]{re}, \mathsf{hypot}\left(re, im\right)\right)}\\


\end{array}
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (if (<= re -3.9425870866783413e+62)
   (* 0.5 (sqrt (* 2.0 (* -0.5 (/ (pow im 2.0) re)))))
   (* 0.5 (sqrt (* 2.0 (fma (pow (cbrt re) 2.0) (cbrt re) (hypot re im)))))))
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 tmp;
	if (re <= -3.9425870866783413e+62) {
		tmp = 0.5 * sqrt((2.0 * (-0.5 * (pow(im, 2.0) / re))));
	} else {
		tmp = 0.5 * sqrt((2.0 * fma(pow(cbrt(re), 2.0), cbrt(re), hypot(re, im))));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Target

Original38.7
Target33.5
Herbie12.5
\[\begin{array}{l} \mathbf{if}\;re < 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 < -3.94258708667834127e62

    1. Initial program 59.3

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

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

    if -3.94258708667834127e62 < re

    1. Initial program 33.3

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Applied egg-rr7.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.9425870866783413 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \mathsf{fma}\left({\left(\sqrt[3]{re}\right)}^{2}, \sqrt[3]{re}, \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Reproduce

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

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

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