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

Percentage Accurate: 41.8% → 88.5%
Time: 5.2s
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.8% accurate, 1.0× speedup?

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

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

Alternative 1: 88.5% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 48.0%

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

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

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

    if 1.02e18 < re

    1. Initial program 8.1%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. add-log-exp18.3%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(0 + \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right) \]
      8. add-sqr-sqrt86.6%

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

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

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

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

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

Alternative 2: 76.3% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{if}\;re \leq -7.8 \cdot 10^{+90}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;re \leq -3.3 \cdot 10^{+15}:\\
\;\;\;\;t_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -7.8000000000000004e90 or -1.54999999999999999e77 < re < -3.3e15

    1. Initial program 40.2%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified90.3%

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

    if -7.8000000000000004e90 < re < -1.54999999999999999e77

    1. Initial program 20.2%

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

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

    if -3.3e15 < re < 3.7e14

    1. Initial program 53.5%

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

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

    if 3.7e14 < re

    1. Initial program 8.1%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. add-log-exp18.0%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(0 + \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right) \]
      8. add-sqr-sqrt85.6%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7.8 \cdot 10^{+90}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.55 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{im}\right)\\ \mathbf{elif}\;re \leq -3.3 \cdot 10^{+15}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 3.7 \cdot 10^{+14}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 76.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -2.8 \cdot 10^{+91}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{+75}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -3.6 \cdot 10^{+16}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.4 \cdot 10^{+14}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0))))
        (t_1 (* 0.5 (sqrt (* 2.0 (- im re))))))
   (if (<= re -2.8e+91)
     t_0
     (if (<= re -4.2e+75)
       t_1
       (if (<= re -3.6e+16)
         t_0
         (if (<= re 2.4e+14) t_1 (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = 0.5 * sqrt((2.0 * (im - re)));
	double tmp;
	if (re <= -2.8e+91) {
		tmp = t_0;
	} else if (re <= -4.2e+75) {
		tmp = t_1;
	} else if (re <= -3.6e+16) {
		tmp = t_0;
	} else if (re <= 2.4e+14) {
		tmp = t_1;
	} 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) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = 0.5d0 * sqrt((2.0d0 * (im - re)))
    if (re <= (-2.8d+91)) then
        tmp = t_0
    else if (re <= (-4.2d+75)) then
        tmp = t_1
    else if (re <= (-3.6d+16)) then
        tmp = t_0
    else if (re <= 2.4d+14) then
        tmp = t_1
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = 0.5 * Math.sqrt((2.0 * (im - re)));
	double tmp;
	if (re <= -2.8e+91) {
		tmp = t_0;
	} else if (re <= -4.2e+75) {
		tmp = t_1;
	} else if (re <= -3.6e+16) {
		tmp = t_0;
	} else if (re <= 2.4e+14) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = 0.5 * math.sqrt((2.0 * (im - re)))
	tmp = 0
	if re <= -2.8e+91:
		tmp = t_0
	elif re <= -4.2e+75:
		tmp = t_1
	elif re <= -3.6e+16:
		tmp = t_0
	elif re <= 2.4e+14:
		tmp = t_1
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))))
	tmp = 0.0
	if (re <= -2.8e+91)
		tmp = t_0;
	elseif (re <= -4.2e+75)
		tmp = t_1;
	elseif (re <= -3.6e+16)
		tmp = t_0;
	elseif (re <= 2.4e+14)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = 0.5 * sqrt((2.0 * (im - re)));
	tmp = 0.0;
	if (re <= -2.8e+91)
		tmp = t_0;
	elseif (re <= -4.2e+75)
		tmp = t_1;
	elseif (re <= -3.6e+16)
		tmp = t_0;
	elseif (re <= 2.4e+14)
		tmp = t_1;
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -2.8e+91], t$95$0, If[LessEqual[re, -4.2e+75], t$95$1, If[LessEqual[re, -3.6e+16], t$95$0, If[LessEqual[re, 2.4e+14], t$95$1, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -2.8 \cdot 10^{+91}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -4.2 \cdot 10^{+75}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -3.6 \cdot 10^{+16}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 2.4 \cdot 10^{+14}:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -2.7999999999999999e91 or -4.19999999999999997e75 < re < -3.6e16

    1. Initial program 40.2%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified90.3%

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

    if -2.7999999999999999e91 < re < -4.19999999999999997e75 or -3.6e16 < re < 2.4e14

    1. Initial program 52.0%

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

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

    if 2.4e14 < re

    1. Initial program 8.1%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. add-log-exp18.0%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(0 + \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right) \]
      8. add-sqr-sqrt85.6%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.8 \cdot 10^{+91}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{+75}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -3.6 \cdot 10^{+16}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.4 \cdot 10^{+14}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 4: 75.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -7.8 \cdot 10^{+90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.55 \cdot 10^{+77}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -6.6 \cdot 10^{+18}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.35 \cdot 10^{+17}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))) (t_1 (* 0.5 (sqrt (* 2.0 im)))))
   (if (<= re -7.8e+90)
     t_0
     (if (<= re -1.55e+77)
       t_1
       (if (<= re -6.6e+18)
         t_0
         (if (<= re 2.35e+17) t_1 (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = 0.5 * sqrt((2.0 * im));
	double tmp;
	if (re <= -7.8e+90) {
		tmp = t_0;
	} else if (re <= -1.55e+77) {
		tmp = t_1;
	} else if (re <= -6.6e+18) {
		tmp = t_0;
	} else if (re <= 2.35e+17) {
		tmp = t_1;
	} 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) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = 0.5d0 * sqrt((2.0d0 * im))
    if (re <= (-7.8d+90)) then
        tmp = t_0
    else if (re <= (-1.55d+77)) then
        tmp = t_1
    else if (re <= (-6.6d+18)) then
        tmp = t_0
    else if (re <= 2.35d+17) then
        tmp = t_1
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((re * -4.0));
	double t_1 = 0.5 * Math.sqrt((2.0 * im));
	double tmp;
	if (re <= -7.8e+90) {
		tmp = t_0;
	} else if (re <= -1.55e+77) {
		tmp = t_1;
	} else if (re <= -6.6e+18) {
		tmp = t_0;
	} else if (re <= 2.35e+17) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = 0.5 * math.sqrt((2.0 * im))
	tmp = 0
	if re <= -7.8e+90:
		tmp = t_0
	elif re <= -1.55e+77:
		tmp = t_1
	elif re <= -6.6e+18:
		tmp = t_0
	elif re <= 2.35e+17:
		tmp = t_1
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = Float64(0.5 * sqrt(Float64(2.0 * im)))
	tmp = 0.0
	if (re <= -7.8e+90)
		tmp = t_0;
	elseif (re <= -1.55e+77)
		tmp = t_1;
	elseif (re <= -6.6e+18)
		tmp = t_0;
	elseif (re <= 2.35e+17)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = 0.5 * sqrt((2.0 * im));
	tmp = 0.0;
	if (re <= -7.8e+90)
		tmp = t_0;
	elseif (re <= -1.55e+77)
		tmp = t_1;
	elseif (re <= -6.6e+18)
		tmp = t_0;
	elseif (re <= 2.35e+17)
		tmp = t_1;
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -7.8e+90], t$95$0, If[LessEqual[re, -1.55e+77], t$95$1, If[LessEqual[re, -6.6e+18], t$95$0, If[LessEqual[re, 2.35e+17], t$95$1, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{if}\;re \leq -7.8 \cdot 10^{+90}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -1.55 \cdot 10^{+77}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -6.6 \cdot 10^{+18}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 2.35 \cdot 10^{+17}:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -7.8000000000000004e90 or -1.54999999999999999e77 < re < -6.6e18

    1. Initial program 40.2%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified90.3%

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

    if -7.8000000000000004e90 < re < -1.54999999999999999e77 or -6.6e18 < re < 2.35e17

    1. Initial program 52.0%

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

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

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

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

    if 2.35e17 < re

    1. Initial program 8.1%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot im}{re}}} \]
    5. Step-by-step derivation
      1. add-log-exp18.0%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(0 + \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{re}}\right) \]
      8. add-sqr-sqrt85.6%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7.8 \cdot 10^{+90}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.55 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq -6.6 \cdot 10^{+18}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.35 \cdot 10^{+17}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 5: 63.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.3 \cdot 10^{+91} \lor \neg \left(re \leq -5.2 \cdot 10^{+76}\right) \land re \leq -1.1 \cdot 10^{+18}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re -1.3e+91) (and (not (<= re -5.2e+76)) (<= re -1.1e+18)))
   (* 0.5 (sqrt (* re -4.0)))
   (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
	double tmp;
	if ((re <= -1.3e+91) || (!(re <= -5.2e+76) && (re <= -1.1e+18))) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((re <= (-1.3d+91)) .or. (.not. (re <= (-5.2d+76))) .and. (re <= (-1.1d+18))) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((re <= -1.3e+91) || (!(re <= -5.2e+76) && (re <= -1.1e+18))) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= -1.3e+91) or (not (re <= -5.2e+76) and (re <= -1.1e+18)):
		tmp = 0.5 * math.sqrt((re * -4.0))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= -1.3e+91) || (!(re <= -5.2e+76) && (re <= -1.1e+18)))
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= -1.3e+91) || (~((re <= -5.2e+76)) && (re <= -1.1e+18)))
		tmp = 0.5 * sqrt((re * -4.0));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, -1.3e+91], And[N[Not[LessEqual[re, -5.2e+76]], $MachinePrecision], LessEqual[re, -1.1e+18]]], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.3 \cdot 10^{+91} \lor \neg \left(re \leq -5.2 \cdot 10^{+76}\right) \land re \leq -1.1 \cdot 10^{+18}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -1.3e91 or -5.1999999999999999e76 < re < -1.1e18

    1. Initial program 40.2%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    4. Simplified90.3%

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

    if -1.3e91 < re < -5.1999999999999999e76 or -1.1e18 < re

    1. Initial program 36.4%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.3 \cdot 10^{+91} \lor \neg \left(re \leq -5.2 \cdot 10^{+76}\right) \land re \leq -1.1 \cdot 10^{+18}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 6: 52.6% 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 37.2%

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

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

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

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

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

Reproduce

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