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

Percentage Accurate: 41.7% → 90.3%
Time: 10.3s
Alternatives: 6
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

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

Initial Program: 41.7% 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.3% 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:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0)
   (/ (* im 0.5) (sqrt re))
   (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
	double tmp;
	if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
		tmp = (im * 0.5) / sqrt(re);
	} else {
		tmp = sqrt((0.5 * (hypot(re, im) - re)));
	}
	return tmp;
}
public static double code(double re, double im) {
	double tmp;
	if (Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
		tmp = (im * 0.5) / Math.sqrt(re);
	} else {
		tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0:
		tmp = (im * 0.5) / math.sqrt(re)
	else:
		tmp = math.sqrt((0.5 * (math.hypot(re, im) - re)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))) <= 0.0)
		tmp = Float64(Float64(im * 0.5) / sqrt(re));
	else
		tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0)
		tmp = (im * 0.5) / sqrt(re);
	else
		tmp = sqrt((0.5 * (hypot(re, im) - re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $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:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\

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


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

    1. Initial program 11.2%

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

      \[\leadsto 0.5 \cdot \color{blue}{\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. associate-*r*98.1%

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

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

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

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

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

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

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

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

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

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

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

    1. Initial program 41.9%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative90.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 77.0% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -8.4 \cdot 10^{+34}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 2.05 \cdot 10^{-16}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -8.4e+34)
   (sqrt (- re))
   (if (<= re 2.05e-16) (sqrt (* 0.5 (- im re))) (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if (re <= -8.4e+34) {
		tmp = sqrt(-re);
	} else if (re <= 2.05e-16) {
		tmp = sqrt((0.5 * (im - re)));
	} 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 <= (-8.4d+34)) then
        tmp = sqrt(-re)
    else if (re <= 2.05d-16) then
        tmp = sqrt((0.5d0 * (im - re)))
    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 <= -8.4e+34) {
		tmp = Math.sqrt(-re);
	} else if (re <= 2.05e-16) {
		tmp = Math.sqrt((0.5 * (im - re)));
	} else {
		tmp = (im * 0.5) / Math.sqrt(re);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -8.4e+34:
		tmp = math.sqrt(-re)
	elif re <= 2.05e-16:
		tmp = math.sqrt((0.5 * (im - re)))
	else:
		tmp = (im * 0.5) / math.sqrt(re)
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -8.4e+34)
		tmp = sqrt(Float64(-re));
	elseif (re <= 2.05e-16)
		tmp = sqrt(Float64(0.5 * Float64(im - re)));
	else
		tmp = Float64(Float64(im * 0.5) / sqrt(re));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -8.4e+34)
		tmp = sqrt(-re);
	elseif (re <= 2.05e-16)
		tmp = sqrt((0.5 * (im - re)));
	else
		tmp = (im * 0.5) / sqrt(re);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -8.4e+34], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 2.05e-16], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

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

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


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

    1. Initial program 33.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    11. Simplified81.1%

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

    if -8.4000000000000007e34 < re < 2.05000000000000003e-16

    1. Initial program 58.4%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative91.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.05000000000000003e-16 < re

    1. Initial program 9.5%

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

      \[\leadsto 0.5 \cdot \color{blue}{\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. associate-*r*77.7%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -8.4 \cdot 10^{+34}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 2.05 \cdot 10^{-16}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 76.8% accurate, 1.9× speedup?

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

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

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

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


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

    1. Initial program 33.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    11. Simplified81.1%

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

    if -5.80000000000000049e33 < re < 2.74999999999999982e-16

    1. Initial program 58.4%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative91.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.74999999999999982e-16 < re

    1. Initial program 9.5%

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

      \[\leadsto 0.5 \cdot \color{blue}{\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. associate-*r*77.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5.8 \cdot 10^{+33}:\\ \;\;\;\;\sqrt{-re}\\ \mathbf{elif}\;re \leq 2.75 \cdot 10^{-16}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 65.1% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 33.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    11. Simplified81.1%

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

    if -3.3999999999999999e33 < re

    1. Initial program 40.3%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative72.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{im \cdot 0.5}} \]
    11. Simplified59.2%

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

Alternative 5: 29.1% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 48.1%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{-re}} \]
    11. Simplified57.6%

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

    if -9.999999999999969e-311 < re

    1. Initial program 26.2%

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

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

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

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutative57.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-re}} \]
    12. Taylor expanded in re around 0 0.0%

      \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
    13. Step-by-step derivation
      1. mul-1-neg0.0%

        \[\leadsto \sqrt{\color{blue}{-re}} \]
      2. rem-square-sqrt0.0%

        \[\leadsto \sqrt{-\color{blue}{\sqrt{re} \cdot \sqrt{re}}} \]
      3. distribute-lft-neg-out0.0%

        \[\leadsto \sqrt{\color{blue}{\left(-\sqrt{re}\right) \cdot \sqrt{re}}} \]
      4. neg-mul-10.0%

        \[\leadsto \sqrt{\color{blue}{\left(-1 \cdot \sqrt{re}\right)} \cdot \sqrt{re}} \]
      5. rem-square-sqrt0.0%

        \[\leadsto \sqrt{\left(\color{blue}{\left(\sqrt{-1} \cdot \sqrt{-1}\right)} \cdot \sqrt{re}\right) \cdot \sqrt{re}} \]
      6. unpow1/20.0%

        \[\leadsto \sqrt{\left(\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot \color{blue}{{re}^{0.5}}\right) \cdot \sqrt{re}} \]
      7. metadata-eval0.0%

        \[\leadsto \sqrt{\left(\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot {re}^{\color{blue}{\left(2 \cdot 0.25\right)}}\right) \cdot \sqrt{re}} \]
      8. pow-sqr0.0%

        \[\leadsto \sqrt{\left(\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot \color{blue}{\left({re}^{0.25} \cdot {re}^{0.25}\right)}\right) \cdot \sqrt{re}} \]
      9. unswap-sqr0.0%

        \[\leadsto \sqrt{\color{blue}{\left(\left(\sqrt{-1} \cdot {re}^{0.25}\right) \cdot \left(\sqrt{-1} \cdot {re}^{0.25}\right)\right)} \cdot \sqrt{re}} \]
      10. fabs-sqr0.0%

        \[\leadsto \sqrt{\color{blue}{\left|\left(\sqrt{-1} \cdot {re}^{0.25}\right) \cdot \left(\sqrt{-1} \cdot {re}^{0.25}\right)\right|} \cdot \sqrt{re}} \]
      11. unswap-sqr0.0%

        \[\leadsto \sqrt{\left|\color{blue}{\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot \left({re}^{0.25} \cdot {re}^{0.25}\right)}\right| \cdot \sqrt{re}} \]
      12. rem-square-sqrt5.5%

        \[\leadsto \sqrt{\left|\color{blue}{-1} \cdot \left({re}^{0.25} \cdot {re}^{0.25}\right)\right| \cdot \sqrt{re}} \]
      13. pow-sqr5.5%

        \[\leadsto \sqrt{\left|-1 \cdot \color{blue}{{re}^{\left(2 \cdot 0.25\right)}}\right| \cdot \sqrt{re}} \]
      14. metadata-eval5.5%

        \[\leadsto \sqrt{\left|-1 \cdot {re}^{\color{blue}{0.5}}\right| \cdot \sqrt{re}} \]
      15. unpow1/25.5%

        \[\leadsto \sqrt{\left|-1 \cdot \color{blue}{\sqrt{re}}\right| \cdot \sqrt{re}} \]
      16. neg-mul-15.5%

        \[\leadsto \sqrt{\left|\color{blue}{-\sqrt{re}}\right| \cdot \sqrt{re}} \]
      17. fabs-neg5.5%

        \[\leadsto \sqrt{\color{blue}{\left|\sqrt{re}\right|} \cdot \sqrt{re}} \]
      18. unpow1/25.5%

        \[\leadsto \sqrt{\left|\color{blue}{{re}^{0.5}}\right| \cdot \sqrt{re}} \]
      19. metadata-eval5.5%

        \[\leadsto \sqrt{\left|{re}^{\color{blue}{\left(2 \cdot 0.25\right)}}\right| \cdot \sqrt{re}} \]
      20. pow-sqr5.5%

        \[\leadsto \sqrt{\left|\color{blue}{{re}^{0.25} \cdot {re}^{0.25}}\right| \cdot \sqrt{re}} \]
      21. fabs-sqr5.5%

        \[\leadsto \sqrt{\color{blue}{\left({re}^{0.25} \cdot {re}^{0.25}\right)} \cdot \sqrt{re}} \]
      22. pow-sqr5.5%

        \[\leadsto \sqrt{\color{blue}{{re}^{\left(2 \cdot 0.25\right)}} \cdot \sqrt{re}} \]
      23. metadata-eval5.5%

        \[\leadsto \sqrt{{re}^{\color{blue}{0.5}} \cdot \sqrt{re}} \]
      24. unpow1/25.5%

        \[\leadsto \sqrt{\color{blue}{\sqrt{re}} \cdot \sqrt{re}} \]
    14. Simplified5.5%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. *-commutative80.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{-re}} \]
  11. Simplified31.7%

    \[\leadsto \sqrt{\color{blue}{-re}} \]
  12. Taylor expanded in re around 0 31.7%

    \[\leadsto \sqrt{\color{blue}{-1 \cdot re}} \]
  13. Step-by-step derivation
    1. mul-1-neg31.7%

      \[\leadsto \sqrt{\color{blue}{-re}} \]
    2. rem-square-sqrt0.0%

      \[\leadsto \sqrt{-\color{blue}{\sqrt{re} \cdot \sqrt{re}}} \]
    3. distribute-lft-neg-out0.0%

      \[\leadsto \sqrt{\color{blue}{\left(-\sqrt{re}\right) \cdot \sqrt{re}}} \]
    4. neg-mul-10.0%

      \[\leadsto \sqrt{\color{blue}{\left(-1 \cdot \sqrt{re}\right)} \cdot \sqrt{re}} \]
    5. rem-square-sqrt0.0%

      \[\leadsto \sqrt{\left(\color{blue}{\left(\sqrt{-1} \cdot \sqrt{-1}\right)} \cdot \sqrt{re}\right) \cdot \sqrt{re}} \]
    6. unpow1/20.0%

      \[\leadsto \sqrt{\left(\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot \color{blue}{{re}^{0.5}}\right) \cdot \sqrt{re}} \]
    7. metadata-eval0.0%

      \[\leadsto \sqrt{\left(\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot {re}^{\color{blue}{\left(2 \cdot 0.25\right)}}\right) \cdot \sqrt{re}} \]
    8. pow-sqr0.0%

      \[\leadsto \sqrt{\left(\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot \color{blue}{\left({re}^{0.25} \cdot {re}^{0.25}\right)}\right) \cdot \sqrt{re}} \]
    9. unswap-sqr0.0%

      \[\leadsto \sqrt{\color{blue}{\left(\left(\sqrt{-1} \cdot {re}^{0.25}\right) \cdot \left(\sqrt{-1} \cdot {re}^{0.25}\right)\right)} \cdot \sqrt{re}} \]
    10. fabs-sqr0.0%

      \[\leadsto \sqrt{\color{blue}{\left|\left(\sqrt{-1} \cdot {re}^{0.25}\right) \cdot \left(\sqrt{-1} \cdot {re}^{0.25}\right)\right|} \cdot \sqrt{re}} \]
    11. unswap-sqr0.0%

      \[\leadsto \sqrt{\left|\color{blue}{\left(\sqrt{-1} \cdot \sqrt{-1}\right) \cdot \left({re}^{0.25} \cdot {re}^{0.25}\right)}\right| \cdot \sqrt{re}} \]
    12. rem-square-sqrt2.5%

      \[\leadsto \sqrt{\left|\color{blue}{-1} \cdot \left({re}^{0.25} \cdot {re}^{0.25}\right)\right| \cdot \sqrt{re}} \]
    13. pow-sqr2.5%

      \[\leadsto \sqrt{\left|-1 \cdot \color{blue}{{re}^{\left(2 \cdot 0.25\right)}}\right| \cdot \sqrt{re}} \]
    14. metadata-eval2.5%

      \[\leadsto \sqrt{\left|-1 \cdot {re}^{\color{blue}{0.5}}\right| \cdot \sqrt{re}} \]
    15. unpow1/22.5%

      \[\leadsto \sqrt{\left|-1 \cdot \color{blue}{\sqrt{re}}\right| \cdot \sqrt{re}} \]
    16. neg-mul-12.5%

      \[\leadsto \sqrt{\left|\color{blue}{-\sqrt{re}}\right| \cdot \sqrt{re}} \]
    17. fabs-neg2.5%

      \[\leadsto \sqrt{\color{blue}{\left|\sqrt{re}\right|} \cdot \sqrt{re}} \]
    18. unpow1/22.5%

      \[\leadsto \sqrt{\left|\color{blue}{{re}^{0.5}}\right| \cdot \sqrt{re}} \]
    19. metadata-eval2.5%

      \[\leadsto \sqrt{\left|{re}^{\color{blue}{\left(2 \cdot 0.25\right)}}\right| \cdot \sqrt{re}} \]
    20. pow-sqr2.5%

      \[\leadsto \sqrt{\left|\color{blue}{{re}^{0.25} \cdot {re}^{0.25}}\right| \cdot \sqrt{re}} \]
    21. fabs-sqr2.5%

      \[\leadsto \sqrt{\color{blue}{\left({re}^{0.25} \cdot {re}^{0.25}\right)} \cdot \sqrt{re}} \]
    22. pow-sqr2.5%

      \[\leadsto \sqrt{\color{blue}{{re}^{\left(2 \cdot 0.25\right)}} \cdot \sqrt{re}} \]
    23. metadata-eval2.5%

      \[\leadsto \sqrt{{re}^{\color{blue}{0.5}} \cdot \sqrt{re}} \]
    24. unpow1/22.5%

      \[\leadsto \sqrt{\color{blue}{\sqrt{re}} \cdot \sqrt{re}} \]
  14. Simplified2.5%

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

Reproduce

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