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

Percentage Accurate: 41.4% → 89.9%
Time: 10.7s
Alternatives: 6
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 6 alternatives:

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

Initial Program: 41.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 {re}^{-0.5}\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 (pow re -0.5)))
   (* 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 * pow(re, -0.5));
	} 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.pow(re, -0.5));
	} 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.pow(re, -0.5))
	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 * (re ^ -0.5)));
	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 * (re ^ -0.5));
	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[Power[re, -0.5], $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 {re}^{-0.5}\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 12.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 35.9%

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

        \[\leadsto 0.5 \cdot \color{blue}{{\left(2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)\right)}^{0.5}} \]
      2. associate-*r*35.9%

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

        \[\leadsto 0.5 \cdot {\left(\color{blue}{1} \cdot \frac{{im}^{2}}{re}\right)}^{0.5} \]
      4. *-un-lft-identity35.9%

        \[\leadsto 0.5 \cdot {\color{blue}{\left(\frac{{im}^{2}}{re}\right)}}^{0.5} \]
    5. Applied egg-rr35.9%

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{{im}^{2}}{re}\right)}^{0.5}} \]
    6. Step-by-step derivation
      1. unpow1/235.9%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\frac{{im}^{2}}{re}}} \]
    7. Simplified35.9%

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left({re}^{\color{blue}{-0.5}} \cdot im\right) \]
    9. Applied egg-rr91.7%

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

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

    1. Initial program 46.2%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\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.8% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.38 \cdot 10^{+63}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1450:\\ \;\;\;\;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 -1.38e+63)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 1450.0)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.38e+63) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 1450.0) {
		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 <= (-1.38d+63)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 1450.0d0) 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 <= -1.38e+63) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 1450.0) {
		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 <= -1.38e+63:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 1450.0:
		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 <= -1.38e+63)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 1450.0)
		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 <= -1.38e+63)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 1450.0)
		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, -1.38e+63], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1450.0], 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 -1.38 \cdot 10^{+63}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq 1450:\\
\;\;\;\;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 < -1.38e63

    1. Initial program 30.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 -inf 83.1%

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

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

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

    if -1.38e63 < re < 1450

    1. Initial program 56.5%

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

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

    if 1450 < re

    1. Initial program 13.6%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{{\left(2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)\right)}^{0.5}} \]
      2. associate-*r*49.4%

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

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

        \[\leadsto 0.5 \cdot {\color{blue}{\left(\frac{{im}^{2}}{re}\right)}}^{0.5} \]
    5. Applied egg-rr49.4%

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{{im}^{2}}{re}\right)}^{0.5}} \]
    6. Step-by-step derivation
      1. unpow1/249.4%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\frac{{im}^{2}}{re}}} \]
    7. Simplified49.4%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.38 \cdot 10^{+63}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1450:\\ \;\;\;\;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: 76.0% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -7 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4500:\\ \;\;\;\;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+62)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 4500.0) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -7e+62) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 4500.0) {
		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+62)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 4500.0d0) 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+62) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 4500.0) {
		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+62:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 4500.0:
		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+62)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 4500.0)
		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+62)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 4500.0)
		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+62], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4500.0], 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^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

\mathbf{elif}\;re \leq 4500:\\
\;\;\;\;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 < -6.99999999999999967e62

    1. Initial program 30.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 -inf 83.1%

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

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

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

    if -6.99999999999999967e62 < re < 4500

    1. Initial program 56.5%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{\left(\mathsf{hypot}\left(re, im\right)\right)}^{2}} - \left(-re\right) \cdot \left(-re\right)}{\mathsf{hypot}\left(re, im\right) - \left(-re\right)}} \]
    4. Applied egg-rr46.9%

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

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

    if 4500 < re

    1. Initial program 13.6%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{{\left(2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)\right)}^{0.5}} \]
      2. associate-*r*49.4%

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

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

        \[\leadsto 0.5 \cdot {\color{blue}{\left(\frac{{im}^{2}}{re}\right)}}^{0.5} \]
    5. Applied egg-rr49.4%

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{{im}^{2}}{re}\right)}^{0.5}} \]
    6. Step-by-step derivation
      1. unpow1/249.4%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\frac{{im}^{2}}{re}}} \]
    7. Simplified49.4%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4500:\\ \;\;\;\;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: 64.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 5600:\\ \;\;\;\;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 5600.0) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
	double tmp;
	if (re <= 5600.0) {
		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 <= 5600.0d0) 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 <= 5600.0) {
		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 <= 5600.0:
		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 <= 5600.0)
		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 <= 5600.0)
		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, 5600.0], 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 5600:\\
\;\;\;\;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 2 regimes
  2. if re < 5600

    1. Initial program 49.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{\left(\mathsf{hypot}\left(re, im\right)\right)}^{2}} - \left(-re\right) \cdot \left(-re\right)}{\mathsf{hypot}\left(re, im\right) - \left(-re\right)}} \]
    4. Applied egg-rr34.7%

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

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

    if 5600 < re

    1. Initial program 13.6%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{{\left(2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)\right)}^{0.5}} \]
      2. associate-*r*49.4%

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

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

        \[\leadsto 0.5 \cdot {\color{blue}{\left(\frac{{im}^{2}}{re}\right)}}^{0.5} \]
    5. Applied egg-rr49.4%

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{{im}^{2}}{re}\right)}^{0.5}} \]
    6. Step-by-step derivation
      1. unpow1/249.4%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\frac{{im}^{2}}{re}}} \]
    7. Simplified49.4%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 5600:\\ \;\;\;\;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 5: 64.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 6400:\\ \;\;\;\;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 6400.0) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if (re <= 6400.0) {
		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 <= 6400.0d0) 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 <= 6400.0) {
		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 <= 6400.0:
		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 <= 6400.0)
		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 <= 6400.0)
		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, 6400.0], 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 6400:\\
\;\;\;\;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 2 regimes
  2. if re < 6400

    1. Initial program 49.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{\left(\mathsf{hypot}\left(re, im\right)\right)}^{2}} - \left(-re\right) \cdot \left(-re\right)}{\mathsf{hypot}\left(re, im\right) - \left(-re\right)}} \]
    4. Applied egg-rr34.7%

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

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

    if 6400 < re

    1. Initial program 13.6%

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right)} - 1\right) \]
      9. add-sqr-sqrt27.2%

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. Simplified74.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 6400:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 53.0% accurate, 2.0× 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 41.7%

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

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

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

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

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

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

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

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

Reproduce

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