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

Percentage Accurate: 40.9% → 90.0%
Time: 7.8s
Alternatives: 7
Speedup: 2.0×

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: 40.9% accurate, 1.0× speedup?

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

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

Alternative 1: 90.0% 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 \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \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 (pow re -0.5)))
   (* 0.5 (sqrt (* 2.0 (- (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 * pow(re, -0.5));
	} else {
		tmp = 0.5 * sqrt((2.0 * (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.pow(re, -0.5));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (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.pow(re, -0.5))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (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 * (re ^ -0.5)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * 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 * (re ^ -0.5));
	else
		tmp = 0.5 * sqrt((2.0 * (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[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \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 14.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 47.3%

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

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

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

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

Alternative 2: 75.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{if}\;re \leq -9.5 \cdot 10^{+62}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -4.8 \cdot 10^{-21}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -4.9 \cdot 10^{-39}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -5.8 \cdot 10^{-53}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{im - re} \cdot \sqrt{2}\right)\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{-36} \lor \neg \left(re \leq 1.05 \cdot 10^{+26}\right) \land re \leq 1.45 \cdot 10^{+40}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* 2.0 (- im re)))))
        (t_1 (* 0.5 (sqrt (* 2.0 (* re -2.0))))))
   (if (<= re -9.5e+62)
     t_1
     (if (<= re -4.8e-21)
       t_0
       (if (<= re -4.9e-39)
         t_1
         (if (<= re -5.8e-53)
           (* 0.5 (* (sqrt (- im re)) (sqrt 2.0)))
           (if (or (<= re 4.8e-36)
                   (and (not (<= re 1.05e+26)) (<= re 1.45e+40)))
             t_0
             (* 0.5 (/ im (sqrt re))))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((2.0 * (im - re)));
	double t_1 = 0.5 * sqrt((2.0 * (re * -2.0)));
	double tmp;
	if (re <= -9.5e+62) {
		tmp = t_1;
	} else if (re <= -4.8e-21) {
		tmp = t_0;
	} else if (re <= -4.9e-39) {
		tmp = t_1;
	} else if (re <= -5.8e-53) {
		tmp = 0.5 * (sqrt((im - re)) * sqrt(2.0));
	} else if ((re <= 4.8e-36) || (!(re <= 1.05e+26) && (re <= 1.45e+40))) {
		tmp = t_0;
	} 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((2.0d0 * (im - re)))
    t_1 = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    if (re <= (-9.5d+62)) then
        tmp = t_1
    else if (re <= (-4.8d-21)) then
        tmp = t_0
    else if (re <= (-4.9d-39)) then
        tmp = t_1
    else if (re <= (-5.8d-53)) then
        tmp = 0.5d0 * (sqrt((im - re)) * sqrt(2.0d0))
    else if ((re <= 4.8d-36) .or. (.not. (re <= 1.05d+26)) .and. (re <= 1.45d+40)) then
        tmp = t_0
    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((2.0 * (im - re)));
	double t_1 = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	double tmp;
	if (re <= -9.5e+62) {
		tmp = t_1;
	} else if (re <= -4.8e-21) {
		tmp = t_0;
	} else if (re <= -4.9e-39) {
		tmp = t_1;
	} else if (re <= -5.8e-53) {
		tmp = 0.5 * (Math.sqrt((im - re)) * Math.sqrt(2.0));
	} else if ((re <= 4.8e-36) || (!(re <= 1.05e+26) && (re <= 1.45e+40))) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((2.0 * (im - re)))
	t_1 = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	tmp = 0
	if re <= -9.5e+62:
		tmp = t_1
	elif re <= -4.8e-21:
		tmp = t_0
	elif re <= -4.9e-39:
		tmp = t_1
	elif re <= -5.8e-53:
		tmp = 0.5 * (math.sqrt((im - re)) * math.sqrt(2.0))
	elif (re <= 4.8e-36) or (not (re <= 1.05e+26) and (re <= 1.45e+40)):
		tmp = t_0
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))))
	t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))))
	tmp = 0.0
	if (re <= -9.5e+62)
		tmp = t_1;
	elseif (re <= -4.8e-21)
		tmp = t_0;
	elseif (re <= -4.9e-39)
		tmp = t_1;
	elseif (re <= -5.8e-53)
		tmp = Float64(0.5 * Float64(sqrt(Float64(im - re)) * sqrt(2.0)));
	elseif ((re <= 4.8e-36) || (!(re <= 1.05e+26) && (re <= 1.45e+40)))
		tmp = t_0;
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((2.0 * (im - re)));
	t_1 = 0.5 * sqrt((2.0 * (re * -2.0)));
	tmp = 0.0;
	if (re <= -9.5e+62)
		tmp = t_1;
	elseif (re <= -4.8e-21)
		tmp = t_0;
	elseif (re <= -4.9e-39)
		tmp = t_1;
	elseif (re <= -5.8e-53)
		tmp = 0.5 * (sqrt((im - re)) * sqrt(2.0));
	elseif ((re <= 4.8e-36) || (~((re <= 1.05e+26)) && (re <= 1.45e+40)))
		tmp = t_0;
	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[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -9.5e+62], t$95$1, If[LessEqual[re, -4.8e-21], t$95$0, If[LessEqual[re, -4.9e-39], t$95$1, If[LessEqual[re, -5.8e-53], N[(0.5 * N[(N[Sqrt[N[(im - re), $MachinePrecision]], $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 4.8e-36], And[N[Not[LessEqual[re, 1.05e+26]], $MachinePrecision], LessEqual[re, 1.45e+40]]], t$95$0, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq -4.8 \cdot 10^{-21}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -4.9 \cdot 10^{-39}:\\
\;\;\;\;t_1\\

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

\mathbf{elif}\;re \leq 4.8 \cdot 10^{-36} \lor \neg \left(re \leq 1.05 \cdot 10^{+26}\right) \land re \leq 1.45 \cdot 10^{+40}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -9.5000000000000003e62 or -4.7999999999999999e-21 < re < -4.89999999999999974e-39

    1. Initial program 38.5%

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

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

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

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

    if -9.5000000000000003e62 < re < -4.7999999999999999e-21 or -5.7999999999999996e-53 < re < 4.8e-36 or 1.05e26 < re < 1.45000000000000009e40

    1. Initial program 58.1%

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

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

    if -4.89999999999999974e-39 < re < -5.7999999999999996e-53

    1. Initial program 51.7%

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

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

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

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

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

    if 4.8e-36 < re < 1.05e26 or 1.45000000000000009e40 < re

    1. Initial program 15.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{\color{blue}{im \cdot im}}{re}\right)} \]
    4. Simplified53.8%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im \cdot im}{re}\right)}} \]
    5. Applied egg-rr25.3%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. Simplified83.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -9.5 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -4.8 \cdot 10^{-21}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -4.9 \cdot 10^{-39}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq -5.8 \cdot 10^{-53}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{im - re} \cdot \sqrt{2}\right)\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{-36} \lor \neg \left(re \leq 1.05 \cdot 10^{+26}\right) \land re \leq 1.45 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 76.3% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{+58}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4 \cdot 10^{-34} \lor \neg \left(re \leq 6 \cdot 10^{+25}\right) \land re \leq 1.5 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -3.1e+58)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (or (<= re 4e-34) (and (not (<= re 6e+25)) (<= re 1.5e+40)))
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -3.1e+58) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 4e-34) || (!(re <= 6e+25) && (re <= 1.5e+40))) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} 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) :: tmp
    if (re <= (-3.1d+58)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if ((re <= 4d-34) .or. (.not. (re <= 6d+25)) .and. (re <= 1.5d+40)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -3.1e+58) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 4e-34) || (!(re <= 6e+25) && (re <= 1.5e+40))) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -3.1e+58:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif (re <= 4e-34) or (not (re <= 6e+25) and (re <= 1.5e+40)):
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -3.1e+58)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif ((re <= 4e-34) || (!(re <= 6e+25) && (re <= 1.5e+40)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -3.1e+58)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif ((re <= 4e-34) || (~((re <= 6e+25)) && (re <= 1.5e+40)))
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -3.1e+58], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 4e-34], And[N[Not[LessEqual[re, 6e+25]], $MachinePrecision], LessEqual[re, 1.5e+40]]], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 4 \cdot 10^{-34} \lor \neg \left(re \leq 6 \cdot 10^{+25}\right) \land re \leq 1.5 \cdot 10^{+40}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\

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


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

    1. Initial program 32.1%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-2 \cdot re\right)}} \]
    3. Step-by-step derivation
      1. *-commutative82.8%

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

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

    if -3.0999999999999999e58 < re < 3.99999999999999971e-34 or 6.00000000000000011e25 < re < 1.5000000000000001e40

    1. Initial program 59.5%

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

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

    if 3.99999999999999971e-34 < re < 6.00000000000000011e25 or 1.5000000000000001e40 < re

    1. Initial program 15.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{\color{blue}{im \cdot im}}{re}\right)} \]
    4. Simplified53.8%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im \cdot im}{re}\right)}} \]
    5. Applied egg-rr25.3%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. Simplified83.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{+58}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4 \cdot 10^{-34} \lor \neg \left(re \leq 6 \cdot 10^{+25}\right) \land re \leq 1.5 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 4: 75.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{if}\;re \leq -1.05 \cdot 10^{-37}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{-10}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.12 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 7 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* im 2.0)))))
   (if (<= re -1.05e-37)
     (* 0.5 (sqrt (* 2.0 (* re -2.0))))
     (if (<= re 2.1e-10)
       t_0
       (if (<= re 1.12e+26)
         (* 0.5 (/ im (sqrt re)))
         (if (<= re 7e+41) t_0 (* 0.5 (* im (pow re -0.5)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((im * 2.0));
	double tmp;
	if (re <= -1.05e-37) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 2.1e-10) {
		tmp = t_0;
	} else if (re <= 1.12e+26) {
		tmp = 0.5 * (im / sqrt(re));
	} else if (re <= 7e+41) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im * pow(re, -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((im * 2.0d0))
    if (re <= (-1.05d-37)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 2.1d-10) then
        tmp = t_0
    else if (re <= 1.12d+26) then
        tmp = 0.5d0 * (im / sqrt(re))
    else if (re <= 7d+41) then
        tmp = t_0
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    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 (re <= -1.05e-37) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 2.1e-10) {
		tmp = t_0;
	} else if (re <= 1.12e+26) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else if (re <= 7e+41) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((im * 2.0))
	tmp = 0
	if re <= -1.05e-37:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 2.1e-10:
		tmp = t_0
	elif re <= 1.12e+26:
		tmp = 0.5 * (im / math.sqrt(re))
	elif re <= 7e+41:
		tmp = t_0
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(im * 2.0)))
	tmp = 0.0
	if (re <= -1.05e-37)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 2.1e-10)
		tmp = t_0;
	elseif (re <= 1.12e+26)
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	elseif (re <= 7e+41)
		tmp = t_0;
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((im * 2.0));
	tmp = 0.0;
	if (re <= -1.05e-37)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 2.1e-10)
		tmp = t_0;
	elseif (re <= 1.12e+26)
		tmp = 0.5 * (im / sqrt(re));
	elseif (re <= 7e+41)
		tmp = t_0;
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	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[re, -1.05e-37], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.1e-10], t$95$0, If[LessEqual[re, 1.12e+26], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7e+41], t$95$0, N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 2.1 \cdot 10^{-10}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 1.12 \cdot 10^{+26}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -1.05e-37

    1. Initial program 44.0%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-2 \cdot re\right)}} \]
    3. Step-by-step derivation
      1. *-commutative75.8%

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

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

    if -1.05e-37 < re < 2.1e-10 or 1.1200000000000001e26 < re < 6.9999999999999998e41

    1. Initial program 55.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{{\left(\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{0.3333333333333333}}} \]
      4. pow-to-exp34.2%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\log \color{blue}{\left({\left(\mathsf{hypot}\left(re, im\right) - re\right)}^{3}\right)} \cdot 0.3333333333333333}} \]
      6. log-pow83.4%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\color{blue}{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)} \cdot 0.3333333333333333}} \]
    3. Applied egg-rr83.4%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.3333333333333333}}} \]
    4. Taylor expanded in re around 0 87.8%

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

    if 2.1e-10 < re < 1.1200000000000001e26

    1. Initial program 20.6%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. Simplified84.4%

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

    if 6.9999999999999998e41 < re

    1. Initial program 13.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.05 \cdot 10^{-37}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{-10}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq 1.12 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 7 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 5: 63.9% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{if}\;re \leq 1.55 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 5.9 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* im 2.0)))))
   (if (<= re 1.55e-12)
     t_0
     (if (<= re 4.8e+26)
       (* 0.5 (/ im (sqrt re)))
       (if (<= re 5.9e+41) t_0 (* 0.5 (* im (pow re -0.5))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((im * 2.0));
	double tmp;
	if (re <= 1.55e-12) {
		tmp = t_0;
	} else if (re <= 4.8e+26) {
		tmp = 0.5 * (im / sqrt(re));
	} else if (re <= 5.9e+41) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im * pow(re, -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((im * 2.0d0))
    if (re <= 1.55d-12) then
        tmp = t_0
    else if (re <= 4.8d+26) then
        tmp = 0.5d0 * (im / sqrt(re))
    else if (re <= 5.9d+41) then
        tmp = t_0
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    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 (re <= 1.55e-12) {
		tmp = t_0;
	} else if (re <= 4.8e+26) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else if (re <= 5.9e+41) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((im * 2.0))
	tmp = 0
	if re <= 1.55e-12:
		tmp = t_0
	elif re <= 4.8e+26:
		tmp = 0.5 * (im / math.sqrt(re))
	elif re <= 5.9e+41:
		tmp = t_0
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(im * 2.0)))
	tmp = 0.0
	if (re <= 1.55e-12)
		tmp = t_0;
	elseif (re <= 4.8e+26)
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	elseif (re <= 5.9e+41)
		tmp = t_0;
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((im * 2.0));
	tmp = 0.0;
	if (re <= 1.55e-12)
		tmp = t_0;
	elseif (re <= 4.8e+26)
		tmp = 0.5 * (im / sqrt(re));
	elseif (re <= 5.9e+41)
		tmp = t_0;
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	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[re, 1.55e-12], t$95$0, If[LessEqual[re, 4.8e+26], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.9e+41], t$95$0, N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{if}\;re \leq 1.55 \cdot 10^{-12}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 4.8 \cdot 10^{+26}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < 1.5500000000000001e-12 or 4.80000000000000009e26 < re < 5.9000000000000001e41

    1. Initial program 51.4%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{{\left(\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{0.3333333333333333}}} \]
      4. pow-to-exp31.9%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\log \color{blue}{\left({\left(\mathsf{hypot}\left(re, im\right) - re\right)}^{3}\right)} \cdot 0.3333333333333333}} \]
      6. log-pow85.9%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\color{blue}{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)} \cdot 0.3333333333333333}} \]
    3. Applied egg-rr85.9%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.3333333333333333}}} \]
    4. Taylor expanded in re around 0 68.2%

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

    if 1.5500000000000001e-12 < re < 4.80000000000000009e26

    1. Initial program 20.6%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. Simplified84.4%

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

    if 5.9000000000000001e41 < re

    1. Initial program 13.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.55 \cdot 10^{-12}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{+26}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 5.9 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 6: 63.9% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 2.6 \cdot 10^{-11} \lor \neg \left(re \leq 1.2 \cdot 10^{+26}\right) \land re \leq 1.08 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re 2.6e-11) (and (not (<= re 1.2e+26)) (<= re 1.08e+40)))
   (* 0.5 (sqrt (* im 2.0)))
   (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if ((re <= 2.6e-11) || (!(re <= 1.2e+26) && (re <= 1.08e+40))) {
		tmp = 0.5 * sqrt((im * 2.0));
	} 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) :: tmp
    if ((re <= 2.6d-11) .or. (.not. (re <= 1.2d+26)) .and. (re <= 1.08d+40)) then
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((re <= 2.6e-11) || (!(re <= 1.2e+26) && (re <= 1.08e+40))) {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= 2.6e-11) or (not (re <= 1.2e+26) and (re <= 1.08e+40)):
		tmp = 0.5 * math.sqrt((im * 2.0))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= 2.6e-11) || (!(re <= 1.2e+26) && (re <= 1.08e+40)))
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= 2.6e-11) || (~((re <= 1.2e+26)) && (re <= 1.08e+40)))
		tmp = 0.5 * sqrt((im * 2.0));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, 2.6e-11], And[N[Not[LessEqual[re, 1.2e+26]], $MachinePrecision], LessEqual[re, 1.08e+40]]], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.6 \cdot 10^{-11} \lor \neg \left(re \leq 1.2 \cdot 10^{+26}\right) \land re \leq 1.08 \cdot 10^{+40}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 2.6000000000000001e-11 or 1.20000000000000002e26 < re < 1.08000000000000001e40

    1. Initial program 51.9%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\log \left(\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.3333333333333333}}} \]
      5. pow332.1%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\log \color{blue}{\left({\left(\mathsf{hypot}\left(re, im\right) - re\right)}^{3}\right)} \cdot 0.3333333333333333}} \]
      6. log-pow86.3%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\color{blue}{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)} \cdot 0.3333333333333333}} \]
    3. Applied egg-rr86.3%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.3333333333333333}}} \]
    4. Taylor expanded in re around 0 68.4%

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

    if 2.6000000000000001e-11 < re < 1.20000000000000002e26 or 1.08000000000000001e40 < re

    1. Initial program 14.8%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 2.6 \cdot 10^{-11} \lor \neg \left(re \leq 1.2 \cdot 10^{+26}\right) \land re \leq 1.08 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 7: 52.0% accurate, 2.0× speedup?

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\log \left(\left(\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.3333333333333333}}} \]
    5. pow329.6%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\log \color{blue}{\left({\left(\mathsf{hypot}\left(re, im\right) - re\right)}^{3}\right)} \cdot 0.3333333333333333}} \]
    6. log-pow72.6%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot e^{\color{blue}{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)} \cdot 0.3333333333333333}} \]
  3. Applied egg-rr72.6%

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{e^{\left(3 \cdot \log \left(\mathsf{hypot}\left(re, im\right) - re\right)\right) \cdot 0.3333333333333333}}} \]
  4. Taylor expanded in re around 0 55.7%

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

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

Reproduce

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