math.sqrt on complex, real part

Percentage Accurate: 41.4% → 90.1%
Time: 7.8s
Alternatives: 7
Speedup: 2.0×

Specification

?
\[\begin{array}{l} \\ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re))))
end
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 41.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re))))
end
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}

Alternative 1: 90.1% accurate, 0.2× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} t_0 := {\left(\frac{-1}{re}\right)}^{0.25}\\ \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot t_0\right)}^{2}}{\frac{re}{im} \cdot \frac{re}{im}}, {\left(t_0 \cdot \sqrt{im}\right)}^{2}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (pow (/ -1.0 re) 0.25)))
   (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
     (*
      0.5
      (fma
       -0.125
       (/ (pow (* (pow (* im im) 0.25) t_0) 2.0) (* (/ re im) (/ re im)))
       (pow (* t_0 (sqrt im)) 2.0)))
     (* 0.5 (sqrt (* 2.0 (+ re (hypot re im))))))))
im = abs(im);
double code(double re, double im) {
	double t_0 = pow((-1.0 / re), 0.25);
	double tmp;
	if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * fma(-0.125, (pow((pow((im * im), 0.25) * t_0), 2.0) / ((re / im) * (re / im))), pow((t_0 * sqrt(im)), 2.0));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	return tmp;
}
im = abs(im)
function code(re, im)
	t_0 = Float64(-1.0 / re) ^ 0.25
	tmp = 0.0
	if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0)
		tmp = Float64(0.5 * fma(-0.125, Float64((Float64((Float64(im * im) ^ 0.25) * t_0) ^ 2.0) / Float64(Float64(re / im) * Float64(re / im))), (Float64(t_0 * sqrt(im)) ^ 2.0)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return tmp
end
NOTE: im should be positive before calling this function
code[re_, im_] := Block[{t$95$0 = N[Power[N[(-1.0 / re), $MachinePrecision], 0.25], $MachinePrecision]}, If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[(-0.125 * N[(N[Power[N[(N[Power[N[(im * im), $MachinePrecision], 0.25], $MachinePrecision] * t$95$0), $MachinePrecision], 2.0], $MachinePrecision] / N[(N[(re / im), $MachinePrecision] * N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[Power[N[(t$95$0 * N[Sqrt[im], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
t_0 := {\left(\frac{-1}{re}\right)}^{0.25}\\
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot t_0\right)}^{2}}{\frac{re}{im} \cdot \frac{re}{im}}, {\left(t_0 \cdot \sqrt{im}\right)}^{2}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 8.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative8.1%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified8.1%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Step-by-step derivation
      1. add-sqr-sqrt8.1%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \cdot \sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)} \]
      2. pow28.1%

        \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)}^{2}} \]
      3. pow1/28.1%

        \[\leadsto 0.5 \cdot {\left(\sqrt{\color{blue}{{\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{0.5}}}\right)}^{2} \]
      4. sqrt-pow18.1%

        \[\leadsto 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]
      5. *-commutative8.1%

        \[\leadsto 0.5 \cdot {\left({\color{blue}{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2} \]
      6. metadata-eval8.1%

        \[\leadsto 0.5 \cdot {\left({\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}^{\color{blue}{0.25}}\right)}^{2} \]
    5. Applied egg-rr8.1%

      \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}^{0.25}\right)}^{2}} \]
    6. Taylor expanded in re around -inf 47.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(-0.125 \cdot \frac{{\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2} \cdot {im}^{2}}{{re}^{2}} + {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right)} \]
    7. Step-by-step derivation
      1. fma-def47.5%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(-0.125, \frac{{\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2} \cdot {im}^{2}}{{re}^{2}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right)} \]
      2. associate-/l*47.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \color{blue}{\frac{{\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}}{\frac{{re}^{2}}{{im}^{2}}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      3. distribute-rgt-in47.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left(e^{\color{blue}{\log \left({im}^{2}\right) \cdot 0.25 + \log \left(\frac{-1}{re}\right) \cdot 0.25}}\right)}^{2}}{\frac{{re}^{2}}{{im}^{2}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      4. exp-sum47.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\color{blue}{\left(e^{\log \left({im}^{2}\right) \cdot 0.25} \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.25}\right)}}^{2}}{\frac{{re}^{2}}{{im}^{2}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      5. exp-to-pow47.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left(\color{blue}{{\left({im}^{2}\right)}^{0.25}} \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.25}\right)}^{2}}{\frac{{re}^{2}}{{im}^{2}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      6. unpow247.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\color{blue}{\left(im \cdot im\right)}}^{0.25} \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.25}\right)}^{2}}{\frac{{re}^{2}}{{im}^{2}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      7. exp-to-pow47.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot \color{blue}{{\left(\frac{-1}{re}\right)}^{0.25}}\right)}^{2}}{\frac{{re}^{2}}{{im}^{2}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      8. unpow247.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}}{\frac{\color{blue}{re \cdot re}}{{im}^{2}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      9. unpow247.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}}{\frac{re \cdot re}{\color{blue}{im \cdot im}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
      10. times-frac47.5%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}}{\color{blue}{\frac{re}{im} \cdot \frac{re}{im}}}, {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\right) \]
    8. Simplified50.9%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(-0.125, \frac{{\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}}{\frac{re}{im} \cdot \frac{re}{im}}, {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\right)} \]
    9. Taylor expanded in re around -inf 21.7%

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

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

    1. Initial program 47.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative47.3%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def91.8%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified91.8%

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

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

