math.sqrt on complex, imaginary part, im greater than 0 branch

Percentage Accurate: 41.2% → 90.3%
Time: 8.1s
Alternatives: 7
Speedup: 2.1×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 7 alternatives:

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

Initial Program: 41.2% accurate, 1.0× speedup?

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

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

Alternative 1: 90.3% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\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 6.0%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg6.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. sqr-neg6.0%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    5. Step-by-step derivation
      1. unpow248.6%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \frac{1}{\frac{re}{im}}}} \]
      2. clear-num60.7%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      3. *-un-lft-identity60.7%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{1 \cdot im}}{re}} \]
      4. add-sqr-sqrt60.5%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \left(\color{blue}{\sqrt{\frac{1}{re}}} \cdot \frac{im}{\sqrt{re}}\right)} \]
      8. un-div-inv60.6%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \left(\sqrt{\frac{1}{re}} \cdot \color{blue}{\left(im \cdot \frac{1}{\sqrt{re}}\right)}\right)} \]
      9. metadata-eval60.6%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im \cdot \sqrt{\frac{1}{re}}} \cdot \sqrt{im \cdot \sqrt{\frac{1}{re}}}\right)} \]
      13. add-sqr-sqrt91.7%

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

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

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right)} - 1\right) \]
      17. metadata-eval23.8%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right)} - 1\right) \]
      18. un-div-inv23.8%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    10. Simplified91.8%

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

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

    1. Initial program 42.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 76.1% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := \sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -9.2 \cdot 10^{+103}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 0.016:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))) (t_1 (sqrt (* 0.5 (- im re)))))
   (if (<= re -1.8e+152)
     t_0
     (if (<= re -9.2e+103)
       t_1
       (if (<= re -2.9e+41)
         t_0
         (if (<= re 0.016) t_1 (* (pow re -0.5) (* im 0.5))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -9.2e+103) {
		tmp = t_1;
	} else if (re <= -2.9e+41) {
		tmp = t_0;
	} else if (re <= 0.016) {
		tmp = t_1;
	} else {
		tmp = pow(re, -0.5) * (im * 0.5);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = sqrt((0.5d0 * (im - re)))
    if (re <= (-1.8d+152)) then
        tmp = t_0
    else if (re <= (-9.2d+103)) then
        tmp = t_1
    else if (re <= (-2.9d+41)) then
        tmp = t_0
    else if (re <= 0.016d0) then
        tmp = t_1
    else
        tmp = (re ** (-0.5d0)) * (im * 0.5d0)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = Math.sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -9.2e+103) {
		tmp = t_1;
	} else if (re <= -2.9e+41) {
		tmp = t_0;
	} else if (re <= 0.016) {
		tmp = t_1;
	} else {
		tmp = Math.pow(re, -0.5) * (im * 0.5);
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = math.sqrt((0.5 * (im - re)))
	tmp = 0
	if re <= -1.8e+152:
		tmp = t_0
	elif re <= -9.2e+103:
		tmp = t_1
	elif re <= -2.9e+41:
		tmp = t_0
	elif re <= 0.016:
		tmp = t_1
	else:
		tmp = math.pow(re, -0.5) * (im * 0.5)
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = sqrt(Float64(0.5 * Float64(im - re)))
	tmp = 0.0
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -9.2e+103)
		tmp = t_1;
	elseif (re <= -2.9e+41)
		tmp = t_0;
	elseif (re <= 0.016)
		tmp = t_1;
	else
		tmp = Float64((re ^ -0.5) * Float64(im * 0.5));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = sqrt((0.5 * (im - re)));
	tmp = 0.0;
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -9.2e+103)
		tmp = t_1;
	elseif (re <= -2.9e+41)
		tmp = t_0;
	elseif (re <= 0.016)
		tmp = t_1;
	else
		tmp = (re ^ -0.5) * (im * 0.5);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[re, -1.8e+152], t$95$0, If[LessEqual[re, -9.2e+103], t$95$1, If[LessEqual[re, -2.9e+41], t$95$0, If[LessEqual[re, 0.016], t$95$1, N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
t_1 := \sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -9.2 \cdot 10^{+103}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 0.016:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -1.7999999999999999e152 or -9.20000000000000034e103 < re < -2.89999999999999988e41

    1. Initial program 23.4%

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Step-by-step derivation
      1. *-commutative91.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified91.8%

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

    if -1.7999999999999999e152 < re < -9.20000000000000034e103 or -2.89999999999999988e41 < re < 0.016

    1. Initial program 51.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt88.8%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt89.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 75.9%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-175.9%

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

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

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

    if 0.016 < re

    1. Initial program 13.9%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg13.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. sqr-neg13.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified59.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    7. Step-by-step derivation
      1. add-sqr-sqrt59.1%

        \[\leadsto \color{blue}{\sqrt{0.5 \cdot \sqrt{\frac{im}{\frac{re}{im}}}} \cdot \sqrt{0.5 \cdot \sqrt{\frac{im}{\frac{re}{im}}}}} \]
      2. pow259.1%

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

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

        \[\leadsto \color{blue}{\sqrt{0.5 \cdot \frac{im}{\sqrt{re}}} \cdot \sqrt{0.5 \cdot \frac{im}{\sqrt{re}}}} \]
      2. add-sqr-sqrt76.3%

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

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      4. *-commutative76.3%

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

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

        \[\leadsto \color{blue}{\frac{1}{\sqrt{re}} \cdot \left(im \cdot 0.5\right)} \]
      7. pow1/276.2%

        \[\leadsto \frac{1}{\color{blue}{{re}^{0.5}}} \cdot \left(im \cdot 0.5\right) \]
      8. pow-flip76.3%

        \[\leadsto \color{blue}{{re}^{\left(-0.5\right)}} \cdot \left(im \cdot 0.5\right) \]
      9. metadata-eval76.3%

        \[\leadsto {re}^{\color{blue}{-0.5}} \cdot \left(im \cdot 0.5\right) \]
    10. Applied egg-rr76.3%

      \[\leadsto \color{blue}{{re}^{-0.5} \cdot \left(im \cdot 0.5\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification78.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -9.2 \cdot 10^{+103}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 0.016:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]

Alternative 3: 76.1% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := \sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.45 \cdot 10^{+103}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 8 \cdot 10^{-5}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))) (t_1 (sqrt (* 0.5 (- im re)))))
   (if (<= re -1.8e+152)
     t_0
     (if (<= re -1.45e+103)
       t_1
       (if (<= re -2.9e+41)
         t_0
         (if (<= re 8e-5) t_1 (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -1.45e+103) {
		tmp = t_1;
	} else if (re <= -2.9e+41) {
		tmp = t_0;
	} else if (re <= 8e-5) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im / sqrt(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) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = sqrt((0.5d0 * (im - re)))
    if (re <= (-1.8d+152)) then
        tmp = t_0
    else if (re <= (-1.45d+103)) then
        tmp = t_1
    else if (re <= (-2.9d+41)) then
        tmp = t_0
    else if (re <= 8d-5) then
        tmp = t_1
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = Math.sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -1.45e+103) {
		tmp = t_1;
	} else if (re <= -2.9e+41) {
		tmp = t_0;
	} else if (re <= 8e-5) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = math.sqrt((0.5 * (im - re)))
	tmp = 0
	if re <= -1.8e+152:
		tmp = t_0
	elif re <= -1.45e+103:
		tmp = t_1
	elif re <= -2.9e+41:
		tmp = t_0
	elif re <= 8e-5:
		tmp = t_1
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = sqrt(Float64(0.5 * Float64(im - re)))
	tmp = 0.0
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -1.45e+103)
		tmp = t_1;
	elseif (re <= -2.9e+41)
		tmp = t_0;
	elseif (re <= 8e-5)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = sqrt((0.5 * (im - re)));
	tmp = 0.0;
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -1.45e+103)
		tmp = t_1;
	elseif (re <= -2.9e+41)
		tmp = t_0;
	elseif (re <= 8e-5)
		tmp = t_1;
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[re, -1.8e+152], t$95$0, If[LessEqual[re, -1.45e+103], t$95$1, If[LessEqual[re, -2.9e+41], t$95$0, If[LessEqual[re, 8e-5], t$95$1, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
t_1 := \sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -1.45 \cdot 10^{+103}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 8 \cdot 10^{-5}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -1.7999999999999999e152 or -1.4499999999999999e103 < re < -2.89999999999999988e41

    1. Initial program 23.4%

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Step-by-step derivation
      1. *-commutative91.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified91.8%

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

    if -1.7999999999999999e152 < re < -1.4499999999999999e103 or -2.89999999999999988e41 < re < 8.00000000000000065e-5

    1. Initial program 51.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt88.8%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt89.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 75.9%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-175.9%

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

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

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

    if 8.00000000000000065e-5 < re

    1. Initial program 13.9%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg13.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. sqr-neg13.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified59.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    7. Step-by-step derivation
      1. div-inv59.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \frac{1}{\frac{re}{im}}}} \]
      2. clear-num59.4%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      3. *-un-lft-identity59.4%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{1 \cdot im}}{re}} \]
      4. add-sqr-sqrt59.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \left(\color{blue}{\sqrt{\frac{1}{re}}} \cdot \frac{im}{\sqrt{re}}\right)} \]
      8. un-div-inv59.4%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \left(\sqrt{\frac{1}{re}} \cdot \color{blue}{\left(im \cdot \frac{1}{\sqrt{re}}\right)}\right)} \]
      9. metadata-eval59.4%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right)} - 1\right) \]
      18. un-div-inv36.7%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.45 \cdot 10^{+103}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 8 \cdot 10^{-5}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 4: 76.1% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := \sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.36 \cdot 10^{+104}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 0.00038:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))) (t_1 (sqrt (* 0.5 (- im re)))))
   (if (<= re -1.8e+152)
     t_0
     (if (<= re -1.36e+104)
       t_1
       (if (<= re -2.9e+41)
         t_0
         (if (<= re 0.00038) t_1 (/ (* im 0.5) (sqrt re))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -1.36e+104) {
		tmp = t_1;
	} else if (re <= -2.9e+41) {
		tmp = t_0;
	} else if (re <= 0.00038) {
		tmp = t_1;
	} else {
		tmp = (im * 0.5) / sqrt(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) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = sqrt((0.5d0 * (im - re)))
    if (re <= (-1.8d+152)) then
        tmp = t_0
    else if (re <= (-1.36d+104)) then
        tmp = t_1
    else if (re <= (-2.9d+41)) then
        tmp = t_0
    else if (re <= 0.00038d0) then
        tmp = t_1
    else
        tmp = (im * 0.5d0) / sqrt(re)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = Math.sqrt((0.5 * (im - re)));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -1.36e+104) {
		tmp = t_1;
	} else if (re <= -2.9e+41) {
		tmp = t_0;
	} else if (re <= 0.00038) {
		tmp = t_1;
	} else {
		tmp = (im * 0.5) / Math.sqrt(re);
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = math.sqrt((0.5 * (im - re)))
	tmp = 0
	if re <= -1.8e+152:
		tmp = t_0
	elif re <= -1.36e+104:
		tmp = t_1
	elif re <= -2.9e+41:
		tmp = t_0
	elif re <= 0.00038:
		tmp = t_1
	else:
		tmp = (im * 0.5) / math.sqrt(re)
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = sqrt(Float64(0.5 * Float64(im - re)))
	tmp = 0.0
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -1.36e+104)
		tmp = t_1;
	elseif (re <= -2.9e+41)
		tmp = t_0;
	elseif (re <= 0.00038)
		tmp = t_1;
	else
		tmp = Float64(Float64(im * 0.5) / sqrt(re));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = sqrt((0.5 * (im - re)));
	tmp = 0.0;
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -1.36e+104)
		tmp = t_1;
	elseif (re <= -2.9e+41)
		tmp = t_0;
	elseif (re <= 0.00038)
		tmp = t_1;
	else
		tmp = (im * 0.5) / sqrt(re);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[re, -1.8e+152], t$95$0, If[LessEqual[re, -1.36e+104], t$95$1, If[LessEqual[re, -2.9e+41], t$95$0, If[LessEqual[re, 0.00038], t$95$1, N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
t_1 := \sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -1.36 \cdot 10^{+104}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 0.00038:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -1.7999999999999999e152 or -1.3599999999999999e104 < re < -2.89999999999999988e41

    1. Initial program 23.4%

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Step-by-step derivation
      1. *-commutative91.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified91.8%

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

    if -1.7999999999999999e152 < re < -1.3599999999999999e104 or -2.89999999999999988e41 < re < 3.8000000000000002e-4

    1. Initial program 51.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt88.8%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      9. add-sqr-sqrt89.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 75.9%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-175.9%

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

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

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

    if 3.8000000000000002e-4 < re

    1. Initial program 13.9%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg13.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. sqr-neg13.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified59.3%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    7. Step-by-step derivation
      1. div-inv59.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \frac{1}{\frac{re}{im}}}} \]
      2. clear-num59.4%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
      3. *-un-lft-identity59.4%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{\color{blue}{1 \cdot im}}{re}} \]
      4. add-sqr-sqrt59.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \left(\color{blue}{\sqrt{\frac{1}{re}}} \cdot \frac{im}{\sqrt{re}}\right)} \]
      8. un-div-inv59.4%

        \[\leadsto 0.5 \cdot \sqrt{im \cdot \left(\sqrt{\frac{1}{re}} \cdot \color{blue}{\left(im \cdot \frac{1}{\sqrt{re}}\right)}\right)} \]
      9. metadata-eval59.4%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(im \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right)} - 1\right) \]
      18. un-div-inv36.7%

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

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

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

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

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

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. *-commutative76.3%

        \[\leadsto \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
    12. Applied egg-rr76.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.36 \cdot 10^{+104}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 0.00038:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]

