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

Percentage Accurate: 42.2% → 89.8%
Time: 7.1s
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: 42.2% accurate, 1.0× speedup?

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

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

Alternative 1: 89.8% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\ \;\;\;\;0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot {im}^{0.5}\right)}^{2}\\ \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)
   (* 0.5 (pow (* (pow (/ 1.0 re) 0.25) (pow im 0.5)) 2.0))
   (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 = 0.5 * pow((pow((1.0 / re), 0.25) * pow(im, 0.5)), 2.0);
	} 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 = 0.5 * Math.pow((Math.pow((1.0 / re), 0.25) * Math.pow(im, 0.5)), 2.0);
	} 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 = 0.5 * math.pow((math.pow((1.0 / re), 0.25) * math.pow(im, 0.5)), 2.0)
	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(0.5 * (Float64((Float64(1.0 / re) ^ 0.25) * (im ^ 0.5)) ^ 2.0));
	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 = 0.5 * ((((1.0 / re) ^ 0.25) * (im ^ 0.5)) ^ 2.0);
	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[(0.5 * N[Power[N[(N[Power[N[(1.0 / re), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[im, 0.5], $MachinePrecision]), $MachinePrecision], 2.0], $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:\\
\;\;\;\;0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot {im}^{0.5}\right)}^{2}\\

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

    1. Initial program 14.4%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{0.25 \cdot \left(\log \left(\frac{1}{re}\right) + \log \left({im}^{2}\right)\right)}\right)}^{2}} \]
    5. Step-by-step derivation
      1. log-pow91.8%

        \[\leadsto 0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left(\frac{1}{re}\right) + \color{blue}{2 \cdot \log im}\right)}\right)}^{2} \]
      2. log-pow47.9%

        \[\leadsto 0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left(\frac{1}{re}\right) + \color{blue}{\log \left({im}^{2}\right)}\right)}\right)}^{2} \]
      3. distribute-rgt-in47.9%

        \[\leadsto 0.5 \cdot {\left(e^{\color{blue}{\log \left(\frac{1}{re}\right) \cdot 0.25 + \log \left({im}^{2}\right) \cdot 0.25}}\right)}^{2} \]
      4. exp-sum47.9%

        \[\leadsto 0.5 \cdot {\color{blue}{\left(e^{\log \left(\frac{1}{re}\right) \cdot 0.25} \cdot e^{\log \left({im}^{2}\right) \cdot 0.25}\right)}}^{2} \]
      5. exp-to-pow48.2%

        \[\leadsto 0.5 \cdot {\left(\color{blue}{{\left(\frac{1}{re}\right)}^{0.25}} \cdot e^{\log \left({im}^{2}\right) \cdot 0.25}\right)}^{2} \]
      6. log-pow91.6%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot e^{\color{blue}{\left(2 \cdot \log im\right)} \cdot 0.25}\right)}^{2} \]
      7. *-commutative91.6%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot e^{\color{blue}{\left(\log im \cdot 2\right)} \cdot 0.25}\right)}^{2} \]
      8. associate-*l*91.6%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot e^{\color{blue}{\log im \cdot \left(2 \cdot 0.25\right)}}\right)}^{2} \]
      9. metadata-eval91.6%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot e^{\log im \cdot \color{blue}{0.5}}\right)}^{2} \]
      10. exp-to-pow99.1%

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

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

    if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 42.3%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} + \left(-re\right)\right)}} \]
      2. sqr-neg42.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-neg42.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-neg42.3%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Step-by-step derivation
      1. add-sqr-sqrt87.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)}}} \]
      2. sqrt-unprod87.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)}} \]
      3. *-commutative87.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)} \]
      4. *-commutative87.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)}} \]
      5. swap-sqr87.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)}} \]
      6. add-sqr-sqrt87.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)} \]
      7. metadata-eval87.8%

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

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

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

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

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

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

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

