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

Percentage Accurate: 49.4% → 92.5%
Time: 12.1s
Alternatives: 11
Speedup: 1.9×

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 11 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: 49.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: 92.5% accurate, 0.7× speedup?

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

\\
0.5 \cdot \left(\sqrt{\mathsf{hypot}\left(re, im\right) - re} \cdot \sqrt{2}\right)
\end{array}
Derivation
  1. Initial program 53.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. *-commutative53.5%

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

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

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

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

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

Alternative 2: 70.7% accurate, 1.0× speedup?

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

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

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

\mathbf{elif}\;re \leq 2.1 \cdot 10^{+206}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{re}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -5.2e39

    1. Initial program 49.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 -inf 83.2%

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

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

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

    if -5.2e39 < re < 2.1499999999999999e32

    1. Initial program 62.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 78.4%

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

    if 2.1499999999999999e32 < re < 2.09999999999999987e206

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

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

    if 2.09999999999999987e206 < re

    1. Initial program 3.0%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{1 \cdot \frac{1}{re}}}\right) \]
      5. *-un-lft-identity62.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -5.2 \cdot 10^{+39}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.15 \cdot 10^{+32}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{+206}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 70.7% accurate, 1.0× speedup?

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

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

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

\mathbf{elif}\;re \leq 8 \cdot 10^{+209}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{re}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -3.99999999999999976e39

    1. Initial program 49.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. *-commutative49.5%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\sqrt{\color{blue}{re \cdot -2}} \cdot \sqrt{2}\right) \]
    7. Simplified84.3%

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

    if -3.99999999999999976e39 < re < 6.3999999999999998e32

    1. Initial program 62.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 78.4%

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

    if 6.3999999999999998e32 < re < 8.0000000000000006e209

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

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

    if 8.0000000000000006e209 < re

    1. Initial program 3.0%

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{\sqrt{1 \cdot \frac{1}{re}}}\right) \]
      5. *-un-lft-identity62.5%

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

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

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

Alternative 4: 93.0% accurate, 1.0× speedup?

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

\\
0.5 \cdot \sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 2}
\end{array}
Derivation
  1. Initial program 53.5%

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

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

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

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

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

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

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

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

