math.sqrt on complex, real part

Percentage Accurate: 40.9% → 82.5%
Time: 8.2s
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: 40.9% 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: 82.5% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.02 \cdot 10^{+96}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im \cdot \frac{im}{\frac{re}{-0.5}}\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \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.02000000000000001e96

    1. Initial program 5.0%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re} - re\right)} + re\right)} \]
      4. associate-*r/19.0%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\left(\frac{\left(-0.5 \cdot im\right) \cdot im}{re} - re\right)} + re\right)} \]
    5. Taylor expanded in im around 0 62.0%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    6. Step-by-step derivation
      1. associate-*r/62.0%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{-0.5 \cdot {im}^{2}}{re}}} \]
      2. unpow262.0%

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

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

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

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

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

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

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

    if -1.02000000000000001e96 < re

    1. Initial program 50.5%

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

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

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

      \[\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-sqrt89.3%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      6. add-sqr-sqrt90.0%

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

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

        \[\leadsto \sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot \color{blue}{0.25}} \]
    5. Applied egg-rr90.0%

      \[\leadsto \color{blue}{\sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25}} \]
    6. Step-by-step derivation
      1. associate-*l*90.0%

        \[\leadsto \sqrt{\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \left(2 \cdot 0.25\right)}} \]
      2. metadata-eval90.0%

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

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

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

Alternative 2: 59.9% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 2 \cdot \left(re + im\right)\\ \mathbf{if}\;im \leq -9.2 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{-186}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 2.05 \cdot 10^{-159}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 3.5 \cdot 10^{-72}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + t_0}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{t_0}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 2.0 (+ re im))))
   (if (<= im -9.2e-116)
     (* 0.5 (sqrt (* 2.0 (- re im))))
     (if (<= im 9.8e-186)
       (sqrt re)
       (if (<= im 2.05e-159)
         (* 0.5 (sqrt (* 2.0 im)))
         (if (<= im 1.1e-111)
           (sqrt re)
           (if (<= im 3.5e-72)
             (* 0.5 (sqrt (+ (/ (* re re) im) t_0)))
             (* 0.5 (sqrt t_0)))))))))
