math.sqrt on complex, real part

Percentage Accurate: 41.7% → 84.0%
Time: 8.1s
Alternatives: 9
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 9 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.7% 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: 84.0% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\ \mathbf{if}\;re \leq -1.26 \cdot 10^{+128}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{t_0}{re}, t_0\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
 (let* ((t_0 (pow (* (pow (* im im) 0.25) (pow (/ -1.0 re) 0.25)) 2.0)))
   (if (<= re -1.26e+128)
     (* 0.5 (fma -0.125 (* (/ (* im im) re) (/ t_0 re)) t_0))
     (sqrt (* 0.5 (+ re (hypot re im)))))))
double code(double re, double im) {
	double t_0 = pow((pow((im * im), 0.25) * pow((-1.0 / re), 0.25)), 2.0);
	double tmp;
	if (re <= -1.26e+128) {
		tmp = 0.5 * fma(-0.125, (((im * im) / re) * (t_0 / re)), t_0);
	} else {
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64((Float64(im * im) ^ 0.25) * (Float64(-1.0 / re) ^ 0.25)) ^ 2.0
	tmp = 0.0
	if (re <= -1.26e+128)
		tmp = Float64(0.5 * fma(-0.125, Float64(Float64(Float64(im * im) / re) * Float64(t_0 / re)), t_0));
	else
		tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im))));
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[Power[N[(N[Power[N[(im * im), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[N[(-1.0 / re), $MachinePrecision], 0.25], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[re, -1.26e+128], N[(0.5 * N[(-0.125 * N[(N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision] * N[(t$95$0 / re), $MachinePrecision]), $MachinePrecision] + t$95$0), $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}
t_0 := {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\
\mathbf{if}\;re \leq -1.26 \cdot 10^{+128}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(-0.125, \frac{im \cdot im}{re} \cdot \frac{t_0}{re}, t_0\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.26000000000000009e128

    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. +-commutative3.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.26000000000000009e128 < re

    1. Initial program 50.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 84.3% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.95 \cdot 10^{+101}:\\ \;\;\;\;0.5 \cdot {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\ \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.95e+101)
   (* 0.5 (pow (* (pow (* im im) 0.25) (pow (/ -1.0 re) 0.25)) 2.0))
   (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.95e+101) {
		tmp = 0.5 * pow((pow((im * im), 0.25) * pow((-1.0 / re), 0.25)), 2.0);
	} else {
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.95e+101) {
		tmp = 0.5 * Math.pow((Math.pow((im * im), 0.25) * Math.pow((-1.0 / re), 0.25)), 2.0);
	} else {
		tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.95e+101:
		tmp = 0.5 * math.pow((math.pow((im * im), 0.25) * math.pow((-1.0 / re), 0.25)), 2.0)
	else:
		tmp = math.sqrt((0.5 * (re + math.hypot(re, im))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.95e+101)
		tmp = Float64(0.5 * (Float64((Float64(im * im) ^ 0.25) * (Float64(-1.0 / re) ^ 0.25)) ^ 2.0));
	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.95e+101)
		tmp = 0.5 * ((((im * im) ^ 0.25) * ((-1.0 / re) ^ 0.25)) ^ 2.0);
	else
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.95e+101], N[(0.5 * N[Power[N[(N[Power[N[(im * im), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[N[(-1.0 / re), $MachinePrecision], 0.25], $MachinePrecision]), $MachinePrecision], 2.0], $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.95 \cdot 10^{+101}:\\
\;\;\;\;0.5 \cdot {\left({\left(im \cdot im\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right)}^{2}\\

\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.95e101

    1. Initial program 8.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot {\color{blue}{\left(e^{\log \left({im}^{2}\right) \cdot 0.25} \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.25}\right)}}^{2} \]
      3. exp-to-pow80.7%

        \[\leadsto 0.5 \cdot {\left(\color{blue}{{\left({im}^{2}\right)}^{0.25}} \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.25}\right)}^{2} \]
      4. unpow280.7%

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

        \[\leadsto 0.5 \cdot {\left({\left(im \cdot im\right)}^{0.25} \cdot \color{blue}{{\left(\frac{-1}{re}\right)}^{0.25}}\right)}^{2} \]
    8. Simplified84.7%

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

    if -1.95e101 < re

    1. Initial program 49.7%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\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*91.2%

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

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

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

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

Alternative 3: 82.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{+99}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \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 -5e+99)
   (* 0.5 (sqrt (* (- im) (/ im re))))
   (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
	double tmp;
	if (re <= -5e+99) {
		tmp = 0.5 * sqrt((-im * (im / re)));
	} else {
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if (re <= -5e+99) {
		tmp = 0.5 * Math.sqrt((-im * (im / re)));
	} else {
		tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -5e+99:
		tmp = 0.5 * math.sqrt((-im * (im / re)))
	else:
		tmp = math.sqrt((0.5 * (re + math.hypot(re, im))))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -5e+99)
		tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re))));
	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 <= -5e+99)
		tmp = 0.5 * sqrt((-im * (im / re)));
	else
		tmp = sqrt((0.5 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -5e+99], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $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 -5 \cdot 10^{+99}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\

\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 < -5.00000000000000008e99

    1. Initial program 8.5%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    5. Step-by-step derivation
      1. associate-*r/71.7%

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{-{im}^{2}}}{re}} \]
      3. unpow271.7%

        \[\leadsto 0.5 \cdot \sqrt{\frac{-\color{blue}{im \cdot im}}{re}} \]
      4. distribute-rgt-neg-in71.7%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}} \]
      4. distribute-rgt-neg-in72.5%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}} \]
      5. distribute-neg-frac72.5%

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

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

    if -5.00000000000000008e99 < re

    1. Initial program 49.7%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\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*91.2%

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

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

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

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

Alternative 4: 57.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;im \leq -4.5 \cdot 10^{+16}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.55 \cdot 10^{-38}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq -1.6 \cdot 10^{-120}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -8.5 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{-264}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* im -2.0)))))
   (if (<= im -4.5e+16)
     t_0
     (if (<= im -1.55e-38)
       (sqrt re)
       (if (<= im -1.6e-120)
         t_0
         (if (<= im -8.5e-287)
           (sqrt re)
           (if (<= im 1.05e-264)
             (* 0.5 (/ im (sqrt (- re))))
             (if (<= im 5.5e-153) (sqrt re) (* 0.5 (sqrt (* im 2.0)))))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((im * -2.0));
	double tmp;
	if (im <= -4.5e+16) {
		tmp = t_0;
	} else if (im <= -1.55e-38) {
		tmp = sqrt(re);
	} else if (im <= -1.6e-120) {
		tmp = t_0;
	} else if (im <= -8.5e-287) {
		tmp = sqrt(re);
	} else if (im <= 1.05e-264) {
		tmp = 0.5 * (im / sqrt(-re));
	} else if (im <= 5.5e-153) {
		tmp = sqrt(re);
	} else {
		tmp = 0.5 * sqrt((im * 2.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 = 0.5d0 * sqrt((im * (-2.0d0)))
    if (im <= (-4.5d+16)) then
        tmp = t_0
    else if (im <= (-1.55d-38)) then
        tmp = sqrt(re)
    else if (im <= (-1.6d-120)) then
        tmp = t_0
    else if (im <= (-8.5d-287)) then
        tmp = sqrt(re)
    else if (im <= 1.05d-264) then
        tmp = 0.5d0 * (im / sqrt(-re))
    else if (im <= 5.5d-153) then
        tmp = sqrt(re)
    else
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((im * -2.0));
	double tmp;
	if (im <= -4.5e+16) {
		tmp = t_0;
	} else if (im <= -1.55e-38) {
		tmp = Math.sqrt(re);
	} else if (im <= -1.6e-120) {
		tmp = t_0;
	} else if (im <= -8.5e-287) {
		tmp = Math.sqrt(re);
	} else if (im <= 1.05e-264) {
		tmp = 0.5 * (im / Math.sqrt(-re));
	} else if (im <= 5.5e-153) {
		tmp = Math.sqrt(re);
	} else {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((im * -2.0))
	tmp = 0
	if im <= -4.5e+16:
		tmp = t_0
	elif im <= -1.55e-38:
		tmp = math.sqrt(re)
	elif im <= -1.6e-120:
		tmp = t_0
	elif im <= -8.5e-287:
		tmp = math.sqrt(re)
	elif im <= 1.05e-264:
		tmp = 0.5 * (im / math.sqrt(-re))
	elif im <= 5.5e-153:
		tmp = math.sqrt(re)
	else:
		tmp = 0.5 * math.sqrt((im * 2.0))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(im * -2.0)))
	tmp = 0.0
	if (im <= -4.5e+16)
		tmp = t_0;
	elseif (im <= -1.55e-38)
		tmp = sqrt(re);
	elseif (im <= -1.6e-120)
		tmp = t_0;
	elseif (im <= -8.5e-287)
		tmp = sqrt(re);
	elseif (im <= 1.05e-264)
		tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re))));
	elseif (im <= 5.5e-153)
		tmp = sqrt(re);
	else
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((im * -2.0));
	tmp = 0.0;
	if (im <= -4.5e+16)
		tmp = t_0;
	elseif (im <= -1.55e-38)
		tmp = sqrt(re);
	elseif (im <= -1.6e-120)
		tmp = t_0;
	elseif (im <= -8.5e-287)
		tmp = sqrt(re);
	elseif (im <= 1.05e-264)
		tmp = 0.5 * (im / sqrt(-re));
	elseif (im <= 5.5e-153)
		tmp = sqrt(re);
	else
		tmp = 0.5 * sqrt((im * 2.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -4.5e+16], t$95$0, If[LessEqual[im, -1.55e-38], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, -1.6e-120], t$95$0, If[LessEqual[im, -8.5e-287], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 1.05e-264], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5.5e-153], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{if}\;im \leq -4.5 \cdot 10^{+16}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;im \leq -1.55 \cdot 10^{-38}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq -1.6 \cdot 10^{-120}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;im \leq -8.5 \cdot 10^{-287}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq 1.05 \cdot 10^{-264}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\

\mathbf{elif}\;im \leq 5.5 \cdot 10^{-153}:\\
\;\;\;\;\sqrt{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if im < -4.5e16 or -1.54999999999999991e-38 < im < -1.6e-120

    1. Initial program 38.5%

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

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

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

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

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

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

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

    if -4.5e16 < im < -1.54999999999999991e-38 or -1.6e-120 < im < -8.5000000000000003e-287 or 1.0500000000000001e-264 < im < 5.49999999999999962e-153

    1. Initial program 47.3%

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

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

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

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

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

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

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

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

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

    if -8.5000000000000003e-287 < im < 1.0500000000000001e-264

    1. Initial program 12.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    5. Step-by-step derivation
      1. associate-*r/70.5%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{-\color{blue}{im \cdot im}}{re}} \]
      4. distribute-rgt-neg-in70.5%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{-im \cdot \left(-im\right)}}{\sqrt{-re}}} \]
      3. distribute-rgt-neg-out70.1%

        \[\leadsto 0.5 \cdot \frac{\sqrt{-\color{blue}{\left(-im \cdot im\right)}}}{\sqrt{-re}} \]
      4. remove-double-neg70.1%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{-re}} \]
      6. add-sqr-sqrt80.6%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{-re}} \]
    8. Applied egg-rr80.6%

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

    if 5.49999999999999962e-153 < im

    1. Initial program 47.0%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -4.5 \cdot 10^{+16}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq -1.55 \cdot 10^{-38}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq -1.6 \cdot 10^{-120}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq -8.5 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{-264}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]

