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

Percentage Accurate: 40.4% → 90.2%
Time: 11.0s
Alternatives: 8
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 8 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.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.2% 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:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \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)
   (* (pow re -0.5) (* im 0.5))
   (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 = pow(re, -0.5) * (im * 0.5);
	} 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 = Math.pow(re, -0.5) * (im * 0.5);
	} 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 = math.pow(re, -0.5) * (im * 0.5)
	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((re ^ -0.5) * Float64(im * 0.5));
	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 = (re ^ -0.5) * (im * 0.5);
	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[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $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:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\

\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 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 12.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{re}^{-0.5}} \cdot \left(0.5 \cdot im\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 46.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. pow146.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-rr88.3%

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

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

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

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

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

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

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

Alternative 2: 76.8% accurate, 1.7× speedup?

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

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

\mathbf{elif}\;re \leq 1.15 \cdot 10^{+49}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im + re \cdot \left(\frac{re}{im} - 2\right)}\\

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


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

    1. Initial program 53.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. pow153.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-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)} \]
    6. Simplified100.0%

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified75.5%

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

    if -2.1000000000000001e-23 < re < 1.15000000000000001e49

    1. Initial program 49.3%

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

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

    if 1.15000000000000001e49 < re

    1. Initial program 9.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.1 \cdot 10^{-23}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 1.15 \cdot 10^{+49}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im + re \cdot \left(\frac{re}{im} - 2\right)}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 76.6% accurate, 1.8× speedup?

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

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

\mathbf{elif}\;re \leq 3.9 \cdot 10^{+49}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\

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


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

    1. Initial program 53.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. pow153.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-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)} \]
    6. Simplified100.0%

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified75.5%

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

    if -4.8999999999999998e-23 < re < 3.9000000000000001e49

    1. Initial program 49.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. pow149.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-rr84.5%

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

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

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

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

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

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

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

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

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

    if 3.9000000000000001e49 < re

    1. Initial program 9.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.9 \cdot 10^{-23}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 3.9 \cdot 10^{+49}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 76.7% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;re \leq 1.5 \cdot 10^{+49}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\

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


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

    1. Initial program 53.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. pow153.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-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)} \]
    6. Simplified100.0%

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified75.5%

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

    if -4.79999999999999993e-23 < re < 1.5000000000000001e49

    1. Initial program 49.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. pow149.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-rr84.5%

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

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

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

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

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

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

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

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

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

    if 1.5000000000000001e49 < re

    1. Initial program 9.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Applied egg-rr82.9%

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
    8. Step-by-step derivation
      1. *-commutative82.9%

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

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

Alternative 5: 76.6% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;re \leq 1.75 \cdot 10^{+49}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\

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


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

    1. Initial program 53.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. pow153.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-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)} \]
    6. Simplified100.0%

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified75.5%

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

    if -1.8000000000000001e-26 < re < 1.74999999999999987e49

    1. Initial program 49.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. pow149.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-rr84.5%

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

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

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

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

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

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

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

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

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

    if 1.74999999999999987e49 < re

    1. Initial program 9.4%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right) \cdot \left(0.5 \cdot im\right)\right)} - 1} \]
      3. associate-*r*24.5%

        \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\left(\left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right) \cdot 0.5\right) \cdot im}\right)} - 1 \]
      4. *-commutative24.5%

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

        \[\leadsto e^{\mathsf{log1p}\left(im \cdot \left(\left(\color{blue}{\sqrt{2 \cdot 0.5}} \cdot \sqrt{\frac{1}{re}}\right) \cdot 0.5\right)\right)} - 1 \]
      6. metadata-eval24.5%

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

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

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

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

        \[\leadsto e^{\mathsf{log1p}\left(im \cdot \left(\frac{\color{blue}{1}}{\sqrt{re}} \cdot 0.5\right)\right)} - 1 \]
      11. associate-*l/24.5%

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\frac{0.5}{\sqrt{re}} \cdot im} \]
    11. Applied egg-rr82.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.8 \cdot 10^{-26}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 1.75 \cdot 10^{+49}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 64.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -8.5 \cdot 10^{-27}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -8.5e-27) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
	double tmp;
	if (re <= -8.5e-27) {
		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 <= (-8.5d-27)) 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 <= -8.5e-27) {
		tmp = Math.sqrt(-re);
	} else {
		tmp = Math.sqrt((im * 0.5));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -8.5e-27:
		tmp = math.sqrt(-re)
	else:
		tmp = math.sqrt((im * 0.5))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -8.5e-27)
		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 <= -8.5e-27)
		tmp = sqrt(-re);
	else
		tmp = sqrt((im * 0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -8.5e-27], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 53.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. pow153.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-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)} \]
    6. Simplified100.0%

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    9. Simplified75.5%

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

    if -8.50000000000000033e-27 < re

    1. Initial program 37.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. pow137.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-rr69.5%

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

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

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

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

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

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

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

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

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

Alternative 7: 31.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -2 \cdot 10^{-310}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
(FPCore (re im) :precision binary64 (if (<= re -2e-310) (sqrt (- re)) 0.0))
double code(double re, double im) {
	double tmp;
	if (re <= -2e-310) {
		tmp = sqrt(-re);
	} else {
		tmp = 0.0;
	}
	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 = 0.0d0
    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 = 0.0;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -2e-310:
		tmp = math.sqrt(-re)
	else:
		tmp = 0.0
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -2e-310)
		tmp = sqrt(Float64(-re));
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -2e-310)
		tmp = sqrt(-re);
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -2e-310], N[Sqrt[(-re)], $MachinePrecision], 0.0]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;0\\


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

    1. Initial program 52.7%

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

        \[\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)} \]
    6. Simplified100.0%

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

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

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

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

    if -1.999999999999994e-310 < re

    1. Initial program 33.4%

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

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

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

Alternative 8: 6.3% accurate, 213.0× speedup?

\[\begin{array}{l} \\ 0 \end{array} \]
(FPCore (re im) :precision binary64 0.0)
double code(double re, double im) {
	return 0.0;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.0d0
end function
public static double code(double re, double im) {
	return 0.0;
}
def code(re, im):
	return 0.0
function code(re, im)
	return 0.0
end
function tmp = code(re, im)
	tmp = 0.0;
end
code[re_, im_] := 0.0
\begin{array}{l}

\\
0
\end{array}
Derivation
  1. Initial program 41.8%

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

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

    \[\leadsto \color{blue}{0} \]
  5. Add Preprocessing

Reproduce

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