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

Percentage Accurate: 40.9% → 88.3%
Time: 6.5s
Alternatives: 6
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 6 alternatives:

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

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

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 3 \cdot 10^{-38}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

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


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

    1. Initial program 47.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.99999999999999989e-38 < re

    1. Initial program 14.3%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{\color{blue}{{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{\left(1 + 0.5\right)}}} \]
      9. metadata-eval21.8%

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\frac{\color{blue}{1}}{re}}\right) \]
      6. rem-exp-log73.0%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\color{blue}{e^{-\log re}}}\right) \]
      8. unpow1/273.0%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(e^{-\log re}\right)}^{0.5}}\right) \]
      9. exp-prod72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      10. distribute-lft-neg-out72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      11. distribute-rgt-neg-in72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      12. metadata-eval72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      13. exp-to-pow77.4%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{re}^{-0.5}}\right) \]
    6. Simplified77.4%

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

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

Alternative 2: 75.9% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.45 \cdot 10^{-12}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-74}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1.45e-12)
   (* 0.5 (sqrt (* re -4.0)))
   (if (<= re 2.3e-74)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.45e-12) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 2.3e-74) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} 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) :: tmp
    if (re <= (-1.45d-12)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 2.3d-74) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.45e-12) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 2.3e-74) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.45e-12:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 2.3e-74:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.45e-12)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 2.3e-74)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.45e-12)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 2.3e-74)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.45e-12], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.3e-74], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.45 \cdot 10^{-12}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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

\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.4500000000000001e-12

    1. Initial program 36.9%

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

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

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

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

    if -1.4500000000000001e-12 < re < 2.2999999999999998e-74

    1. Initial program 57.7%

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

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

    if 2.2999999999999998e-74 < re

    1. Initial program 15.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\color{blue}{e^{-\log re}}}\right) \]
      8. unpow1/271.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(e^{-\log re}\right)}^{0.5}}\right) \]
      9. exp-prod71.8%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      10. distribute-lft-neg-out71.8%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      11. distribute-rgt-neg-in71.8%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      12. metadata-eval71.8%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      13. exp-to-pow76.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.45 \cdot 10^{-12}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-74}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 3: 75.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.8 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -4.7999999999999999e-15

    1. Initial program 36.9%

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

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

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

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

    if -4.7999999999999999e-15 < re < 1.90000000000000001e-72

    1. Initial program 57.7%

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

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

    if 1.90000000000000001e-72 < re

    1. Initial program 15.0%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{\frac{1}{re}} \cdot im\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification79.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.8 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.9 \cdot 10^{-72}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \]

Alternative 4: 75.9% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{-35}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -3.1e-15)
   (* 0.5 (sqrt (* re -4.0)))
   (if (<= re 2.1e-35)
     (* 0.5 (sqrt (* im 2.0)))
     (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -3.1e-15) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 2.1e-35) {
		tmp = 0.5 * sqrt((im * 2.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) :: tmp
    if (re <= (-3.1d-15)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 2.1d-35) then
        tmp = 0.5d0 * sqrt((im * 2.0d0))
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -3.1e-15) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 2.1e-35) {
		tmp = 0.5 * Math.sqrt((im * 2.0));
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -3.1e-15:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 2.1e-35:
		tmp = 0.5 * math.sqrt((im * 2.0))
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -3.1e-15)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 2.1e-35)
		tmp = Float64(0.5 * sqrt(Float64(im * 2.0)));
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -3.1e-15)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 2.1e-35)
		tmp = 0.5 * sqrt((im * 2.0));
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -3.1e-15], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.1e-35], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.1 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 2.1 \cdot 10^{-35}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

\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 < -3.0999999999999999e-15

    1. Initial program 36.9%

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

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

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

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

    if -3.0999999999999999e-15 < re < 2.1e-35

    1. Initial program 56.5%

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

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

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

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

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

    if 2.1e-35 < re

    1. Initial program 14.3%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{\color{blue}{{\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{\left(1 + 0.5\right)}}} \]
      9. metadata-eval21.8%

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\frac{\color{blue}{1}}{re}}\right) \]
      6. rem-exp-log73.0%

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

        \[\leadsto 0.5 \cdot \left(im \cdot \sqrt{\color{blue}{e^{-\log re}}}\right) \]
      8. unpow1/273.0%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(e^{-\log re}\right)}^{0.5}}\right) \]
      9. exp-prod72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      10. distribute-lft-neg-out72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      11. distribute-rgt-neg-in72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      12. metadata-eval72.9%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      13. exp-to-pow77.4%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{re}^{-0.5}}\right) \]
    6. Simplified77.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{-35}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 5: 63.4% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.3 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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


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

    1. Initial program 36.9%

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

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

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

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

    if -1.30000000000000002e-17 < re

    1. Initial program 36.7%

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

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

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

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

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

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

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

Alternative 6: 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 36.8%

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

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

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

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

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

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

Reproduce

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