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

Percentage Accurate: 41.4% → 90.1%
Time: 6.0s
Alternatives: 5
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 5 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.4% 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.1% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \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 (* 2.0 (- (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((2.0 * (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((2.0 * (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((2.0 * (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 (sqrt(Float64(2.0 * 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((2.0 * (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[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $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{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \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 (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 4.9%

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{\frac{{im}^{2}}{re}}}\right)} \]
      2. *-un-lft-identity8.5%

        \[\leadsto 0.5 \cdot \log \color{blue}{\left(1 \cdot e^{\sqrt{\frac{{im}^{2}}{re}}}\right)} \]
      3. log-prod8.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{\frac{{im}^{2}}{re}}}\right)\right)} \]
      4. metadata-eval8.5%

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    7. Step-by-step derivation
      1. +-lft-identity99.8%

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

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

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

    1. Initial program 47.1%

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

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

        \[\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)}}} \]
      3. sqrt-unprod89.5%

        \[\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)}} \]
      4. *-commutative89.5%

        \[\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)} \]
      5. *-commutative89.5%

        \[\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)}} \]
      6. swap-sqr89.5%

        \[\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)}} \]
      7. add-sqr-sqrt89.5%

        \[\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)} \]
      8. metadata-eval89.5%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \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: 72.8% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -5.2 \cdot 10^{+71}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -3.9 \cdot 10^{+33}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -400000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.45 \cdot 10^{-147} \lor \neg \left(re \leq 1.95 \cdot 10^{+28}\right) \land re \leq 1.5 \cdot 10^{+53}:\\ \;\;\;\;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 (* 0.5 (sqrt (* 2.0 im)))))
   (if (<= re -5.2e+71)
     t_0
     (if (<= re -3.9e+33)
       t_1
       (if (<= re -400000000000.0)
         t_0
         (if (or (<= re 1.45e-147)
                 (and (not (<= re 1.95e+28)) (<= re 1.5e+53)))
           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 = 0.5 * sqrt((2.0 * im));
	double tmp;
	if (re <= -5.2e+71) {
		tmp = t_0;
	} else if (re <= -3.9e+33) {
		tmp = t_1;
	} else if (re <= -400000000000.0) {
		tmp = t_0;
	} else if ((re <= 1.45e-147) || (!(re <= 1.95e+28) && (re <= 1.5e+53))) {
		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 = 0.5d0 * sqrt((2.0d0 * im))
    if (re <= (-5.2d+71)) then
        tmp = t_0
    else if (re <= (-3.9d+33)) then
        tmp = t_1
    else if (re <= (-400000000000.0d0)) then
        tmp = t_0
    else if ((re <= 1.45d-147) .or. (.not. (re <= 1.95d+28)) .and. (re <= 1.5d+53)) 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 = 0.5 * Math.sqrt((2.0 * im));
	double tmp;
	if (re <= -5.2e+71) {
		tmp = t_0;
	} else if (re <= -3.9e+33) {
		tmp = t_1;
	} else if (re <= -400000000000.0) {
		tmp = t_0;
	} else if ((re <= 1.45e-147) || (!(re <= 1.95e+28) && (re <= 1.5e+53))) {
		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 = 0.5 * math.sqrt((2.0 * im))
	tmp = 0
	if re <= -5.2e+71:
		tmp = t_0
	elif re <= -3.9e+33:
		tmp = t_1
	elif re <= -400000000000.0:
		tmp = t_0
	elif (re <= 1.45e-147) or (not (re <= 1.95e+28) and (re <= 1.5e+53)):
		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 = Float64(0.5 * sqrt(Float64(2.0 * im)))
	tmp = 0.0
	if (re <= -5.2e+71)
		tmp = t_0;
	elseif (re <= -3.9e+33)
		tmp = t_1;
	elseif (re <= -400000000000.0)
		tmp = t_0;
	elseif ((re <= 1.45e-147) || (!(re <= 1.95e+28) && (re <= 1.5e+53)))
		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 = 0.5 * sqrt((2.0 * im));
	tmp = 0.0;
	if (re <= -5.2e+71)
		tmp = t_0;
	elseif (re <= -3.9e+33)
		tmp = t_1;
	elseif (re <= -400000000000.0)
		tmp = t_0;
	elseif ((re <= 1.45e-147) || (~((re <= 1.95e+28)) && (re <= 1.5e+53)))
		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[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -5.2e+71], t$95$0, If[LessEqual[re, -3.9e+33], t$95$1, If[LessEqual[re, -400000000000.0], t$95$0, If[Or[LessEqual[re, 1.45e-147], And[N[Not[LessEqual[re, 1.95e+28]], $MachinePrecision], LessEqual[re, 1.5e+53]]], 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 := 0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{if}\;re \leq -5.2 \cdot 10^{+71}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -3.9 \cdot 10^{+33}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -400000000000:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 1.45 \cdot 10^{-147} \lor \neg \left(re \leq 1.95 \cdot 10^{+28}\right) \land re \leq 1.5 \cdot 10^{+53}:\\
\;\;\;\;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 < -5.19999999999999983e71 or -3.9000000000000002e33 < re < -4e11

    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. sqr-neg42.2%

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

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

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

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

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

    if -5.19999999999999983e71 < re < -3.9000000000000002e33 or -4e11 < re < 1.4500000000000001e-147 or 1.9499999999999999e28 < re < 1.49999999999999999e53

    1. Initial program 61.3%

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

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

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

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

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

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

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

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

    if 1.4500000000000001e-147 < re < 1.9499999999999999e28 or 1.49999999999999999e53 < re

    1. Initial program 16.6%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{\frac{{im}^{2}}{re}}}\right)} \]
      2. *-un-lft-identity13.7%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    7. Step-by-step derivation
      1. +-lft-identity74.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5.2 \cdot 10^{+71}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -3.9 \cdot 10^{+33}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq -400000000000:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.45 \cdot 10^{-147} \lor \neg \left(re \leq 1.95 \cdot 10^{+28}\right) \land re \leq 1.5 \cdot 10^{+53}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 73.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -5.3 \cdot 10^{+71}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -7.5 \cdot 10^{+31}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -350000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.05 \cdot 10^{-151}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 6.1 \cdot 10^{+28} \lor \neg \left(re \leq 1.5 \cdot 10^{+53}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0))))
        (t_1 (* 0.5 (sqrt (* 2.0 (- im re))))))
   (if (<= re -5.3e+71)
     t_0
     (if (<= re -7.5e+31)
       t_1
       (if (<= re -350000000000.0)
         t_0
         (if (<= re 2.05e-151)
           t_1
           (if (or (<= re 6.1e+28) (not (<= re 1.5e+53)))
             (* 0.5 (/ im (sqrt re)))
             (* 0.5 (sqrt (* 2.0 im))))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = 0.5 * sqrt((2.0 * (im - re)));
	double tmp;
	if (re <= -5.3e+71) {
		tmp = t_0;
	} else if (re <= -7.5e+31) {
		tmp = t_1;
	} else if (re <= -350000000000.0) {
		tmp = t_0;
	} else if (re <= 2.05e-151) {
		tmp = t_1;
	} else if ((re <= 6.1e+28) || !(re <= 1.5e+53)) {
		tmp = 0.5 * (im / sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = 0.5d0 * sqrt((2.0d0 * (im - re)))
    if (re <= (-5.3d+71)) then
        tmp = t_0
    else if (re <= (-7.5d+31)) then
        tmp = t_1
    else if (re <= (-350000000000.0d0)) then
        tmp = t_0
    else if (re <= 2.05d-151) then
        tmp = t_1
    else if ((re <= 6.1d+28) .or. (.not. (re <= 1.5d+53))) then
        tmp = 0.5d0 * (im / sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = 0.5 * Math.sqrt((2.0 * (im - re)));
	double tmp;
	if (re <= -5.3e+71) {
		tmp = t_0;
	} else if (re <= -7.5e+31) {
		tmp = t_1;
	} else if (re <= -350000000000.0) {
		tmp = t_0;
	} else if (re <= 2.05e-151) {
		tmp = t_1;
	} else if ((re <= 6.1e+28) || !(re <= 1.5e+53)) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = 0.5 * math.sqrt((2.0 * (im - re)))
	tmp = 0
	if re <= -5.3e+71:
		tmp = t_0
	elif re <= -7.5e+31:
		tmp = t_1
	elif re <= -350000000000.0:
		tmp = t_0
	elif re <= 2.05e-151:
		tmp = t_1
	elif (re <= 6.1e+28) or not (re <= 1.5e+53):
		tmp = 0.5 * (im / math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))))
	tmp = 0.0
	if (re <= -5.3e+71)
		tmp = t_0;
	elseif (re <= -7.5e+31)
		tmp = t_1;
	elseif (re <= -350000000000.0)
		tmp = t_0;
	elseif (re <= 2.05e-151)
		tmp = t_1;
	elseif ((re <= 6.1e+28) || !(re <= 1.5e+53))
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = 0.5 * sqrt((2.0 * (im - re)));
	tmp = 0.0;
	if (re <= -5.3e+71)
		tmp = t_0;
	elseif (re <= -7.5e+31)
		tmp = t_1;
	elseif (re <= -350000000000.0)
		tmp = t_0;
	elseif (re <= 2.05e-151)
		tmp = t_1;
	elseif ((re <= 6.1e+28) || ~((re <= 1.5e+53)))
		tmp = 0.5 * (im / sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	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[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -5.3e+71], t$95$0, If[LessEqual[re, -7.5e+31], t$95$1, If[LessEqual[re, -350000000000.0], t$95$0, If[LessEqual[re, 2.05e-151], t$95$1, If[Or[LessEqual[re, 6.1e+28], N[Not[LessEqual[re, 1.5e+53]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq -7.5 \cdot 10^{+31}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -350000000000:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 2.05 \cdot 10^{-151}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq 6.1 \cdot 10^{+28} \lor \neg \left(re \leq 1.5 \cdot 10^{+53}\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -5.2999999999999999e71 or -7.5e31 < re < -3.5e11

    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. sqr-neg42.2%

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

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

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

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

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

    if -5.2999999999999999e71 < re < -7.5e31 or -3.5e11 < re < 2.0500000000000001e-151

    1. Initial program 65.9%

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

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

    if 2.0500000000000001e-151 < re < 6.1000000000000002e28 or 1.49999999999999999e53 < re

    1. Initial program 16.3%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{\frac{{im}^{2}}{re}}}\right)} \]
      2. *-un-lft-identity13.5%

        \[\leadsto 0.5 \cdot \log \color{blue}{\left(1 \cdot e^{\sqrt{\frac{{im}^{2}}{re}}}\right)} \]
      3. log-prod13.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{\frac{{im}^{2}}{re}}}\right)\right)} \]
      4. metadata-eval13.5%

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

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

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

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

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

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

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

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

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

    if 6.1000000000000002e28 < re < 1.49999999999999999e53

    1. Initial program 18.1%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5.3 \cdot 10^{+71}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -7.5 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -350000000000:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.05 \cdot 10^{-151}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 6.1 \cdot 10^{+28} \lor \neg \left(re \leq 1.5 \cdot 10^{+53}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 4: 60.3% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 37.0%

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

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

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

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

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

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

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

    if 2.80000000000000015e-96 < im

    1. Initial program 41.1%

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

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

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

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

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

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

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

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

Alternative 5: 52.7% accurate, 2.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

Reproduce

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