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

Percentage Accurate: 41.6% → 85.0%
Time: 9.3s
Alternatives: 10
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 10 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.6% 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: 85.0% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 9.5 \cdot 10^{-54}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \mathsf{fma}\left(im, \sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right), \sqrt{\frac{1}{{re}^{5}}} \cdot \left(-0.0625 \cdot \left({im}^{3} \cdot \frac{\sqrt{2}}{\sqrt{0.5}}\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re 9.5e-54)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (*
    0.5
    (fma
     im
     (* (sqrt 2.0) (* (sqrt 0.5) (sqrt (/ 1.0 re))))
     (*
      (sqrt (/ 1.0 (pow re 5.0)))
      (* -0.0625 (* (pow im 3.0) (/ (sqrt 2.0) (sqrt 0.5)))))))))
double code(double re, double im) {
	double tmp;
	if (re <= 9.5e-54) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else {
		tmp = 0.5 * fma(im, (sqrt(2.0) * (sqrt(0.5) * sqrt((1.0 / re)))), (sqrt((1.0 / pow(re, 5.0))) * (-0.0625 * (pow(im, 3.0) * (sqrt(2.0) / sqrt(0.5))))));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (re <= 9.5e-54)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	else
		tmp = Float64(0.5 * fma(im, Float64(sqrt(2.0) * Float64(sqrt(0.5) * sqrt(Float64(1.0 / re)))), Float64(sqrt(Float64(1.0 / (re ^ 5.0))) * Float64(-0.0625 * Float64((im ^ 3.0) * Float64(sqrt(2.0) / sqrt(0.5)))))));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[re, 9.5e-54], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[(N[Sqrt[2.0], $MachinePrecision] * N[(N[Sqrt[0.5], $MachinePrecision] * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Sqrt[N[(1.0 / N[Power[re, 5.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(-0.0625 * N[(N[Power[im, 3.0], $MachinePrecision] * N[(N[Sqrt[2.0], $MachinePrecision] / N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \mathsf{fma}\left(im, \sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right), \sqrt{\frac{1}{{re}^{5}}} \cdot \left(-0.0625 \cdot \left({im}^{3} \cdot \frac{\sqrt{2}}{\sqrt{0.5}}\right)\right)\right)\\


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

    1. Initial program 52.4%

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

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

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

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

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

    if 9.4999999999999994e-54 < re

    1. Initial program 12.2%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(-0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right) + \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    4. Step-by-step derivation
      1. +-commutative73.8%

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{im \cdot \left(\left(\sqrt{0.5} \cdot \sqrt{2}\right) \cdot \sqrt{\frac{1}{re}}\right)} + -0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right)\right) \]
      3. *-commutative73.8%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\left(\sqrt{\frac{1}{re}} \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right)\right)} + -0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right)\right) \]
      4. fma-define73.8%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{fma}\left(im, \sqrt{\frac{1}{re}} \cdot \left(\sqrt{0.5} \cdot \sqrt{2}\right), -0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right)\right)} \]
      5. *-commutative73.8%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, \color{blue}{\left(\sqrt{0.5} \cdot \sqrt{2}\right) \cdot \sqrt{\frac{1}{re}}}, -0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right)\right) \]
      6. *-commutative73.8%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, \color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}, -0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right)\right) \]
      7. associate-*l*74.2%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, \color{blue}{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)}, -0.0625 \cdot \left(\frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}} \cdot \sqrt{\frac{1}{{re}^{5}}}\right)\right) \]
      8. associate-*r*74.2%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, \sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right), \color{blue}{\left(-0.0625 \cdot \frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}}\right) \cdot \sqrt{\frac{1}{{re}^{5}}}}\right) \]
      9. *-commutative74.2%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, \sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right), \color{blue}{\sqrt{\frac{1}{{re}^{5}}} \cdot \left(-0.0625 \cdot \frac{{im}^{3} \cdot \sqrt{2}}{\sqrt{0.5}}\right)}\right) \]
      10. associate-/l*74.2%

        \[\leadsto 0.5 \cdot \mathsf{fma}\left(im, \sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right), \sqrt{\frac{1}{{re}^{5}}} \cdot \left(-0.0625 \cdot \color{blue}{\left({im}^{3} \cdot \frac{\sqrt{2}}{\sqrt{0.5}}\right)}\right)\right) \]
    5. Simplified74.2%

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

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