Alternative 2: 84.6% 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:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0)
   (* 0.5 (sqrt (* 2.0 (* 0.5 (/ (pow im 2.0) 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 = 0.5 * sqrt((2.0 * (0.5 * (pow(im, 2.0) / 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 = 0.5 * Math.sqrt((2.0 * (0.5 * (Math.pow(im, 2.0) / 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 = 0.5 * math.sqrt((2.0 * (0.5 * (math.pow(im, 2.0) / 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(0.5 * sqrt(Float64(2.0 * Float64(0.5 * Float64((im ^ 2.0) / 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 = 0.5 * sqrt((2.0 * (0.5 * ((im ^ 2.0) / 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[(0.5 * N[Sqrt[N[(2.0 * N[(0.5 * N[(N[Power[im, 2.0], $MachinePrecision] / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $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:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)}\\

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


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

    1. Initial program 14.4%

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

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

    if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 42.3%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} + \left(-re\right)\right)}} \]
      2. sqr-neg42.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-neg42.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-neg42.3%

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
    4. Step-by-step derivation
      1. add-sqr-sqrt87.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)}}} \]
      2. sqrt-unprod87.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)}} \]
      3. *-commutative87.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)} \]
      4. *-commutative87.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)}} \]
      5. swap-sqr87.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)}} \]
      6. add-sqr-sqrt87.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)} \]
      7. metadata-eval87.8%

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

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

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

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

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

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

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

Alternative 3: 79.3% accurate, 1.0× speedup?

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

\\
\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}
\end{array}
Derivation
  1. Initial program 38.4%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} + \left(-re\right)\right)}} \]
    2. sqr-neg38.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-neg38.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-neg38.4%

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

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

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  4. Step-by-step derivation
    1. add-sqr-sqrt77.0%

      \[\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)}}} \]
    2. sqrt-unprod77.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)}} \]
    3. *-commutative77.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)} \]
    4. *-commutative77.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)}} \]
    5. swap-sqr77.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)}} \]
    6. add-sqr-sqrt77.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)} \]
    7. metadata-eval77.5%

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

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

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

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

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

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

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

Alternative 4: 64.0% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;re \leq 1.12 \cdot 10^{+237}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\


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

    1. Initial program 38.1%

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

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

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

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

    if -800 < re < 1.11999999999999997e237

    1. Initial program 40.7%

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}\right)}^{2}} \]
    5. Step-by-step derivation
      1. unpow257.2%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \cdot e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}\right)} \]
      2. prod-exp57.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}} \]
      3. +-commutative57.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right) + \log 2\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      4. mul-1-neg57.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\color{blue}{\left(-\log \left(\frac{1}{im}\right)\right)} + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      5. log-rec57.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\left(-\color{blue}{\left(-\log im\right)}\right) + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      6. remove-double-neg57.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\color{blue}{\log im} + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      7. +-commutative57.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\left(\log 2 + \log im\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      8. log-prod57.3%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\log \left(2 \cdot im\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      9. log-pow57.3%

        \[\leadsto 0.5 \cdot e^{\color{blue}{\log \left({\left(2 \cdot im\right)}^{0.25}\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      10. metadata-eval57.3%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\color{blue}{\left(\frac{0.5}{2}\right)}}\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      11. +-commutative57.3%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right) + \log 2\right)}} \]
      12. mul-1-neg57.3%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\color{blue}{\left(-\log \left(\frac{1}{im}\right)\right)} + \log 2\right)} \]
      13. log-rec57.3%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\left(-\color{blue}{\left(-\log im\right)}\right) + \log 2\right)} \]
      14. remove-double-neg57.3%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\color{blue}{\log im} + \log 2\right)} \]
    6. Simplified62.0%

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

    if 1.11999999999999997e237 < re

    1. Initial program 2.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -800:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.12 \cdot 10^{+237}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\ \end{array} \]

