Average Error: 38.7 → 11.7
Time: 4.6s
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 -2.926404548759696 \cdot 10^{+196}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot \mathsf{fma}\left(0.125, \frac{im}{\frac{re \cdot re}{im}}, -0.5\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(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 -2.926404548759696 \cdot 10^{+196}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot \mathsf{fma}\left(0.125, \frac{im}{\frac{re \cdot re}{im}}, -0.5\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(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 -2.926404548759696e+196)
   (*
    0.5
    (sqrt
     (* 2.0 (* (/ (* im im) re) (fma 0.125 (/ im (/ (* re re) im)) -0.5)))))
   (* 0.5 (sqrt (* 2.0 (+ 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 <= -2.926404548759696e+196) {
		tmp = 0.5 * sqrt(2.0 * (((im * im) / re) * fma(0.125, (im / ((re * re) / im)), -0.5)));
	} else {
		tmp = 0.5 * sqrt(2.0 * (re + hypot(re, im)));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Target

Original38.7
Target33.9
Herbie11.7
\[\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 < -2.92640454875969598e196

    1. Initial program 64.0

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified45.2

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\mathsf{fma}\left(\sqrt[3]{re} \cdot \sqrt[3]{re}, \sqrt[3]{re}, \mathsf{hypot}\left(re, im\right)\right)}} \]
    5. Applied add-exp-log_binary6457.8

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\log \left(\mathsf{fma}\left(\sqrt[3]{re} \cdot \sqrt[3]{re}, \sqrt[3]{re}, \mathsf{hypot}\left(re, im\right)\right)\right)}}} \]
    6. Taylor expanded in re around -inf 40.7

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.125 \cdot \frac{{im}^{4}}{{re}^{3}} - 0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    7. Simplified31.1

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

    if -2.92640454875969598e196 < re

    1. Initial program 36.3

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified9.9

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

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

Reproduce

herbie shell --seed 2022103 
(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)))))