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

Percentage Accurate: 41.4% → 89.9%
Time: 7.5s
Alternatives: 6
Speedup: 2.1×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 6 alternatives:

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

Initial Program: 41.4% accurate, 1.0× speedup?

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

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

Alternative 1: 89.9% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\

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


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

    1. Initial program 6.4%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    4. Taylor expanded in im around 0 92.8%

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

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

    1. Initial program 46.8%

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

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

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

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

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

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

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

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

Alternative 2: 76.5% accurate, 1.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.95 \cdot 10^{+19}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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

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


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

    1. Initial program 43.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    4. Step-by-step derivation
      1. *-commutative86.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    5. Simplified86.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -2.95e19 < re < 1e-84

    1. Initial program 55.4%

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

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

    if 1e-84 < re

    1. Initial program 11.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. div-inv46.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{{im}^{2} \cdot \frac{1}{re}}} \]
      2. sqrt-prod58.0%

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

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

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

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

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

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

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

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

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

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

Alternative 3: 75.7% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -7 \cdot 10^{+17}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 1.8 \cdot 10^{-84}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


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

    1. Initial program 43.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    4. Step-by-step derivation
      1. *-commutative86.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    5. Simplified86.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -7e17 < re < 1.80000000000000002e-84

    1. Initial program 55.4%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    5. Simplified85.0%

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

    if 1.80000000000000002e-84 < re

    1. Initial program 11.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. div-inv46.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{{im}^{2} \cdot \frac{1}{re}}} \]
      2. sqrt-prod58.0%

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

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

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

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

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

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

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

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

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

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

Alternative 4: 75.6% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.1 \cdot 10^{+18}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 1.9 \cdot 10^{-84}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


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

    1. Initial program 43.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    4. Step-by-step derivation
      1. *-commutative86.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    5. Simplified86.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -4.1e18 < re < 1.89999999999999993e-84

    1. Initial program 55.4%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
    5. Simplified85.0%

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

    if 1.89999999999999993e-84 < re

    1. Initial program 11.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re}}} \]
    4. Taylor expanded in im around 0 76.3%

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

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

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

        \[\leadsto 0.5 \cdot \left(\sqrt{{re}^{\color{blue}{\left(2 \cdot -0.5\right)}}} \cdot im\right) \]
      4. pow-sqr76.3%

        \[\leadsto 0.5 \cdot \left(\sqrt{\color{blue}{{re}^{-0.5} \cdot {re}^{-0.5}}} \cdot im\right) \]
      5. rem-sqrt-square76.3%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left|{re}^{-0.5}\right|} \cdot im\right) \]
      6. rem-square-sqrt75.9%

        \[\leadsto 0.5 \cdot \left(\left|\color{blue}{\sqrt{{re}^{-0.5}} \cdot \sqrt{{re}^{-0.5}}}\right| \cdot im\right) \]
      7. fabs-sqr75.9%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{{re}^{-0.5}} \cdot \sqrt{{re}^{-0.5}}\right)} \cdot im\right) \]
      8. rem-square-sqrt76.3%

        \[\leadsto 0.5 \cdot \left(\color{blue}{{re}^{-0.5}} \cdot im\right) \]
      9. exp-to-pow72.4%

        \[\leadsto 0.5 \cdot \left(\color{blue}{e^{\log re \cdot -0.5}} \cdot im\right) \]
      10. metadata-eval72.4%

        \[\leadsto 0.5 \cdot \left(e^{\log re \cdot \color{blue}{\left(-0.5\right)}} \cdot im\right) \]
      11. distribute-rgt-neg-in72.4%

        \[\leadsto 0.5 \cdot \left(e^{\color{blue}{-\log re \cdot 0.5}} \cdot im\right) \]
      12. exp-neg72.4%

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

        \[\leadsto 0.5 \cdot \left(\frac{1}{\color{blue}{{re}^{0.5}}} \cdot im\right) \]
      14. unpow1/276.1%

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{1 \cdot im}{\sqrt{re}}} \]
      16. *-lft-identity76.1%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    6. Simplified76.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.1 \cdot 10^{+18}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.9 \cdot 10^{-84}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 64.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.6 \cdot 10^{+17}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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


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

    1. Initial program 43.8%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
    4. Step-by-step derivation
      1. *-commutative86.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    5. Simplified86.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]

    if -6.6e17 < re

    1. Initial program 39.7%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -6.6 \cdot 10^{+17}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 51.6% accurate, 2.1× speedup?

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  5. Simplified54.4%

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

    \[\leadsto 0.5 \cdot \sqrt{im \cdot 2} \]
  7. Add Preprocessing

Reproduce

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