Average Error: 38.9 → 10.6
Time: 6.6s
Precision: binary64
\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\begin{array}{l} \mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot e^{\sqrt[3]{{\left(\mathsf{fma}\left(2, \log im, \log \left(\frac{0.5}{re}\right)\right)\right)}^{3}}}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\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}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot e^{\sqrt[3]{{\left(\mathsf{fma}\left(2, \log im, \log \left(\frac{0.5}{re}\right)\right)\right)}^{3}}}}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\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 (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0)
   (*
    0.5
    (sqrt (* 2.0 (exp (cbrt (pow (fma 2.0 (log im) (log (/ 0.5 re))) 3.0))))))
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))))
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 ((sqrt((re * re) + (im * im)) - re) <= 0.0) {
		tmp = 0.5 * sqrt(2.0 * exp(cbrt(pow(fma(2.0, log(im), log(0.5 / re)), 3.0))));
	} else {
		tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Derivation

  1. Split input into 2 regimes
  2. if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 59.0

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    3. Applied add-sqr-sqrt_binary6453.7

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\log \left(\mathsf{fma}\left(\sqrt{\mathsf{hypot}\left(re, im\right)}, \sqrt{\mathsf{hypot}\left(re, im\right)}, -re\right)\right)}}} \]
    6. Applied add-cube-cbrt_binary6456.6

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\color{blue}{\left(\sqrt[3]{\log \left(\mathsf{fma}\left(\sqrt{\mathsf{hypot}\left(re, im\right)}, \sqrt{\mathsf{hypot}\left(re, im\right)}, -re\right)\right)} \cdot \sqrt[3]{\log \left(\mathsf{fma}\left(\sqrt{\mathsf{hypot}\left(re, im\right)}, \sqrt{\mathsf{hypot}\left(re, im\right)}, -re\right)\right)}\right) \cdot \sqrt[3]{\log \left(\mathsf{fma}\left(\sqrt{\mathsf{hypot}\left(re, im\right)}, \sqrt{\mathsf{hypot}\left(re, im\right)}, -re\right)\right)}}}} \]
    7. Applied exp-prod_binary6456.6

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{{\left(\left(\log \left(\frac{0.5}{re}\right) + 2 \cdot \log im\right) \cdot {\left(2 \cdot \log im + \log \left(\frac{0.5}{re}\right)\right)}^{2}\right)}^{0.3333333333333333}}}} \]
    9. Simplified31.9

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\sqrt[3]{{\left(\mathsf{fma}\left(2, \log im, \log \left(\frac{0.5}{re}\right)\right)\right)}^{3}}}}} \]

    if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

    1. Initial program 35.3

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

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

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

Reproduce

herbie shell --seed 2022003 
(FPCore (re im)
  :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
  :precision binary64
  :pre (> im 0.0)
  (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))