Alternative 5: 63.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.8 \cdot 10^{+104}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -4.75 \cdot 10^{+33}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))))
   (if (<= re -1.8e+152)
     t_0
     (if (<= re -1.8e+104)
       (sqrt (* 0.5 (- im re)))
       (if (<= re -4.75e+33) t_0 (sqrt (* im 0.5)))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -1.8e+104) {
		tmp = sqrt((0.5 * (im - re)));
	} else if (re <= -4.75e+33) {
		tmp = t_0;
	} else {
		tmp = sqrt((im * 0.5));
	}
	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((re * (-4.0d0)))
    if (re <= (-1.8d+152)) then
        tmp = t_0
    else if (re <= (-1.8d+104)) then
        tmp = sqrt((0.5d0 * (im - re)))
    else if (re <= (-4.75d+33)) then
        tmp = t_0
    else
        tmp = sqrt((im * 0.5d0))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double tmp;
	if (re <= -1.8e+152) {
		tmp = t_0;
	} else if (re <= -1.8e+104) {
		tmp = Math.sqrt((0.5 * (im - re)));
	} else if (re <= -4.75e+33) {
		tmp = t_0;
	} else {
		tmp = Math.sqrt((im * 0.5));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	tmp = 0
	if re <= -1.8e+152:
		tmp = t_0
	elif re <= -1.8e+104:
		tmp = math.sqrt((0.5 * (im - re)))
	elif re <= -4.75e+33:
		tmp = t_0
	else:
		tmp = math.sqrt((im * 0.5))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	tmp = 0.0
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -1.8e+104)
		tmp = sqrt(Float64(0.5 * Float64(im - re)));
	elseif (re <= -4.75e+33)
		tmp = t_0;
	else
		tmp = sqrt(Float64(im * 0.5));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	tmp = 0.0;
	if (re <= -1.8e+152)
		tmp = t_0;
	elseif (re <= -1.8e+104)
		tmp = sqrt((0.5 * (im - re)));
	elseif (re <= -4.75e+33)
		tmp = t_0;
	else
		tmp = sqrt((im * 0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1.8e+152], t$95$0, If[LessEqual[re, -1.8e+104], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, -4.75e+33], t$95$0, N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{if}\;re \leq -1.8 \cdot 10^{+152}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;re \leq -4.75 \cdot 10^{+33}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -1.7999999999999999e152 or -1.8e104 < re < -4.7500000000000002e33

    1. Initial program 28.6%

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    5. Step-by-step derivation
      1. *-commutative87.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified87.3%

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

    if -1.7999999999999999e152 < re < -1.8e104

    1. Initial program 17.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 88.7%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-188.7%

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

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

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

    if -4.7500000000000002e33 < re

    1. Initial program 38.1%

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5\right) \cdot \color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot 0.5\right)}} \]
      8. swap-sqr69.9%

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
    5. Simplified69.9%

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 58.0%

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

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

Alternative 6: 55.9% accurate, 2.0× speedup?

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

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

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


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

    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. *-commutative46.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 59.5%

      \[\leadsto \sqrt{0.5 \cdot \color{blue}{\left(im + -1 \cdot re\right)}} \]
    7. Step-by-step derivation
      1. neg-mul-159.5%

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

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

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

    if -2.60000000000000005e-303 < re

    1. Initial program 25.7%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      4. add-sqr-sqrt53.8%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    6. Taylor expanded in re around 0 49.5%

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

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

Alternative 7: 52.0% accurate, 2.1× speedup?

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

\\
\sqrt{im \cdot 0.5}
\end{array}
Derivation
  1. Initial program 35.7%

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
    9. add-sqr-sqrt76.7%

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{0.5} \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
  5. Simplified76.7%

    \[\leadsto \color{blue}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  6. Taylor expanded in re around 0 50.8%

    \[\leadsto \sqrt{0.5 \cdot \color{blue}{im}} \]
  7. Final simplification50.8%

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

Reproduce

?
herbie shell --seed 2023271 
(FPCore (re im)
  :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
  :precision binary64
  :pre (> im 0.0)
  (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))