Alternative 2: 89.5% accurate, 0.5× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{im} \cdot \sqrt{\frac{-im}{re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
   (* 0.5 (* (sqrt im) (sqrt (/ (- im) re))))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * (sqrt(im) * sqrt((-im / re)));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * (Math.sqrt(im) * Math.sqrt((-im / re)));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0:
		tmp = 0.5 * (math.sqrt(im) * math.sqrt((-im / re)))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0)
		tmp = Float64(0.5 * Float64(sqrt(im) * sqrt(Float64(Float64(-im) / re))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0)
		tmp = 0.5 * (sqrt(im) * sqrt((-im / re)));
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[(N[Sqrt[im], $MachinePrecision] * N[Sqrt[N[((-im) / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{im} \cdot \sqrt{\frac{-im}{re}}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 8.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative8.1%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified8.1%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    5. Step-by-step derivation
      1. *-commutative51.0%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
      3. associate-/l*61.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\frac{im}{\frac{re}{im}}} \cdot -0.5\right)} \]
    6. Simplified61.7%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}} \]
    7. Step-by-step derivation
      1. expm1-log1p-u61.3%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\right)\right)} \]
      2. expm1-udef17.3%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\right)} - 1\right)} \]
      3. *-commutative17.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(\frac{im}{\frac{re}{im}} \cdot -0.5\right) \cdot 2}}\right)} - 1\right) \]
      4. associate-*l*17.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\frac{im}{\frac{re}{im}} \cdot \left(-0.5 \cdot 2\right)}}\right)} - 1\right) \]
      5. associate-/r/17.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(\frac{im}{re} \cdot im\right)} \cdot \left(-0.5 \cdot 2\right)}\right)} - 1\right) \]
      6. *-commutative17.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(im \cdot \frac{im}{re}\right)} \cdot \left(-0.5 \cdot 2\right)}\right)} - 1\right) \]
      7. metadata-eval17.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot \color{blue}{-1}}\right)} - 1\right) \]
    8. Applied egg-rr17.3%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}\right)} - 1\right)} \]
    9. Step-by-step derivation
      1. expm1-def61.2%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}\right)\right)} \]
      2. expm1-log1p61.6%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}} \]
      3. associate-*l*61.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(\frac{im}{re} \cdot -1\right)}} \]
      4. associate-*l/61.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im \cdot -1}{re}}} \]
      5. *-commutative61.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{-1 \cdot im}}{re}} \]
      6. neg-mul-161.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{-im}}{re}} \]
    10. Simplified61.6%

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt{im \cdot \frac{-im}{re}}} \]
    11. Step-by-step derivation
      1. sqrt-prod20.1%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{\frac{-im}{re}}\right)} \]
    12. Applied egg-rr20.1%

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

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

    1. Initial program 47.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative47.3%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def91.8%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified91.8%

      \[\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 simplification82.8%

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

Alternative 3: 83.6% accurate, 1.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq -1.6 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re -1.6e+77)
   (* 0.5 (sqrt (* im (/ (- im) re))))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= -1.6e+77) {
		tmp = 0.5 * sqrt((im * (-im / re)));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.6e+77) {
		tmp = 0.5 * Math.sqrt((im * (-im / re)));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= -1.6e+77:
		tmp = 0.5 * math.sqrt((im * (-im / re)))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= -1.6e+77)
		tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.6e+77)
		tmp = 0.5 * sqrt((im * (-im / re)));
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, -1.6e+77], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.6 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -1.6000000000000001e77

    1. Initial program 7.6%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative7.6%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def40.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified40.0%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    5. Step-by-step derivation
      1. *-commutative52.3%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
      3. associate-/l*58.6%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}} \]
    7. Step-by-step derivation
      1. expm1-log1p-u57.9%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\right)\right)} \]
      2. expm1-udef36.3%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\right)} - 1\right)} \]
      3. *-commutative36.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(\frac{im}{\frac{re}{im}} \cdot -0.5\right) \cdot 2}}\right)} - 1\right) \]
      4. associate-*l*36.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\frac{im}{\frac{re}{im}} \cdot \left(-0.5 \cdot 2\right)}}\right)} - 1\right) \]
      5. associate-/r/36.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(\frac{im}{re} \cdot im\right)} \cdot \left(-0.5 \cdot 2\right)}\right)} - 1\right) \]
      6. *-commutative36.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(im \cdot \frac{im}{re}\right)} \cdot \left(-0.5 \cdot 2\right)}\right)} - 1\right) \]
      7. metadata-eval36.3%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot \color{blue}{-1}}\right)} - 1\right) \]
    8. Applied egg-rr36.3%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}\right)} - 1\right)} \]
    9. Step-by-step derivation
      1. expm1-def57.9%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}\right)\right)} \]
      2. expm1-log1p58.6%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}} \]
      3. associate-*l*58.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(\frac{im}{re} \cdot -1\right)}} \]
      4. associate-*l/58.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im \cdot -1}{re}}} \]
      5. *-commutative58.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{-1 \cdot im}}{re}} \]
      6. neg-mul-158.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{-im}}{re}} \]
    10. Simplified58.6%

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt{im \cdot \frac{-im}{re}}} \]

    if -1.6000000000000001e77 < re

    1. Initial program 49.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative49.4%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def89.7%

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

      \[\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 simplification84.4%

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

Alternative 4: 71.7% accurate, 1.9× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq -2 \cdot 10^{+73}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{elif}\;re \leq 2.85 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re -2e+73)
   (* 0.5 (sqrt (* im (/ (- im) re))))
   (if (<= re 2.85e+32)
     (* 0.5 (sqrt (* 2.0 (+ re im))))
     (* 0.5 (* 2.0 (sqrt re))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= -2e+73) {
		tmp = 0.5 * sqrt((im * (-im / re)));
	} else if (re <= 2.85e+32) {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-2d+73)) then
        tmp = 0.5d0 * sqrt((im * (-im / re)))
    else if (re <= 2.85d+32) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= -2e+73) {
		tmp = 0.5 * Math.sqrt((im * (-im / re)));
	} else if (re <= 2.85e+32) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= -2e+73:
		tmp = 0.5 * math.sqrt((im * (-im / re)))
	elif re <= 2.85e+32:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= -2e+73)
		tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re))));
	elseif (re <= 2.85e+32)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -2e+73)
		tmp = 0.5 * sqrt((im * (-im / re)));
	elseif (re <= 2.85e+32)
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, -2e+73], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.85e+32], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{+73}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\