double code(double re, double im) {
	double t_0 = 2.0 * (re + im);
	double tmp;
	if (im <= -9.2e-116) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else if (im <= 9.8e-186) {
		tmp = sqrt(re);
	} else if (im <= 2.05e-159) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else if (im <= 1.1e-111) {
		tmp = sqrt(re);
	} else if (im <= 3.5e-72) {
		tmp = 0.5 * sqrt((((re * re) / im) + t_0));
	} else {
		tmp = 0.5 * sqrt(t_0);
	}
	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 = 2.0d0 * (re + im)
    if (im <= (-9.2d-116)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else if (im <= 9.8d-186) then
        tmp = sqrt(re)
    else if (im <= 2.05d-159) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else if (im <= 1.1d-111) then
        tmp = sqrt(re)
    else if (im <= 3.5d-72) then
        tmp = 0.5d0 * sqrt((((re * re) / im) + t_0))
    else
        tmp = 0.5d0 * sqrt(t_0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 2.0 * (re + im);
	double tmp;
	if (im <= -9.2e-116) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else if (im <= 9.8e-186) {
		tmp = Math.sqrt(re);
	} else if (im <= 2.05e-159) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else if (im <= 1.1e-111) {
		tmp = Math.sqrt(re);
	} else if (im <= 3.5e-72) {
		tmp = 0.5 * Math.sqrt((((re * re) / im) + t_0));
	} else {
		tmp = 0.5 * Math.sqrt(t_0);
	}
	return tmp;
}
def code(re, im):
	t_0 = 2.0 * (re + im)
	tmp = 0
	if im <= -9.2e-116:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	elif im <= 9.8e-186:
		tmp = math.sqrt(re)
	elif im <= 2.05e-159:
		tmp = 0.5 * math.sqrt((2.0 * im))
	elif im <= 1.1e-111:
		tmp = math.sqrt(re)
	elif im <= 3.5e-72:
		tmp = 0.5 * math.sqrt((((re * re) / im) + t_0))
	else:
		tmp = 0.5 * math.sqrt(t_0)
	return tmp
function code(re, im)
	t_0 = Float64(2.0 * Float64(re + im))
	tmp = 0.0
	if (im <= -9.2e-116)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	elseif (im <= 9.8e-186)
		tmp = sqrt(re);
	elseif (im <= 2.05e-159)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	elseif (im <= 1.1e-111)
		tmp = sqrt(re);
	elseif (im <= 3.5e-72)
		tmp = Float64(0.5 * sqrt(Float64(Float64(Float64(re * re) / im) + t_0)));
	else
		tmp = Float64(0.5 * sqrt(t_0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 2.0 * (re + im);
	tmp = 0.0;
	if (im <= -9.2e-116)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	elseif (im <= 9.8e-186)
		tmp = sqrt(re);
	elseif (im <= 2.05e-159)
		tmp = 0.5 * sqrt((2.0 * im));
	elseif (im <= 1.1e-111)
		tmp = sqrt(re);
	elseif (im <= 3.5e-72)
		tmp = 0.5 * sqrt((((re * re) / im) + t_0));
	else
		tmp = 0.5 * sqrt(t_0);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -9.2e-116], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 9.8e-186], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 2.05e-159], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.1e-111], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 3.5e-72], N[(0.5 * N[Sqrt[N[(N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 2 \cdot \left(re + im\right)\\
\mathbf{if}\;im \leq -9.2 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{elif}\;im \leq 9.8 \cdot 10^{-186}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq 2.05 \cdot 10^{-159}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{elif}\;im \leq 1.1 \cdot 10^{-111}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq 3.5 \cdot 10^{-72}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + t_0}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{t_0}\\


\end{array}
\end{array}
Derivation
  1. Split input into 5 regimes
  2. if im < -9.20000000000000006e-116

    1. Initial program 45.9%

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

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

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

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

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

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

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

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

    if -9.20000000000000006e-116 < im < 9.7999999999999992e-186 or 2.05000000000000007e-159 < im < 1.1e-111

    1. Initial program 35.1%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified71.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 43.9%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{re}} \]
      2. unpow243.9%

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

        \[\leadsto \left(0.5 \cdot \color{blue}{2}\right) \cdot \sqrt{re} \]
      4. metadata-eval44.8%

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      5. *-lft-identity44.8%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified44.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]

    if 9.7999999999999992e-186 < im < 2.05000000000000007e-159

    1. Initial program 26.3%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified76.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 0 65.2%

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

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

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

    if 1.1e-111 < im < 3.5e-72

    1. Initial program 78.8%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified78.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 64.0%

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{re \cdot re}}{im} + \left(2 \cdot im + 2 \cdot re\right)} \]
      2. distribute-lft-out64.0%

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

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

    if 3.5e-72 < im

    1. Initial program 41.8%

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

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

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

      \[\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 65.0%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -9.2 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{-186}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 2.05 \cdot 10^{-159}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 3.5 \cdot 10^{-72}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 3: 59.0% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -1.5 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 10^{-185} \lor \neg \left(im \leq 3.4 \cdot 10^{-159}\right) \land im \leq 1.65 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -1.5e-116)
   (* 0.5 (sqrt (* im -2.0)))
   (if (or (<= im 1e-185) (and (not (<= im 3.4e-159)) (<= im 1.65e-111)))
     (sqrt re)
     (* 0.5 (sqrt (* 2.0 im))))))
double code(double re, double im) {
	double tmp;
	if (im <= -1.5e-116) {
		tmp = 0.5 * sqrt((im * -2.0));
	} else if ((im <= 1e-185) || (!(im <= 3.4e-159) && (im <= 1.65e-111))) {
		tmp = sqrt(re);
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-1.5d-116)) then
        tmp = 0.5d0 * sqrt((im * (-2.0d0)))
    else if ((im <= 1d-185) .or. (.not. (im <= 3.4d-159)) .and. (im <= 1.65d-111)) then
        tmp = sqrt(re)
    else
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= -1.5e-116) {
		tmp = 0.5 * Math.sqrt((im * -2.0));
	} else if ((im <= 1e-185) || (!(im <= 3.4e-159) && (im <= 1.65e-111))) {
		tmp = Math.sqrt(re);
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -1.5e-116:
		tmp = 0.5 * math.sqrt((im * -2.0))
	elif (im <= 1e-185) or (not (im <= 3.4e-159) and (im <= 1.65e-111)):
		tmp = math.sqrt(re)
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -1.5e-116)
		tmp = Float64(0.5 * sqrt(Float64(im * -2.0)));
	elseif ((im <= 1e-185) || (!(im <= 3.4e-159) && (im <= 1.65e-111)))
		tmp = sqrt(re);
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= -1.5e-116)
		tmp = 0.5 * sqrt((im * -2.0));
	elseif ((im <= 1e-185) || (~((im <= 3.4e-159)) && (im <= 1.65e-111)))
		tmp = sqrt(re);
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -1.5e-116], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[im, 1e-185], And[N[Not[LessEqual[im, 3.4e-159]], $MachinePrecision], LessEqual[im, 1.65e-111]]], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.5 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\

