math.sqrt on complex, real part

Percentage Accurate: 41.8% → 86.6%
Time: 10.9s
Alternatives: 6
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 6 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.8% 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: 86.6% accurate, 0.7× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq -3.7 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot e^{0.5 \cdot \left(2 \cdot \log im + \log \left(\frac{-1}{re}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \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 -3.7e+31)
   (* 0.5 (exp (* 0.5 (+ (* 2.0 (log im)) (log (/ -1.0 re))))))
   (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= -3.7e+31) {
		tmp = 0.5 * exp((0.5 * ((2.0 * log(im)) + log((-1.0 / re)))));
	} else {
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	}
	return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= -3.7e+31) {
		tmp = 0.5 * Math.exp((0.5 * ((2.0 * Math.log(im)) + Math.log((-1.0 / re)))));
	} else {
		tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= -3.7e+31:
		tmp = 0.5 * math.exp((0.5 * ((2.0 * math.log(im)) + math.log((-1.0 / re)))))
	else:
		tmp = math.sqrt((0.5 * (re + math.hypot(re, im))))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= -3.7e+31)
		tmp = Float64(0.5 * exp(Float64(0.5 * Float64(Float64(2.0 * log(im)) + log(Float64(-1.0 / re))))));
	else
		tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im))));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -3.7e+31)
		tmp = 0.5 * exp((0.5 * ((2.0 * log(im)) + log((-1.0 / re)))));
	else
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, -3.7e+31], N[(0.5 * N[Exp[N[(0.5 * N[(N[(2.0 * N[Log[im], $MachinePrecision]), $MachinePrecision] + N[Log[N[(-1.0 / re), $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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.7 \cdot 10^{+31}:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(2 \cdot \log im + \log \left(\frac{-1}{re}\right)\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 < -3.6999999999999998e31

    1. Initial program 5.8%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in5.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub5.8%

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re} + 0.25 \cdot \frac{{im}^{4}}{{re}^{3}}}} \]
    5. Step-by-step derivation
      1. +-commutative29.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg29.1%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. associate-*r/29.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{0.25 \cdot {im}^{4}}{{re}^{3}}} - \frac{{im}^{2}}{re}} \]
      5. unpow229.1%

        \[\leadsto 0.5 \cdot \sqrt{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*28.8%

        \[\leadsto 0.5 \cdot \sqrt{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified28.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{im}{\frac{re}{im}}}} \]
    7. Step-by-step derivation
      1. pow1/228.9%

        \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{im}{\frac{re}{im}}\right)}^{0.5}} \]
      2. pow-to-exp27.8%

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{im}{\frac{re}{im}}\right) \cdot 0.5}} \]
    8. Applied egg-rr28.1%

      \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(\mathsf{fma}\left(0.25 \cdot {im}^{4}, {re}^{-3}, im \cdot \left(-\frac{im}{re}\right)\right)\right) \cdot 0.5}} \]
    9. Taylor expanded in im around 0 34.6%

      \[\leadsto 0.5 \cdot e^{\color{blue}{\left(\log \left(-\frac{1}{re}\right) + 2 \cdot \log im\right)} \cdot 0.5} \]
    10. Step-by-step derivation
      1. +-commutative34.6%

        \[\leadsto 0.5 \cdot e^{\color{blue}{\left(2 \cdot \log im + \log \left(-\frac{1}{re}\right)\right)} \cdot 0.5} \]
      2. distribute-neg-frac34.6%

        \[\leadsto 0.5 \cdot e^{\left(2 \cdot \log im + \log \color{blue}{\left(\frac{-1}{re}\right)}\right) \cdot 0.5} \]
      3. metadata-eval34.6%

        \[\leadsto 0.5 \cdot e^{\left(2 \cdot \log im + \log \left(\frac{\color{blue}{-1}}{re}\right)\right) \cdot 0.5} \]
    11. Simplified34.6%

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

    if -3.6999999999999998e31 < re

    1. Initial program 51.1%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in51.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub51.1%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg51.1%

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

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

      \[\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-sqrt93.6%

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}} \]
      2. associate-*r*94.3%

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

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

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

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

Alternative 2: 84.7% accurate, 0.7× speedup?

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