Alternative 5: 59.8% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -1.16 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{-265}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\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.16e-121)
   (* 0.5 (sqrt (* 2.0 (- re im))))
   (if (<= im -8e-287)
     (sqrt re)
     (if (<= im 1.5e-265)
       (* 0.5 (/ im (sqrt (- re))))
       (if (<= im 5.8e-153) (sqrt re) (* 0.5 (sqrt (* 2.0 (+ re im)))))))))
double code(double re, double im) {
	double tmp;
	if (im <= -1.16e-121) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else if (im <= -8e-287) {
		tmp = sqrt(re);
	} else if (im <= 1.5e-265) {
		tmp = 0.5 * (im / sqrt(-re));
	} else if (im <= 5.8e-153) {
		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.16d-121)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else if (im <= (-8d-287)) then
        tmp = sqrt(re)
    else if (im <= 1.5d-265) then
        tmp = 0.5d0 * (im / sqrt(-re))
    else if (im <= 5.8d-153) 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.16e-121) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else if (im <= -8e-287) {
		tmp = Math.sqrt(re);
	} else if (im <= 1.5e-265) {
		tmp = 0.5 * (im / Math.sqrt(-re));
	} else if (im <= 5.8e-153) {
		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.16e-121:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	elif im <= -8e-287:
		tmp = math.sqrt(re)
	elif im <= 1.5e-265:
		tmp = 0.5 * (im / math.sqrt(-re))
	elif im <= 5.8e-153:
		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.16e-121)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	elseif (im <= -8e-287)
		tmp = sqrt(re);
	elseif (im <= 1.5e-265)
		tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re))));
	elseif (im <= 5.8e-153)
		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.16e-121)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	elseif (im <= -8e-287)
		tmp = sqrt(re);
	elseif (im <= 1.5e-265)
		tmp = 0.5 * (im / sqrt(-re));
	elseif (im <= 5.8e-153)
		tmp = sqrt(re);
	else
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -1.16e-121], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, -8e-287], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, 1.5e-265], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 5.8e-153], 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.16 \cdot 10^{-121}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq 1.5 \cdot 10^{-265}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\

\mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\
\;\;\;\;\sqrt{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if im < -1.1600000000000001e-121

    1. Initial program 42.6%

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

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

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

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

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

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

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

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

    if -1.1600000000000001e-121 < im < -8.00000000000000017e-287 or 1.4999999999999999e-265 < im < 5.80000000000000004e-153

    1. Initial program 42.3%

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

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

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

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

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

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

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

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

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

    if -8.00000000000000017e-287 < im < 1.4999999999999999e-265

    1. Initial program 12.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    5. Step-by-step derivation
      1. associate-*r/70.5%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{-\color{blue}{im \cdot im}}{re}} \]
      4. distribute-rgt-neg-in70.5%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{-im \cdot \left(-im\right)}}{\sqrt{-re}}} \]
      3. distribute-rgt-neg-out70.1%

        \[\leadsto 0.5 \cdot \frac{\sqrt{-\color{blue}{\left(-im \cdot im\right)}}}{\sqrt{-re}} \]
      4. remove-double-neg70.1%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{-re}} \]
      6. add-sqr-sqrt80.6%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{-re}} \]
    8. Applied egg-rr80.6%

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

    if 5.80000000000000004e-153 < im

    1. Initial program 47.0%

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -1.16 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 1.5 \cdot 10^{-265}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 6: 58.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;im \leq -5.2 \cdot 10^{+17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6 \cdot 10^{-41}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq -5.5 \cdot 10^{-122}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* im -2.0)))))
   (if (<= im -5.2e+17)
     t_0
     (if (<= im -6e-41)
       (sqrt re)
       (if (<= im -5.5e-122)
         t_0
         (if (<= im 5.8e-153) (sqrt re) (* 0.5 (sqrt (* im 2.0)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((im * -2.0));
	double tmp;
	if (im <= -5.2e+17) {
		tmp = t_0;
	} else if (im <= -6e-41) {
		tmp = sqrt(re);
	} else if (im <= -5.5e-122) {
		tmp = t_0;
	} else if (im <= 5.8e-153) {
		tmp = sqrt(re);
	} else {
		tmp = 0.5 * sqrt((im * 2.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 = 0.5d0 * sqrt((im * (-2.0d0)))
    if (im <= (-5.2d+17)) then
        tmp = t_0
    else if (im <= (-6d-41)) then
        tmp = sqrt(re)
    else if (im <= (-5.5d-122)) then
        tmp = t_0
    else if (im <= 5.8d-153) then
        tmp = sqrt(re)
    else
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((im * -2.0));
	double tmp;
	if (im <= -5.2e+17) {
		tmp = t_0;
	} else if (im <= -6e-41) {
		tmp = Math.sqrt(re);
	} else if (im <= -5.5e-122) {
		tmp = t_0;
	} else if (im <= 5.8e-153) {
		tmp = Math.sqrt(re);
	} else {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((im * -2.0))
	tmp = 0
	if im <= -5.2e+17:
		tmp = t_0
	elif im <= -6e-41:
		tmp = math.sqrt(re)
	elif im <= -5.5e-122:
		tmp = t_0
	elif im <= 5.8e-153:
		tmp = math.sqrt(re)
	else:
		tmp = 0.5 * math.sqrt((im * 2.0))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(im * -2.0)))
	tmp = 0.0
	if (im <= -5.2e+17)
		tmp = t_0;
	elseif (im <= -6e-41)
		tmp = sqrt(re);
	elseif (im <= -5.5e-122)
		tmp = t_0;
	elseif (im <= 5.8e-153)
		tmp = sqrt(re);
	else
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((im * -2.0));
	tmp = 0.0;
	if (im <= -5.2e+17)
		tmp = t_0;
	elseif (im <= -6e-41)
		tmp = sqrt(re);
	elseif (im <= -5.5e-122)
		tmp = t_0;
	elseif (im <= 5.8e-153)
		tmp = sqrt(re);
	else
		tmp = 0.5 * sqrt((im * 2.0));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -5.2e+17], t$95$0, If[LessEqual[im, -6e-41], N[Sqrt[re], $MachinePrecision], If[LessEqual[im, -5.5e-122], t$95$0, If[LessEqual[im, 5.8e-153], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{if}\;im \leq -5.2 \cdot 10^{+17}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;im \leq -6 \cdot 10^{-41}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq -5.5 \cdot 10^{-122}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\
\;\;\;\;\sqrt{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -5.2e17 or -5.99999999999999978e-41 < im < -5.50000000000000053e-122

    1. Initial program 38.5%

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

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

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

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

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

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

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

    if -5.2e17 < im < -5.99999999999999978e-41 or -5.50000000000000053e-122 < im < 5.80000000000000004e-153

    1. Initial program 42.7%

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{re}} \]
    6. Simplified51.7%

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

    if 5.80000000000000004e-153 < im

    1. Initial program 47.0%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -5.2 \cdot 10^{+17}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq -6 \cdot 10^{-41}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq -5.5 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]

Alternative 7: 59.3% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\
\;\;\;\;\sqrt{re}\\

\mathbf{elif}\;im \leq 2.3 \cdot 10^{-263}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\

\mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\
\;\;\;\;\sqrt{re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if im < -2.35e-122

    1. Initial program 42.6%

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

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

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

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

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

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

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

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

    if -2.35e-122 < im < -8.00000000000000017e-287 or 2.30000000000000003e-263 < im < 5.80000000000000004e-153

    1. Initial program 42.3%

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

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

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

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

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

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

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

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

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

    if -8.00000000000000017e-287 < im < 2.30000000000000003e-263

    1. Initial program 12.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    5. Step-by-step derivation
      1. associate-*r/70.5%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{-\color{blue}{im \cdot im}}{re}} \]
      4. distribute-rgt-neg-in70.5%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{-im \cdot \left(-im\right)}}{\sqrt{-re}}} \]
      3. distribute-rgt-neg-out70.1%

        \[\leadsto 0.5 \cdot \frac{\sqrt{-\color{blue}{\left(-im \cdot im\right)}}}{\sqrt{-re}} \]
      4. remove-double-neg70.1%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{-re}} \]
      6. add-sqr-sqrt80.6%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{-re}} \]
    8. Applied egg-rr80.6%

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

    if 5.80000000000000004e-153 < im

    1. Initial program 47.0%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -2.35 \cdot 10^{-122}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -8 \cdot 10^{-287}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{-263}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-153}:\\ \;\;\;\;\sqrt{re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]

Alternative 8: 42.6% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 4.5 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re 4.5e-96) (* 0.5 (sqrt (* im -2.0))) (sqrt re)))
double code(double re, double im) {
	double tmp;
	if (re <= 4.5e-96) {
		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 (re <= 4.5d-96) 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 (re <= 4.5e-96) {
		tmp = 0.5 * Math.sqrt((im * -2.0));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= 4.5e-96:
		tmp = 0.5 * math.sqrt((im * -2.0))
	else:
		tmp = math.sqrt(re)
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= 4.5e-96)
		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 (re <= 4.5e-96)
		tmp = 0.5 * sqrt((im * -2.0));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, 4.5e-96], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 4.5 \cdot 10^{-96}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\

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


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

    1. Initial program 42.4%

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

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

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

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

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

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

    if 4.5e-96 < re

    1. Initial program 44.4%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 9: 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 43.1%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \sqrt{re} \]

Developer target: 48.8% 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 2023178 
(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)))))