\mathbf{elif}\;im \leq 10^{-185} \lor \neg \left(im \leq 3.4 \cdot 10^{-159}\right) \land im \leq 1.65 \cdot 10^{-111}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -1.50000000000000013e-116

    1. Initial program 45.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot -2}} \]
    6. Simplified69.3%

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

    if -1.50000000000000013e-116 < im < 9.9999999999999999e-186 or 3.39999999999999984e-159 < im < 1.65e-111

    1. Initial program 35.1%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified71.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 43.9%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{re}} \]
      2. unpow243.9%

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

        \[\leadsto \left(0.5 \cdot \color{blue}{2}\right) \cdot \sqrt{re} \]
      4. metadata-eval44.8%

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      5. *-lft-identity44.8%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified44.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]

    if 9.9999999999999999e-186 < im < 3.39999999999999984e-159 or 1.65e-111 < im

    1. Initial program 43.9%

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

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

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

      \[\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 63.0%

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification60.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -1.5 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 10^{-185} \lor \neg \left(im \leq 3.4 \cdot 10^{-159}\right) \land im \leq 1.65 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 4: 59.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -8.2 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{-185} \lor \neg \left(im \leq 2.2 \cdot 10^{-157}\right) \land im \leq 1.4 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -8.2e-116)
   (* 0.5 (sqrt (* 2.0 (- re im))))
   (if (or (<= im 6.6e-185) (and (not (<= im 2.2e-157)) (<= im 1.4e-111)))
     (sqrt re)
     (* 0.5 (sqrt (* 2.0 im))))))
