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

Percentage Accurate: 41.1% → 87.7%
Time: 7.3s
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.1% 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: 87.7% accurate, 1.0× speedup?

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

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

\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 < 1.55000000000000009e66

    1. Initial program 53.6%

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

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

        \[\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-neg53.6%

        \[\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-neg53.6%

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

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

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

    if 1.55000000000000009e66 < re

    1. Initial program 10.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 75.3% 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 -1.45 \cdot 10^{+63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -3.4 \cdot 10^{-60}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 8.8 \cdot 10^{+21}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \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 -1.45e+63)
     t_0
     (if (<= re -3.4e-60)
       t_1
       (if (<= re -2.7e-117)
         t_0
         (if (<= re 8.8e+21) t_1 (* 0.5 (* im (pow re -0.5)))))))))
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 <= -1.45e+63) {
		tmp = t_0;
	} else if (re <= -3.4e-60) {
		tmp = t_1;
	} else if (re <= -2.7e-117) {
		tmp = t_0;
	} else if (re <= 8.8e+21) {
		tmp = t_1;
	} 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) :: 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 <= (-1.45d+63)) then
        tmp = t_0
    else if (re <= (-3.4d-60)) then
        tmp = t_1
    else if (re <= (-2.7d-117)) then
        tmp = t_0
    else if (re <= 8.8d+21) then
        tmp = t_1
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    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 <= -1.45e+63) {
		tmp = t_0;
	} else if (re <= -3.4e-60) {
		tmp = t_1;
	} else if (re <= -2.7e-117) {
		tmp = t_0;
	} else if (re <= 8.8e+21) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	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 <= -1.45e+63:
		tmp = t_0
	elif re <= -3.4e-60:
		tmp = t_1
	elif re <= -2.7e-117:
		tmp = t_0
	elif re <= 8.8e+21:
		tmp = t_1
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	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 <= -1.45e+63)
		tmp = t_0;
	elseif (re <= -3.4e-60)
		tmp = t_1;
	elseif (re <= -2.7e-117)
		tmp = t_0;
	elseif (re <= 8.8e+21)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	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 <= -1.45e+63)
		tmp = t_0;
	elseif (re <= -3.4e-60)
		tmp = t_1;
	elseif (re <= -2.7e-117)
		tmp = t_0;
	elseif (re <= 8.8e+21)
		tmp = t_1;
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	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, -1.45e+63], t$95$0, If[LessEqual[re, -3.4e-60], t$95$1, If[LessEqual[re, -2.7e-117], t$95$0, If[LessEqual[re, 8.8e+21], t$95$1, N[(0.5 * N[(im * N[Power[re, -0.5], $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 -1.45 \cdot 10^{+63}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -3.4 \cdot 10^{-60}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 8.8 \cdot 10^{+21}:\\
\;\;\;\;t_1\\

\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.45e63 or -3.40000000000000007e-60 < re < -2.70000000000000003e-117

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

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

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

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

    if -1.45e63 < re < -3.40000000000000007e-60 or -2.70000000000000003e-117 < re < 8.8e21

    1. Initial program 61.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 83.0%

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

    if 8.8e21 < re

    1. Initial program 11.8%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.45 \cdot 10^{+63}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -3.4 \cdot 10^{-60}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 8.8 \cdot 10^{+21}:\\ \;\;\;\;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: 74.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 -3.5 \cdot 10^{+55}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -2.2 \cdot 10^{-61}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 7.5 \cdot 10^{+54}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \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 -3.5e+55)
     t_0
     (if (<= re -2.2e-61)
       t_1
       (if (<= re -2.7e-117)
         t_0
         (if (<= re 7.5e+54) t_1 (* 0.5 (* im (pow re -0.5)))))))))
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 <= -3.5e+55) {
		tmp = t_0;
	} else if (re <= -2.2e-61) {
		tmp = t_1;
	} else if (re <= -2.7e-117) {
		tmp = t_0;
	} else if (re <= 7.5e+54) {
		tmp = t_1;
	} 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) :: 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 <= (-3.5d+55)) then
        tmp = t_0
    else if (re <= (-2.2d-61)) then
        tmp = t_1
    else if (re <= (-2.7d-117)) then
        tmp = t_0
    else if (re <= 7.5d+54) then
        tmp = t_1
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    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 <= -3.5e+55) {
		tmp = t_0;
	} else if (re <= -2.2e-61) {
		tmp = t_1;
	} else if (re <= -2.7e-117) {
		tmp = t_0;
	} else if (re <= 7.5e+54) {
		tmp = t_1;
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	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 <= -3.5e+55:
		tmp = t_0
	elif re <= -2.2e-61:
		tmp = t_1
	elif re <= -2.7e-117:
		tmp = t_0
	elif re <= 7.5e+54:
		tmp = t_1
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	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 <= -3.5e+55)
		tmp = t_0;
	elseif (re <= -2.2e-61)
		tmp = t_1;
	elseif (re <= -2.7e-117)
		tmp = t_0;
	elseif (re <= 7.5e+54)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	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 <= -3.5e+55)
		tmp = t_0;
	elseif (re <= -2.2e-61)
		tmp = t_1;
	elseif (re <= -2.7e-117)
		tmp = t_0;
	elseif (re <= 7.5e+54)
		tmp = t_1;
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	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, -3.5e+55], t$95$0, If[LessEqual[re, -2.2e-61], t$95$1, If[LessEqual[re, -2.7e-117], t$95$0, If[LessEqual[re, 7.5e+54], t$95$1, N[(0.5 * N[(im * N[Power[re, -0.5], $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 -3.5 \cdot 10^{+55}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -2.2 \cdot 10^{-61}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 7.5 \cdot 10^{+54}:\\
\;\;\;\;t_1\\

\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 < -3.5000000000000001e55 or -2.20000000000000009e-61 < re < -2.70000000000000003e-117

    1. Initial program 44.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 81.5%

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

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

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

    if -3.5000000000000001e55 < re < -2.20000000000000009e-61 or -2.70000000000000003e-117 < re < 7.50000000000000042e54

    1. Initial program 58.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 81.0%

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

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

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

    if 7.50000000000000042e54 < re

    1. Initial program 10.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 inf 43.5%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.5 \cdot 10^{+55}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -2.2 \cdot 10^{-61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 7.5 \cdot 10^{+54}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 74.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 -1.32 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.9 \cdot 10^{-61}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{+55}:\\ \;\;\;\;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 -1.32e+54)
     t_0
     (if (<= re -1.9e-61)
       t_1
       (if (<= re -2.7e-117)
         t_0
         (if (<= re 1.2e+55) 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 <= -1.32e+54) {
		tmp = t_0;
	} else if (re <= -1.9e-61) {
		tmp = t_1;
	} else if (re <= -2.7e-117) {
		tmp = t_0;
	} else if (re <= 1.2e+55) {
		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 <= (-1.32d+54)) then
        tmp = t_0
    else if (re <= (-1.9d-61)) then
        tmp = t_1
    else if (re <= (-2.7d-117)) then
        tmp = t_0
    else if (re <= 1.2d+55) 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 <= -1.32e+54) {
		tmp = t_0;
	} else if (re <= -1.9e-61) {
		tmp = t_1;
	} else if (re <= -2.7e-117) {
		tmp = t_0;
	} else if (re <= 1.2e+55) {
		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 <= -1.32e+54:
		tmp = t_0
	elif re <= -1.9e-61:
		tmp = t_1
	elif re <= -2.7e-117:
		tmp = t_0
	elif re <= 1.2e+55:
		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 <= -1.32e+54)
		tmp = t_0;
	elseif (re <= -1.9e-61)
		tmp = t_1;
	elseif (re <= -2.7e-117)
		tmp = t_0;
	elseif (re <= 1.2e+55)
		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 <= -1.32e+54)
		tmp = t_0;
	elseif (re <= -1.9e-61)
		tmp = t_1;
	elseif (re <= -2.7e-117)
		tmp = t_0;
	elseif (re <= 1.2e+55)
		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, -1.32e+54], t$95$0, If[LessEqual[re, -1.9e-61], t$95$1, If[LessEqual[re, -2.7e-117], t$95$0, If[LessEqual[re, 1.2e+55], 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 -1.32 \cdot 10^{+54}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq -1.9 \cdot 10^{-61}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 1.2 \cdot 10^{+55}:\\
\;\;\;\;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 < -1.3200000000000001e54 or -1.8999999999999999e-61 < re < -2.70000000000000003e-117

    1. Initial program 44.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 81.5%

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

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

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

    if -1.3200000000000001e54 < re < -1.8999999999999999e-61 or -2.70000000000000003e-117 < re < 1.2e55

    1. Initial program 58.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 81.0%

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

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

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

    if 1.2e55 < re

    1. Initial program 10.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 inf 43.5%

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

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

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

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

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

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

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

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

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

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

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

      \[\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-def84.6%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.32 \cdot 10^{+54}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.9 \cdot 10^{-61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{+55}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 64.0% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{+46} \lor \neg \left(re \leq -7.5 \cdot 10^{-59}\right) \land re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;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 -3.1e+46) (and (not (<= re -7.5e-59)) (<= re -2.7e-117)))
   (* 0.5 (sqrt (* re -4.0)))
   (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
	double tmp;
	if ((re <= -3.1e+46) || (!(re <= -7.5e-59) && (re <= -2.7e-117))) {
		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 <= (-3.1d+46)) .or. (.not. (re <= (-7.5d-59))) .and. (re <= (-2.7d-117))) 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 <= -3.1e+46) || (!(re <= -7.5e-59) && (re <= -2.7e-117))) {
		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 <= -3.1e+46) or (not (re <= -7.5e-59) and (re <= -2.7e-117)):
		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 <= -3.1e+46) || (!(re <= -7.5e-59) && (re <= -2.7e-117)))
		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 <= -3.1e+46) || (~((re <= -7.5e-59)) && (re <= -2.7e-117)))
		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, -3.1e+46], And[N[Not[LessEqual[re, -7.5e-59]], $MachinePrecision], LessEqual[re, -2.7e-117]]], 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 -3.1 \cdot 10^{+46} \lor \neg \left(re \leq -7.5 \cdot 10^{-59}\right) \land re \leq -2.7 \cdot 10^{-117}:\\
\;\;\;\;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 < -3.09999999999999975e46 or -7.50000000000000019e-59 < re < -2.70000000000000003e-117

    1. Initial program 44.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 81.5%

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

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

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

    if -3.09999999999999975e46 < re < -7.50000000000000019e-59 or -2.70000000000000003e-117 < re

    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 63.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{+46} \lor \neg \left(re \leq -7.5 \cdot 10^{-59}\right) \land re \leq -2.7 \cdot 10^{-117}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 52.7% 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 45.4%

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

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

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

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

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

Reproduce

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