Alternative 5: 63.4% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 38.1%

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

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

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

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

    if -0.0749999999999999972 < re

    1. Initial program 38.4%

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

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

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

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

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

        \[\leadsto 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]
      6. metadata-eval69.9%

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

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

      \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}\right)}^{2}} \]
    5. Step-by-step derivation
      1. unpow254.1%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \cdot e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}\right)} \]
      2. prod-exp54.2%

        \[\leadsto 0.5 \cdot \color{blue}{e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}} \]
      3. +-commutative54.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right) + \log 2\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      4. mul-1-neg54.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\color{blue}{\left(-\log \left(\frac{1}{im}\right)\right)} + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      5. log-rec54.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\left(-\color{blue}{\left(-\log im\right)}\right) + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      6. remove-double-neg54.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\color{blue}{\log im} + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      7. +-commutative54.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\left(\log 2 + \log im\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      8. log-prod54.2%

        \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\log \left(2 \cdot im\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      9. log-pow54.2%

        \[\leadsto 0.5 \cdot e^{\color{blue}{\log \left({\left(2 \cdot im\right)}^{0.25}\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      10. metadata-eval54.2%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\color{blue}{\left(\frac{0.5}{2}\right)}}\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
      11. +-commutative54.2%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right) + \log 2\right)}} \]
      12. mul-1-neg54.2%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\color{blue}{\left(-\log \left(\frac{1}{im}\right)\right)} + \log 2\right)} \]
      13. log-rec54.2%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\left(-\color{blue}{\left(-\log im\right)}\right) + \log 2\right)} \]
      14. remove-double-neg54.2%

        \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\color{blue}{\log im} + \log 2\right)} \]
    6. Simplified58.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -0.075:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 6: 52.2% accurate, 2.0× speedup?

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

\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Derivation
  1. Initial program 38.4%

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

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

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

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

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

      \[\leadsto 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \]
    6. metadata-eval77.0%

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

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

    \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}\right)}^{2}} \]
  5. Step-by-step derivation
    1. unpow244.9%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \cdot e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}\right)} \]
    2. prod-exp44.9%

      \[\leadsto 0.5 \cdot \color{blue}{e^{0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)}} \]
    3. +-commutative44.9%

      \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right) + \log 2\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    4. mul-1-neg44.9%

      \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\color{blue}{\left(-\log \left(\frac{1}{im}\right)\right)} + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    5. log-rec44.9%

      \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\left(-\color{blue}{\left(-\log im\right)}\right) + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    6. remove-double-neg44.9%

      \[\leadsto 0.5 \cdot e^{0.25 \cdot \left(\color{blue}{\log im} + \log 2\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    7. +-commutative44.9%

      \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\left(\log 2 + \log im\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    8. log-prod45.0%

      \[\leadsto 0.5 \cdot e^{0.25 \cdot \color{blue}{\log \left(2 \cdot im\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    9. log-pow45.0%

      \[\leadsto 0.5 \cdot e^{\color{blue}{\log \left({\left(2 \cdot im\right)}^{0.25}\right)} + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    10. metadata-eval45.0%

      \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\color{blue}{\left(\frac{0.5}{2}\right)}}\right) + 0.25 \cdot \left(\log 2 + -1 \cdot \log \left(\frac{1}{im}\right)\right)} \]
    11. +-commutative45.0%

      \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \color{blue}{\left(-1 \cdot \log \left(\frac{1}{im}\right) + \log 2\right)}} \]
    12. mul-1-neg45.0%

      \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\color{blue}{\left(-\log \left(\frac{1}{im}\right)\right)} + \log 2\right)} \]
    13. log-rec45.0%

      \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\left(-\color{blue}{\left(-\log im\right)}\right) + \log 2\right)} \]
    14. remove-double-neg45.0%

      \[\leadsto 0.5 \cdot e^{\log \left({\left(2 \cdot im\right)}^{\left(\frac{0.5}{2}\right)}\right) + 0.25 \cdot \left(\color{blue}{\log im} + \log 2\right)} \]
  6. Simplified48.6%

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

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

Reproduce

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