Average Error: 37.7 → 10.1
Time: 7.3s
Precision: binary64
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} t_0 := re + \sqrt{re \cdot re + im \cdot im}\\ \mathbf{if}\;t_0 \leq -2.908944228556174 \cdot 10^{-287}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{log1p}\left(\mathsf{expm1}\left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)}\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{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}
t_0 := re + \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;t_0 \leq -2.908944228556174 \cdot 10^{-287}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{log1p}\left(\mathsf{expm1}\left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)}\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{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
 (let* ((t_0 (+ re (sqrt (+ (* re re) (* im im))))))
   (if (<= t_0 -2.908944228556174e-287)
     (* 0.5 (sqrt (* 2.0 (+ re (log1p (expm1 (hypot re im)))))))
     (if (<= t_0 0.0)
       (* 0.5 (sqrt (* 2.0 (* -0.5 (/ (pow im 2.0) re)))))
       (* 0.5 (* (sqrt 2.0) (sqrt (+ 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 t_0 = re + sqrt((re * re) + (im * im));
	double tmp;
	if (t_0 <= -2.908944228556174e-287) {
		tmp = 0.5 * sqrt(2.0 * (re + log1p(expm1(hypot(re, im)))));
	} else if (t_0 <= 0.0) {
		tmp = 0.5 * sqrt(2.0 * (-0.5 * (pow(im, 2.0) / re)));
	} else {
		tmp = 0.5 * (sqrt(2.0) * sqrt(re + hypot(re, im)));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original37.7
Target33.0
Herbie10.1
\[\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 3 regimes
  2. if (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < -2.908944228556174e-287

    1. Initial program 64.0

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\mathsf{hypot}\left(re, im\right)}\right)\right)}\right)} \]
    5. Simplified32.5

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\mathsf{hypot}\left(re, im\right)\right)}\right)\right)} \]

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

    1. Initial program 57.8

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    3. Taylor expanded in re around -inf 29.4

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

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

    1. Initial program 34.1

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{re + \sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\mathsf{hypot}\left(re, im\right)}}\right)} \]
    5. Simplified6.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq -2.908944228556174 \cdot 10^{-287}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{log1p}\left(\mathsf{expm1}\left(\mathsf{hypot}\left(re, im\right)\right)\right)\right)}\\ \mathbf{elif}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{re + \mathsf{hypot}\left(re, im\right)}\right)\\ \end{array} \]

Reproduce

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