\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 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 4.7%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in4.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub4.7%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg4.7%

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re} + 0.25 \cdot \frac{{im}^{4}}{{re}^{3}}}} \]
    5. Step-by-step derivation
      1. +-commutative28.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg28.1%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. associate-*r/28.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{0.25 \cdot {im}^{4}}{{re}^{3}}} - \frac{{im}^{2}}{re}} \]
      5. unpow228.1%

        \[\leadsto 0.5 \cdot \sqrt{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*28.1%

        \[\leadsto 0.5 \cdot \sqrt{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified28.1%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 32.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. mul-1-neg32.9%

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-/l*47.0%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    9. Simplified47.0%

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

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

    1. Initial program 46.0%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub46.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{0.25 \cdot \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}} \]
      2. associate-*r*90.3%

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

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

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

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

Alternative 3: 71.1% accurate, 1.9× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq -1.26 \cdot 10^{+92}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq 1.6 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re -1.26e+92)
   (* 0.5 (sqrt (/ (- im) (/ re im))))
   (if (<= re 1.6e-33) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= -1.26e+92) {
		tmp = 0.5 * sqrt((-im / (re / im)));
	} else if (re <= 1.6e-33) {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	} else {
		tmp = 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.26d+92)) then
        tmp = 0.5d0 * sqrt((-im / (re / im)))
    else if (re <= 1.6d-33) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    else
        tmp = 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.26e+92) {
		tmp = 0.5 * Math.sqrt((-im / (re / im)));
	} else if (re <= 1.6e-33) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= -1.26e+92:
		tmp = 0.5 * math.sqrt((-im / (re / im)))
	elif re <= 1.6e-33:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	else:
		tmp = math.sqrt(re)
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= -1.26e+92)
		tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im))));
	elseif (re <= 1.6e-33)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.26e+92)
		tmp = 0.5 * sqrt((-im / (re / im)));
	elseif (re <= 1.6e-33)
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, -1.26e+92], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.6e-33], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.26 \cdot 10^{+92}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\

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

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


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

    1. Initial program 3.8%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in3.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub3.8%

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re} + 0.25 \cdot \frac{{im}^{4}}{{re}^{3}}}} \]
    5. Step-by-step derivation
      1. +-commutative32.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg32.4%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. associate-*r/32.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{0.25 \cdot {im}^{4}}{{re}^{3}}} - \frac{{im}^{2}}{re}} \]
      5. unpow232.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*31.9%

        \[\leadsto 0.5 \cdot \sqrt{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified31.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{0.25 \cdot {im}^{4}}{{re}^{3}} - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 39.9%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. mul-1-neg39.9%

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-/l*44.5%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    9. Simplified44.5%

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

    if -1.26e92 < re < 1.59999999999999988e-33

    1. Initial program 51.5%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub51.5%

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

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

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

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

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

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

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

    if 1.59999999999999988e-33 < re

    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. sqr-neg43.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in43.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub43.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg43.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*77.9%

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identity77.9%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified77.9%

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

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

Alternative 4: 63.6% accurate, 2.0× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 8.2e-33

    1. Initial program 39.9%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in39.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub39.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg39.9%

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

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

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

    if 8.2e-33 < re

    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. sqr-neg43.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in43.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub43.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg43.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*77.9%

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identity77.9%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified77.9%

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

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

Alternative 5: 64.4% accurate, 2.0× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 5.4999999999999997e-35

    1. Initial program 39.9%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in39.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub39.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg39.9%

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

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

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

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

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

    if 5.4999999999999997e-35 < re

    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. sqr-neg43.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + \color{blue}{im \cdot im}}\right)} \]
      4. distribute-rgt-in43.9%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
      5. cancel-sign-sub43.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{re \cdot re + im \cdot im}\right)\right)\right)}} \]
      8. remove-double-neg43.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*77.9%

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identity77.9%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified77.9%

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

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

Alternative 6: 26.0% accurate, 2.1× speedup?

\[\begin{array}{l} im = |im|\\ \\ \sqrt{re} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (sqrt re))
im = abs(im);
double code(double re, double im) {
	return sqrt(re);
}
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 = sqrt(re)
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return Math.sqrt(re);
}
im = abs(im)
def code(re, im):
	return math.sqrt(re)
im = abs(im)
function code(re, im)
	return sqrt(re)
end
im = abs(im)
function tmp = code(re, im)
	tmp = sqrt(re);
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\sqrt{re}
\end{array}
Derivation
  1. Initial program 41.0%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{re \cdot re + im \cdot im} \cdot 2}} \]
    5. cancel-sign-sub41.0%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    4. associate-*r*28.1%

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

      \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
    6. *-lft-identity28.1%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  6. Simplified28.1%

    \[\leadsto \color{blue}{\sqrt{re}} \]
  7. Final simplification28.1%

    \[\leadsto \sqrt{re} \]

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 2023277 
(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)))))