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

Percentage Accurate: 41.5% → 88.2%
Time: 11.7s
Alternatives: 7
Speedup: 1.9×

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 7 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.5% accurate, 1.0× speedup?

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

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

Alternative 1: 88.2% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 53.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. hypot-udef94.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 9.50000000000000044e-43 < re

    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. Step-by-step derivation
      1. hypot-udef31.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 77.0% accurate, 1.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.45 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

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

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


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

    1. Initial program 36.1%

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

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

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

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

    if -1.4499999999999999e54 < re < 3.30000000000000006e-44

    1. Initial program 59.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 0 81.7%

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

    if 3.30000000000000006e-44 < re

    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. Step-by-step derivation
      1. hypot-udef31.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 52.4% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{-311}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

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


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

    1. Initial program 54.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 50.5%

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

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

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

    if -3.99999999999979e-311 < re

    1. Initial program 31.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 26.2% accurate, 2.0× speedup?

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

\\
im \cdot \frac{0.5}{\sqrt{re}}
\end{array}
Derivation
  1. Initial program 42.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto e^{\mathsf{log1p}\left(im \cdot \left(0.5 \cdot \frac{\color{blue}{1}}{\sqrt{re}}\right)\right)} - 1 \]
    6. un-div-inv10.1%

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

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

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

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

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

    \[\leadsto im \cdot \frac{0.5}{\sqrt{re}} \]
  15. Add Preprocessing

Alternative 5: 26.3% accurate, 2.0× speedup?

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

\\
im \cdot \sqrt{\frac{0.25}{re}}
\end{array}
Derivation
  1. Initial program 42.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto {\left(\sqrt{im \cdot \color{blue}{\frac{0.5}{\sqrt{re}}}}\right)}^{2} \]
  11. Applied egg-rr27.8%

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

      \[\leadsto \color{blue}{\sqrt{im \cdot \frac{0.5}{\sqrt{re}}} \cdot \sqrt{im \cdot \frac{0.5}{\sqrt{re}}}} \]
    2. add-sqr-sqrt27.9%

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

      \[\leadsto \color{blue}{\frac{0.5}{\sqrt{re}} \cdot im} \]
    4. add-sqr-sqrt27.7%

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

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

      \[\leadsto \sqrt{\color{blue}{\frac{0.5 \cdot 0.5}{\sqrt{re} \cdot \sqrt{re}}}} \cdot im \]
    7. metadata-eval27.9%

      \[\leadsto \sqrt{\frac{\color{blue}{0.25}}{\sqrt{re} \cdot \sqrt{re}}} \cdot im \]
    8. add-sqr-sqrt27.9%

      \[\leadsto \sqrt{\frac{0.25}{\color{blue}{re}}} \cdot im \]
  13. Applied egg-rr27.9%

    \[\leadsto \color{blue}{\sqrt{\frac{0.25}{re}} \cdot im} \]
  14. Final simplification27.9%

    \[\leadsto im \cdot \sqrt{\frac{0.25}{re}} \]
  15. Add Preprocessing

Alternative 6: 26.3% accurate, 2.0× speedup?

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

\\
\frac{0.5 \cdot im}{\sqrt{re}}
\end{array}
Derivation
  1. Initial program 42.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{im \cdot 0.5}{\sqrt{re}}} \]
  12. Final simplification28.0%

    \[\leadsto \frac{0.5 \cdot im}{\sqrt{re}} \]
  13. Add Preprocessing

Alternative 7: 6.2% 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 42.9%

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

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

    \[\leadsto 0.5 \cdot \color{blue}{0} \]
  5. Step-by-step derivation
    1. metadata-eval6.6%

      \[\leadsto \color{blue}{0} \]
  6. Applied egg-rr6.6%

    \[\leadsto \color{blue}{0} \]
  7. Final simplification6.6%

    \[\leadsto 0 \]
  8. Add Preprocessing

Reproduce

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