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

Percentage Accurate: 40.8% → 89.4%
Time: 9.4s
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: 40.8% 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: 89.4% 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:\\ \;\;\;\;im \cdot \left(0.5 \cdot {re}^{-0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(im, re\right) - re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0)
   (* im (* 0.5 (pow re -0.5)))
   (sqrt (* 0.5 (- (hypot im re) re)))))
double code(double re, double im) {
	double tmp;
	if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
		tmp = im * (0.5 * pow(re, -0.5));
	} else {
		tmp = sqrt((0.5 * (hypot(im, re) - 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 = im * (0.5 * Math.pow(re, -0.5));
	} else {
		tmp = Math.sqrt((0.5 * (Math.hypot(im, re) - re)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0:
		tmp = im * (0.5 * math.pow(re, -0.5))
	else:
		tmp = math.sqrt((0.5 * (math.hypot(im, re) - 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(im * Float64(0.5 * (re ^ -0.5)));
	else
		tmp = sqrt(Float64(0.5 * Float64(hypot(im, re) - 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 = im * (0.5 * (re ^ -0.5));
	else
		tmp = sqrt((0.5 * (hypot(im, re) - 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[(im * N[(0.5 * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[im ^ 2 + re ^ 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:\\
\;\;\;\;im \cdot \left(0.5 \cdot {re}^{-0.5}\right)\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(im, re\right) - re\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 5.8%

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

        \[\leadsto \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\right)}^{1}} \]
    4. Applied egg-rr5.8%

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{0.5 \cdot \left(\sqrt{\color{blue}{im \cdot im} + {re}^{2}} - re\right)} \]
      10. unpow25.8%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{im \cdot \left(0.5 \cdot \sqrt{\frac{1}{re}}\right)} \]
    10. Step-by-step derivation
      1. *-un-lft-identity99.6%

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

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

        \[\leadsto im \cdot \left(0.5 \cdot \left(1 \cdot \color{blue}{{re}^{\left(\frac{-1}{2}\right)}}\right)\right) \]
      4. metadata-eval99.6%

        \[\leadsto im \cdot \left(0.5 \cdot \left(1 \cdot {re}^{\color{blue}{-0.5}}\right)\right) \]
    11. Applied egg-rr99.6%

      \[\leadsto im \cdot \left(0.5 \cdot \color{blue}{\left(1 \cdot {re}^{-0.5}\right)}\right) \]
    12. Step-by-step derivation
      1. *-lft-identity99.6%

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

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

    if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 44.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 76.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.066 \cdot 10^{+45}:\\
\;\;\;\;\sqrt{-re}\\

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

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


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

    1. Initial program 37.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    8. Step-by-step derivation
      1. neg-mul-187.8%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified87.8%

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

    if -1.06600000000000005e45 < re < 1.4500000000000001e-52

    1. Initial program 55.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.4500000000000001e-52 < re

    1. Initial program 12.1%

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

        \[\leadsto \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\right)}^{1}} \]
    4. Applied egg-rr43.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto im \cdot \left(\color{blue}{\left(\sqrt{0.5} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right) \]
      3. rem-square-sqrt71.5%

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

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

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

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

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

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

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

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

        \[\leadsto im \cdot \sqrt{\frac{1}{re} \cdot \color{blue}{0.25}} \]
    11. Applied egg-rr71.5%

      \[\leadsto im \cdot \color{blue}{\sqrt{\frac{1}{re} \cdot 0.25}} \]
    12. Step-by-step derivation
      1. associate-*l/71.5%

        \[\leadsto im \cdot \sqrt{\color{blue}{\frac{1 \cdot 0.25}{re}}} \]
      2. metadata-eval71.5%

        \[\leadsto im \cdot \sqrt{\frac{\color{blue}{0.25}}{re}} \]
    13. Simplified71.5%

      \[\leadsto im \cdot \color{blue}{\sqrt{\frac{0.25}{re}}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 63.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.066 \cdot 10^{+45}:\\
\;\;\;\;\sqrt{-re}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -1.06600000000000005e45

    1. Initial program 37.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    8. Step-by-step derivation
      1. neg-mul-187.8%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified87.8%

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

    if -1.06600000000000005e45 < re

    1. Initial program 40.5%

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

        \[\leadsto \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\right)}^{1}} \]
    4. Applied egg-rr74.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{im \cdot 0.5}} \]
    9. Simplified63.4%

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

Alternative 4: 29.2% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\

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


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

    1. Initial program 54.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    8. Step-by-step derivation
      1. neg-mul-148.2%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified48.2%

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

    if -1.999999999999994e-310 < re

    1. Initial program 24.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    8. Step-by-step derivation
      1. neg-mul-10.0%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified0.0%

      \[\leadsto \sqrt{\color{blue}{-re}} \]
    10. Step-by-step derivation
      1. add-sqr-sqrt0.0%

        \[\leadsto \sqrt{\color{blue}{\sqrt{-re} \cdot \sqrt{-re}}} \]
      2. sqrt-unprod4.5%

        \[\leadsto \sqrt{\color{blue}{\sqrt{\left(-re\right) \cdot \left(-re\right)}}} \]
      3. sqr-neg4.5%

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

        \[\leadsto \sqrt{\color{blue}{\sqrt{re} \cdot \sqrt{re}}} \]
      5. add-sqr-sqrt5.8%

        \[\leadsto \sqrt{\color{blue}{re}} \]
      6. *-un-lft-identity5.8%

        \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
    11. Applied egg-rr5.8%

      \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
    12. Step-by-step derivation
      1. *-lft-identity5.8%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    13. Simplified5.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 5: 2.9% accurate, 2.1× speedup?

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

\\
\sqrt{re}
\end{array}
Derivation
  1. Initial program 39.8%

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

      \[\leadsto \color{blue}{{\left(0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\right)}^{1}} \]
  4. Applied egg-rr79.8%

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{0.5 \cdot \left(\sqrt{\color{blue}{im \cdot im} + {re}^{2}} - re\right)} \]
    10. unpow239.8%

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

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

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

    \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
  8. Step-by-step derivation
    1. neg-mul-125.0%

      \[\leadsto \sqrt{\color{blue}{-re}} \]
  9. Simplified25.0%

    \[\leadsto \sqrt{\color{blue}{-re}} \]
  10. Step-by-step derivation
    1. add-sqr-sqrt25.0%

      \[\leadsto \sqrt{\color{blue}{\sqrt{-re} \cdot \sqrt{-re}}} \]
    2. sqrt-unprod16.2%

      \[\leadsto \sqrt{\color{blue}{\sqrt{\left(-re\right) \cdot \left(-re\right)}}} \]
    3. sqr-neg16.2%

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

      \[\leadsto \sqrt{\color{blue}{\sqrt{re} \cdot \sqrt{re}}} \]
    5. add-sqr-sqrt2.8%

      \[\leadsto \sqrt{\color{blue}{re}} \]
    6. *-un-lft-identity2.8%

      \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
  11. Applied egg-rr2.8%

    \[\leadsto \color{blue}{1 \cdot \sqrt{re}} \]
  12. Step-by-step derivation
    1. *-lft-identity2.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  13. Simplified2.8%

    \[\leadsto \color{blue}{\sqrt{re}} \]
  14. Add Preprocessing

Reproduce

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