\mathbf{elif}\;re \leq 2.85 \cdot 10^{+32}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -1.99999999999999997e73

    1. Initial program 7.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative7.4%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def40.6%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified40.6%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    5. Step-by-step derivation
      1. *-commutative52.3%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
      3. associate-/l*58.3%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\frac{im}{\frac{re}{im}}} \cdot -0.5\right)} \]
    6. Simplified58.3%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}} \]
    7. Step-by-step derivation
      1. expm1-log1p-u57.7%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\right)\right)} \]
      2. expm1-udef34.9%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\right)} - 1\right)} \]
      3. *-commutative34.9%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(\frac{im}{\frac{re}{im}} \cdot -0.5\right) \cdot 2}}\right)} - 1\right) \]
      4. associate-*l*34.9%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\frac{im}{\frac{re}{im}} \cdot \left(-0.5 \cdot 2\right)}}\right)} - 1\right) \]
      5. associate-/r/34.9%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(\frac{im}{re} \cdot im\right)} \cdot \left(-0.5 \cdot 2\right)}\right)} - 1\right) \]
      6. *-commutative34.9%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\left(im \cdot \frac{im}{re}\right)} \cdot \left(-0.5 \cdot 2\right)}\right)} - 1\right) \]
      7. metadata-eval34.9%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot \color{blue}{-1}}\right)} - 1\right) \]
    8. Applied egg-rr34.9%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}\right)} - 1\right)} \]
    9. Step-by-step derivation
      1. expm1-def57.6%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}\right)\right)} \]
      2. expm1-log1p58.3%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -1}} \]
      3. associate-*l*58.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(\frac{im}{re} \cdot -1\right)}} \]
      4. associate-*l/58.3%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im \cdot -1}{re}}} \]
      5. *-commutative58.3%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{-1 \cdot im}}{re}} \]
      6. neg-mul-158.3%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{-im}}{re}} \]
    10. Simplified58.3%

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt{im \cdot \frac{-im}{re}}} \]

    if -1.99999999999999997e73 < re < 2.85e32

    1. Initial program 52.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative52.7%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def86.8%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified86.8%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Taylor expanded in re around 0 32.6%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out32.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. +-commutative32.6%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + im\right)}} \]
      3. *-commutative32.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(re + im\right) \cdot 2}} \]
      4. +-commutative32.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(im + re\right)} \cdot 2} \]
    6. Simplified32.6%

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

    if 2.85e32 < re

    1. Initial program 42.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative42.2%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Taylor expanded in im around 0 77.9%

      \[\leadsto 0.5 \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
    5. Step-by-step derivation
      1. unpow277.9%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt79.4%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified79.4%

      \[\leadsto 0.5 \cdot \color{blue}{\left(2 \cdot \sqrt{re}\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification47.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2 \cdot 10^{+73}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{elif}\;re \leq 2.85 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 5: 63.5% accurate, 2.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq 1.8 \cdot 10^{+33}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re 1.8e+33)
   (* 0.5 (sqrt (* 2.0 (+ re im))))
   (* 0.5 (* 2.0 (sqrt re)))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= 1.8e+33) {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= 1.8d+33) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= 1.8e+33) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= 1.8e+33:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= 1.8e+33)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= 1.8e+33)
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, 1.8e+33], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.8 \cdot 10^{+33}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 1.8000000000000001e33

    1. Initial program 42.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative42.4%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def76.3%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified76.3%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Taylor expanded in re around 0 28.6%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out28.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. +-commutative28.6%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + im\right)}} \]
      3. *-commutative28.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(re + im\right) \cdot 2}} \]
      4. +-commutative28.6%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(im + re\right)} \cdot 2} \]
    6. Simplified28.6%

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

    if 1.8000000000000001e33 < re

    1. Initial program 42.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative42.2%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Taylor expanded in im around 0 77.9%

      \[\leadsto 0.5 \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
    5. Step-by-step derivation
      1. unpow277.9%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt79.4%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified79.4%

      \[\leadsto 0.5 \cdot \color{blue}{\left(2 \cdot \sqrt{re}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification40.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.8 \cdot 10^{+33}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 6: 63.9% accurate, 2.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq 1.62 \cdot 10^{+30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re 1.62e+30) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re)))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= 1.62e+30) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= 1.62d+30) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= 1.62e+30) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= 1.62e+30:
		tmp = 0.5 * math.sqrt((2.0 * im))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= 1.62e+30)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= 1.62e+30)
		tmp = 0.5 * sqrt((2.0 * im));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, 1.62e+30], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.62 \cdot 10^{+30}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 1.6200000000000001e30

    1. Initial program 42.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative42.4%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
      2. hypot-def76.3%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified76.3%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Taylor expanded in re around 0 26.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative26.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    6. Simplified26.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]

    if 1.6200000000000001e30 < re

    1. Initial program 42.2%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. +-commutative42.2%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Taylor expanded in im around 0 77.9%

      \[\leadsto 0.5 \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
    5. Step-by-step derivation
      1. unpow277.9%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt79.4%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified79.4%

      \[\leadsto 0.5 \cdot \color{blue}{\left(2 \cdot \sqrt{re}\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification38.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.62 \cdot 10^{+30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 7: 51.9% accurate, 2.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ 0.5 \cdot \sqrt{2 \cdot im} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
im = abs(im);
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * im));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.5d0 * sqrt((2.0d0 * im))
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * im));
}
im = abs(im)
def code(re, im):
	return 0.5 * math.sqrt((2.0 * im))
im = abs(im)
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * im)))
end
im = abs(im)
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * im));
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Derivation
  1. Initial program 42.4%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
  2. Step-by-step derivation
    1. +-commutative42.4%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \]
    2. hypot-def81.3%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
  3. Simplified81.3%

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
  4. Taylor expanded in re around 0 24.0%

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
  5. Step-by-step derivation
    1. *-commutative24.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  6. Simplified24.0%

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  7. Final simplification24.0%

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot im} \]

Developer target: 49.0% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im}\\ \mathbf{if}\;re < 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (sqrt (+ (* re re) (* im im)))))
   (if (< re 0.0)
     (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
     (* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
	double t_0 = sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = sqrt(((re * re) + (im * im)))
    if (re < 0.0d0) then
        tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = Math.sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
	}
	return tmp;
}
def code(re, im):
	t_0 = math.sqrt(((re * re) + (im * im)))
	tmp = 0
	if re < 0.0:
		tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re))))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (t_0 + re)))
	return tmp
function code(re, im)
	t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im)))
	tmp = 0.0
	if (re < 0.0)
		tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re)))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = sqrt(((re * re) + (im * im)));
	tmp = 0.0;
	if (re < 0.0)
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	else
		tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\


\end{array}
\end{array}

Reproduce

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