Alternative 2: 63.9% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 36.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 -inf 74.1%

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

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

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

    if -2.5e26 < re

    1. Initial program 43.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 0 63.5%

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

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

Alternative 3: 79.3% accurate, 1.0× speedup?

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

\\
0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}
\end{array}
Derivation
  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-define79.9%

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

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
  4. Add Preprocessing
  5. Final simplification79.9%

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)} \]
  6. Add Preprocessing

Alternative 4: 64.2% accurate, 1.8× speedup?

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

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

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


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

    1. Initial program 36.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 -inf 74.1%

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

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

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

    if -5.8e26 < re

    1. Initial program 43.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 0 64.0%

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

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

Alternative 5: 63.9% accurate, 1.8× speedup?

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

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

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


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

    1. Initial program 36.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 -inf 74.1%

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

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

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

    if -4.6000000000000001e26 < re

    1. Initial program 43.9%

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

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

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

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

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

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

Alternative 6: 63.9% accurate, 1.9× speedup?

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

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

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


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

    1. Initial program 36.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 -inf 74.1%

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

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

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

    if -1.2200000000000001e27 < re

    1. Initial program 43.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 0 63.1%

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

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

Alternative 7: 55.8% accurate, 1.9× speedup?

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

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


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

    1. Initial program 45.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 62.2%

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

    if 1.85e195 < re

    1. Initial program 2.2%

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

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

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

Alternative 8: 65.9% accurate, 1.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\


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

    1. Initial program 53.2%

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

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

    if 1.7500000000000001e-122 < re

    1. Initial program 17.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 74.8%

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)} \]
      2. *-un-lft-identity14.1%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right)} \]
      4. metadata-eval14.1%

        \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{im \cdot \left(\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)}\right)\right) \]
      5. add-log-exp74.8%

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

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

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

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

        \[\leadsto 0.5 \cdot \left(0 + im \cdot \color{blue}{\sqrt{\frac{1}{re}}}\right) \]
      10. sqrt-div75.6%

        \[\leadsto 0.5 \cdot \left(0 + im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{re}}}\right) \]
      11. metadata-eval75.6%

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

        \[\leadsto 0.5 \cdot \left(0 + \color{blue}{\frac{im}{\sqrt{re}}}\right) \]
    7. Applied egg-rr75.8%

      \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \frac{im}{\sqrt{re}}\right)} \]
    8. Step-by-step derivation
      1. +-lft-identity75.8%

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

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    10. Taylor expanded in im around 0 75.7%

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

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

Alternative 9: 65.9% accurate, 1.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\


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

    1. Initial program 53.2%

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

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

    if 5.4000000000000002e-123 < re

    1. Initial program 17.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 74.8%

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

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

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

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

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

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

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

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

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

Alternative 10: 51.8% 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 42.3%

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{2}\right)} \]
  4. Step-by-step derivation
    1. add-log-exp9.6%

      \[\leadsto 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
    2. *-un-lft-identity9.6%

      \[\leadsto 0.5 \cdot \log \color{blue}{\left(1 \cdot e^{\sqrt{im} \cdot \sqrt{2}}\right)} \]
    3. log-prod9.6%

      \[\leadsto 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right)} \]
    4. metadata-eval9.6%

      \[\leadsto 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{im} \cdot \sqrt{2}}\right)\right) \]
    5. add-log-exp55.6%

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

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(0 + \sqrt{im \cdot 2}\right)} \]
  6. Step-by-step derivation
    1. +-lft-identity55.9%

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

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

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

Reproduce

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