Alternative 5: 67.5% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{if}\;re \leq -1.3 \cdot 10^{+31}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq -1.75 \cdot 10^{-39}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq 1.16 \cdot 10^{+33}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{+167}:\\ \;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\ \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 (* im 2.0)))))
   (if (<= re -1.3e+31)
     t_0
     (if (<= re -1.75e-39)
       t_1
       (if (<= re -4.2e-129)
         t_0
         (if (<= re 1.16e+33)
           t_1
           (if (<= re 1.2e+167)
             (* 0.5 (+ (+ im 1.0) -1.0))
             (* 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((im * 2.0));
	double tmp;
	if (re <= -1.3e+31) {
		tmp = t_0;
	} else if (re <= -1.75e-39) {
		tmp = t_1;
	} else if (re <= -4.2e-129) {
		tmp = t_0;
	} else if (re <= 1.16e+33) {
		tmp = t_1;
	} else if (re <= 1.2e+167) {
		tmp = 0.5 * ((im + 1.0) + -1.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) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((re * (-4.0d0)))
    t_1 = 0.5d0 * sqrt((im * 2.0d0))
    if (re <= (-1.3d+31)) then
        tmp = t_0
    else if (re <= (-1.75d-39)) then
        tmp = t_1
    else if (re <= (-4.2d-129)) then
        tmp = t_0
    else if (re <= 1.16d+33) then
        tmp = t_1
    else if (re <= 1.2d+167) then
        tmp = 0.5d0 * ((im + 1.0d0) + (-1.0d0))
    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((im * 2.0));
	double tmp;
	if (re <= -1.3e+31) {
		tmp = t_0;
	} else if (re <= -1.75e-39) {
		tmp = t_1;
	} else if (re <= -4.2e-129) {
		tmp = t_0;
	} else if (re <= 1.16e+33) {
		tmp = t_1;
	} else if (re <= 1.2e+167) {
		tmp = 0.5 * ((im + 1.0) + -1.0);
	} 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((im * 2.0))
	tmp = 0
	if re <= -1.3e+31:
		tmp = t_0
	elif re <= -1.75e-39:
		tmp = t_1
	elif re <= -4.2e-129:
		tmp = t_0
	elif re <= 1.16e+33:
		tmp = t_1
	elif re <= 1.2e+167:
		tmp = 0.5 * ((im + 1.0) + -1.0)
	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(im * 2.0)))
	tmp = 0.0
	if (re <= -1.3e+31)
		tmp = t_0;
	elseif (re <= -1.75e-39)
		tmp = t_1;
	elseif (re <= -4.2e-129)
		tmp = t_0;
	elseif (re <= 1.16e+33)
		tmp = t_1;
	elseif (re <= 1.2e+167)
		tmp = Float64(0.5 * Float64(Float64(im + 1.0) + -1.0));
	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((im * 2.0));
	tmp = 0.0;
	if (re <= -1.3e+31)
		tmp = t_0;
	elseif (re <= -1.75e-39)
		tmp = t_1;
	elseif (re <= -4.2e-129)
		tmp = t_0;
	elseif (re <= 1.16e+33)
		tmp = t_1;
	elseif (re <= 1.2e+167)
		tmp = 0.5 * ((im + 1.0) + -1.0);
	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[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1.3e+31], t$95$0, If[LessEqual[re, -1.75e-39], t$95$1, If[LessEqual[re, -4.2e-129], t$95$0, If[LessEqual[re, 1.16e+33], t$95$1, If[LessEqual[re, 1.2e+167], N[(0.5 * N[(N[(im + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision], 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{im \cdot 2}\\
\mathbf{if}\;re \leq -1.3 \cdot 10^{+31}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq -1.75 \cdot 10^{-39}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq 1.16 \cdot 10^{+33}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;re \leq 1.2 \cdot 10^{+167}:\\
\;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -1.3e31 or -1.75e-39 < re < -4.2e-129

    1. Initial program 60.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 77.5%

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

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

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

    if -1.3e31 < re < -1.75e-39 or -4.2e-129 < re < 1.16000000000000001e33

    1. Initial program 57.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 83.8%

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

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

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

    if 1.16000000000000001e33 < re < 1.19999999999999999e167

    1. Initial program 57.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(im \cdot {re}^{\color{blue}{-0.5}}\right)}^{3}} \]
    7. Applied egg-rr70.6%

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

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

    if 1.19999999999999999e167 < re

    1. Initial program 3.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.3 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq -1.75 \cdot 10^{-39}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.16 \cdot 10^{+33}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{+167}:\\ \;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 67.6% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{if}\;re \leq -2.5 \cdot 10^{+23}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq -5.1 \cdot 10^{-39}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq 4.2 \cdot 10^{+26}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* re -4.0)))) (t_1 (* 0.5 (sqrt (* im 2.0)))))
   (if (<= re -2.5e+23)
     t_0
     (if (<= re -5.1e-39)
       t_1
       (if (<= re -4.2e-129)
         t_0
         (if (<= re 4.2e+26) t_1 (* 0.5 (+ (+ im 1.0) -1.0))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((re * -4.0));
	double t_1 = 0.5 * sqrt((im * 2.0));
	double tmp;
	if (re <= -2.5e+23) {
		tmp = t_0;
	} else if (re <= -5.1e-39) {
		tmp = t_1;
	} else if (re <= -4.2e-129) {
		tmp = t_0;
	} else if (re <= 4.2e+26) {
		tmp = t_1;
	} else {
		tmp = 0.5 * ((im + 1.0) + -1.0);
	}
	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((im * 2.0d0))
    if (re <= (-2.5d+23)) then
        tmp = t_0
    else if (re <= (-5.1d-39)) then
        tmp = t_1
    else if (re <= (-4.2d-129)) then
        tmp = t_0
    else if (re <= 4.2d+26) then
        tmp = t_1
    else
        tmp = 0.5d0 * ((im + 1.0d0) + (-1.0d0))
    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((im * 2.0));
	double tmp;
	if (re <= -2.5e+23) {
		tmp = t_0;
	} else if (re <= -5.1e-39) {
		tmp = t_1;
	} else if (re <= -4.2e-129) {
		tmp = t_0;
	} else if (re <= 4.2e+26) {
		tmp = t_1;
	} else {
		tmp = 0.5 * ((im + 1.0) + -1.0);
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((re * -4.0))
	t_1 = 0.5 * math.sqrt((im * 2.0))
	tmp = 0
	if re <= -2.5e+23:
		tmp = t_0
	elif re <= -5.1e-39:
		tmp = t_1
	elif re <= -4.2e-129:
		tmp = t_0
	elif re <= 4.2e+26:
		tmp = t_1
	else:
		tmp = 0.5 * ((im + 1.0) + -1.0)
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(re * -4.0)))
	t_1 = Float64(0.5 * sqrt(Float64(im * 2.0)))
	tmp = 0.0
	if (re <= -2.5e+23)
		tmp = t_0;
	elseif (re <= -5.1e-39)
		tmp = t_1;
	elseif (re <= -4.2e-129)
		tmp = t_0;
	elseif (re <= 4.2e+26)
		tmp = t_1;
	else
		tmp = Float64(0.5 * Float64(Float64(im + 1.0) + -1.0));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((re * -4.0));
	t_1 = 0.5 * sqrt((im * 2.0));
	tmp = 0.0;
	if (re <= -2.5e+23)
		tmp = t_0;
	elseif (re <= -5.1e-39)
		tmp = t_1;
	elseif (re <= -4.2e-129)
		tmp = t_0;
	elseif (re <= 4.2e+26)
		tmp = t_1;
	else
		tmp = 0.5 * ((im + 1.0) + -1.0);
	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[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -2.5e+23], t$95$0, If[LessEqual[re, -5.1e-39], t$95$1, If[LessEqual[re, -4.2e-129], t$95$0, If[LessEqual[re, 4.2e+26], t$95$1, N[(0.5 * N[(N[(im + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq -5.1 \cdot 10^{-39}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;re \leq -4.2 \cdot 10^{-129}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq 4.2 \cdot 10^{+26}:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -2.5e23 or -5.09999999999999988e-39 < re < -4.2e-129

    1. Initial program 60.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 77.5%

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

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

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

    if -2.5e23 < re < -5.09999999999999988e-39 or -4.2e-129 < re < 4.2000000000000002e26

    1. Initial program 57.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 83.8%

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

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

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

    if 4.2000000000000002e26 < re

    1. Initial program 34.3%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(im \cdot \color{blue}{\sqrt{1 \cdot \frac{1}{re}}}\right)}^{3}} \]
      7. *-un-lft-identity67.8%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(im \cdot {re}^{\color{blue}{-0.5}}\right)}^{3}} \]
    7. Applied egg-rr67.8%

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

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

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

Alternative 7: 69.9% accurate, 1.7× speedup?

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

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

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

\mathbf{elif}\;re \leq 8.1 \cdot 10^{+170}:\\
\;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -4.6999999999999999e39

    1. Initial program 49.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 -inf 83.2%

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

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

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

    if -4.6999999999999999e39 < re < 7.9999999999999997e31

    1. Initial program 62.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 78.4%

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

    if 7.9999999999999997e31 < re < 8.09999999999999965e170

    1. Initial program 57.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(im \cdot {re}^{\color{blue}{-0.5}}\right)}^{3}} \]
    7. Applied egg-rr70.6%

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

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

    if 8.09999999999999965e170 < re

    1. Initial program 3.1%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.7 \cdot 10^{+39}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 8 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 8.1 \cdot 10^{+170}:\\ \;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 69.8% accurate, 1.8× speedup?

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

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

\mathbf{elif}\;re \leq 2.7 \cdot 10^{+170}:\\
\;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -4.60000000000000024e39

    1. Initial program 49.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 -inf 83.2%

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

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

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

    if -4.60000000000000024e39 < re < 4.2000000000000003e29

    1. Initial program 62.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 78.4%

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

    if 4.2000000000000003e29 < re < 2.7000000000000002e170

    1. Initial program 57.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(im \cdot {re}^{\color{blue}{-0.5}}\right)}^{3}} \]
    7. Applied egg-rr70.6%

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

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

    if 2.7000000000000002e170 < re

    1. Initial program 3.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 9: 58.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.5 \cdot 10^{-92}:\\
\;\;\;\;0.5 \cdot \left(\left(im + 1\right) + -1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < 1.50000000000000007e-92

    1. Initial program 70.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(im \cdot {re}^{\color{blue}{-0.5}}\right)}^{3}} \]
    7. Applied egg-rr37.4%

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

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

    if 1.50000000000000007e-92 < im

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

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

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

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

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

Alternative 10: 20.5% accurate, 30.4× speedup?

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

\\
0.5 \cdot \left(\left(im + 1\right) + -1\right)
\end{array}
Derivation
  1. Initial program 53.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto 0.5 \cdot \color{blue}{\sqrt[3]{{\left(im \cdot {re}^{-0.5}\right)}^{3}}} \]
  8. Applied egg-rr18.7%

    \[\leadsto 0.5 \cdot \color{blue}{\left(\left(im + 1\right) - 1\right)} \]
  9. Final simplification18.7%

    \[\leadsto 0.5 \cdot \left(\left(im + 1\right) + -1\right) \]
  10. Add Preprocessing

Alternative 11: 6.3% accurate, 71.0× speedup?

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

\\
0.5 \cdot im
\end{array}
Derivation
  1. Initial program 53.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{im} \]
  10. Simplified6.3%

    \[\leadsto 0.5 \cdot \color{blue}{im} \]
  11. Final simplification6.3%

    \[\leadsto 0.5 \cdot im \]
  12. Add Preprocessing

Reproduce

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