double code(double re, double im) {
	double tmp;
	if (im <= -8.2e-116) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else if ((im <= 6.6e-185) || (!(im <= 2.2e-157) && (im <= 1.4e-111))) {
		tmp = sqrt(re);
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-8.2d-116)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else if ((im <= 6.6d-185) .or. (.not. (im <= 2.2d-157)) .and. (im <= 1.4d-111)) then
        tmp = sqrt(re)
    else
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= -8.2e-116) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else if ((im <= 6.6e-185) || (!(im <= 2.2e-157) && (im <= 1.4e-111))) {
		tmp = Math.sqrt(re);
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -8.2e-116:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	elif (im <= 6.6e-185) or (not (im <= 2.2e-157) and (im <= 1.4e-111)):
		tmp = math.sqrt(re)
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -8.2e-116)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	elseif ((im <= 6.6e-185) || (!(im <= 2.2e-157) && (im <= 1.4e-111)))
		tmp = sqrt(re);
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= -8.2e-116)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	elseif ((im <= 6.6e-185) || (~((im <= 2.2e-157)) && (im <= 1.4e-111)))
		tmp = sqrt(re);
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -8.2e-116], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[im, 6.6e-185], And[N[Not[LessEqual[im, 2.2e-157]], $MachinePrecision], LessEqual[im, 1.4e-111]]], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -8.2 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{elif}\;im \leq 6.6 \cdot 10^{-185} \lor \neg \left(im \leq 2.2 \cdot 10^{-157}\right) \land im \leq 1.4 \cdot 10^{-111}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -8.1999999999999998e-116

    1. Initial program 45.9%

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

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

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

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

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

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

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

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

    if -8.1999999999999998e-116 < im < 6.5999999999999995e-185 or 2.2000000000000001e-157 < im < 1.39999999999999998e-111

    1. Initial program 35.1%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]
    3. Simplified71.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 43.9%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{re}} \]
      2. unpow243.9%

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

        \[\leadsto \left(0.5 \cdot \color{blue}{2}\right) \cdot \sqrt{re} \]
      4. metadata-eval44.8%

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      5. *-lft-identity44.8%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified44.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]

    if 6.5999999999999995e-185 < im < 2.2000000000000001e-157 or 1.39999999999999998e-111 < im

    1. Initial program 43.9%

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

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

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

      \[\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 63.0%

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification60.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -8.2 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{-185} \lor \neg \left(im \leq 2.2 \cdot 10^{-157}\right) \land im \leq 1.4 \cdot 10^{-111}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 5: 60.1% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -1.05 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 5.1 \cdot 10^{-185}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -1.05e-116)
   (* 0.5 (sqrt (* 2.0 (- re im))))
   (if (<= im 5.1e-185) (sqrt re) (* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
	double tmp;
	if (im <= -1.05e-116) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else if (im <= 5.1e-185) {
		tmp = sqrt(re);
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-1.05d-116)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else if (im <= 5.1d-185) then
        tmp = sqrt(re)
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= -1.05e-116) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else if (im <= 5.1e-185) {
		tmp = Math.sqrt(re);
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -1.05e-116:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	elif im <= 5.1e-185:
		tmp = math.sqrt(re)
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -1.05e-116)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	elseif (im <= 5.1e-185)
		tmp = sqrt(re);
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= -1.05e-116)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	elseif (im <= 5.1e-185)
		tmp = sqrt(re);
	else
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -1.05e-116], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5.1e-185], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.05 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{elif}\;im \leq 5.1 \cdot 10^{-185}:\\
\;\;\;\;\sqrt{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -1.05e-116

    1. Initial program 45.9%

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

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

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

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

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

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

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

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

    if -1.05e-116 < im < 5.1000000000000003e-185

    1. Initial program 31.9%

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

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

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

      \[\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 40.7%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{re}} \]
      2. unpow240.7%

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

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      5. *-lft-identity41.4%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified41.4%

      \[\leadsto \color{blue}{\sqrt{re}} \]

    if 5.1000000000000003e-185 < im

    1. Initial program 44.9%

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

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

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

      \[\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 61.5%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -1.05 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 5.1 \cdot 10^{-185}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 6: 43.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -3.4 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -3.4e-116) (* 0.5 (sqrt (* im -2.0))) (sqrt re)))
double code(double re, double im) {
	double tmp;
	if (im <= -3.4e-116) {
		tmp = 0.5 * sqrt((im * -2.0));
	} else {
		tmp = sqrt(re);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-3.4d-116)) then
        tmp = 0.5d0 * sqrt((im * (-2.0d0)))
    else
        tmp = sqrt(re)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= -3.4e-116) {
		tmp = 0.5 * Math.sqrt((im * -2.0));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -3.4e-116:
		tmp = 0.5 * math.sqrt((im * -2.0))
	else:
		tmp = math.sqrt(re)
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -3.4e-116)
		tmp = Float64(0.5 * sqrt(Float64(im * -2.0)));
	else
		tmp = sqrt(re);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= -3.4e-116)
		tmp = 0.5 * sqrt((im * -2.0));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -3.4e-116], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -3.4 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < -3.39999999999999992e-116

    1. Initial program 45.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot -2}} \]
    6. Simplified69.3%

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

    if -3.39999999999999992e-116 < im

    1. Initial program 40.3%

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

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

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

      \[\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 30.8%

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

        \[\leadsto \color{blue}{\left(0.5 \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{re}} \]
      2. unpow230.8%

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

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      5. *-lft-identity31.4%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified31.4%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification43.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -3.4 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]

Alternative 7: 26.2% accurate, 2.1× speedup?

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

\\
\sqrt{re}
\end{array}
Derivation
  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-def79.1%

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

    \[\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 24.9%

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

      \[\leadsto \color{blue}{\left(0.5 \cdot {\left(\sqrt{2}\right)}^{2}\right) \cdot \sqrt{re}} \]
    2. unpow224.9%

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

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

      \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
    5. *-lft-identity25.4%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  6. Simplified25.4%

    \[\leadsto \color{blue}{\sqrt{re}} \]
  7. Final simplification25.4%

    \[\leadsto \sqrt{re} \]

Developer target: 48.5% 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 2023193 
(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)))))