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

Percentage Accurate: 40.6% → 89.8%
Time: 6.4s
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: 40.6% accurate, 1.0× speedup?

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

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

Alternative 1: 89.8% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 7.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0 + 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      9. associate-*r*94.2%

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

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

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

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

      \[\leadsto \color{blue}{0 + \frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Step-by-step derivation
      1. +-lft-identity95.1%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. associate-/l*95.3%

        \[\leadsto \color{blue}{\frac{0.5}{\frac{\sqrt{re}}{im}}} \]
    8. Simplified95.3%

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

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

    1. Initial program 52.0%

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

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

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

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

Alternative 2: 75.0% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 2.8 \cdot 10^{-59} \lor \neg \left(re \leq 4.1 \cdot 10^{+98}\right) \land re \leq 2.3 \cdot 10^{+129}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -2.69999999999999999e-16

    1. Initial program 49.9%

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

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

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

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

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

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

    if -2.69999999999999999e-16 < re < 2.79999999999999981e-59 or 4.1e98 < re < 2.2999999999999999e129

    1. Initial program 61.6%

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

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

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

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

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

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

    if 2.79999999999999981e-59 < re < 4.1e98 or 2.2999999999999999e129 < re

    1. Initial program 15.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0 + 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      9. associate-*r*73.6%

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

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

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

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

      \[\leadsto \color{blue}{0 + \frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Step-by-step derivation
      1. +-lft-identity74.2%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. associate-*l/74.2%

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

      \[\leadsto \color{blue}{\frac{0.5}{\sqrt{re}} \cdot im} \]
    9. Step-by-step derivation
      1. add-sqr-sqrt73.9%

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

        \[\leadsto \color{blue}{\sqrt{\frac{0.5}{\sqrt{re}} \cdot \frac{0.5}{\sqrt{re}}}} \cdot im \]
      3. frac-times74.1%

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

        \[\leadsto \sqrt{\frac{\color{blue}{0.25}}{\sqrt{re} \cdot \sqrt{re}}} \cdot im \]
      5. add-sqr-sqrt74.3%

        \[\leadsto \sqrt{\frac{0.25}{\color{blue}{re}}} \cdot im \]
    10. Applied egg-rr74.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.7 \cdot 10^{-16}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.8 \cdot 10^{-59} \lor \neg \left(re \leq 4.1 \cdot 10^{+98}\right) \land re \leq 2.3 \cdot 10^{+129}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \end{array} \]

Alternative 3: 74.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{if}\;re \leq -1.08 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

\mathbf{elif}\;re \leq 2.8 \cdot 10^{-59}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 4 \cdot 10^{+98}:\\
\;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\

\mathbf{elif}\;re \leq 2.3 \cdot 10^{+129}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -1.08e-16

    1. Initial program 49.9%

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

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

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

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

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

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

    if -1.08e-16 < re < 2.79999999999999981e-59 or 3.99999999999999999e98 < re < 2.2999999999999999e129

    1. Initial program 61.6%

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

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

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

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

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

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

    if 2.79999999999999981e-59 < re < 3.99999999999999999e98

    1. Initial program 24.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0 + 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      9. associate-*r*62.7%

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

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

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

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

      \[\leadsto \color{blue}{0 + \frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Step-by-step derivation
      1. +-lft-identity63.2%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. associate-/l*63.4%

        \[\leadsto \color{blue}{\frac{0.5}{\frac{\sqrt{re}}{im}}} \]
    8. Simplified63.4%

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

    if 2.2999999999999999e129 < re

    1. Initial program 3.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0 + 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      9. associate-*r*87.3%

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

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

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

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

      \[\leadsto \color{blue}{0 + \frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Step-by-step derivation
      1. +-lft-identity88.0%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. associate-*l/88.1%

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

      \[\leadsto \color{blue}{\frac{0.5}{\sqrt{re}} \cdot im} \]
    9. Step-by-step derivation
      1. add-sqr-sqrt87.6%

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

        \[\leadsto \color{blue}{\sqrt{\frac{0.5}{\sqrt{re}} \cdot \frac{0.5}{\sqrt{re}}}} \cdot im \]
      3. frac-times87.9%

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

        \[\leadsto \sqrt{\frac{\color{blue}{0.25}}{\sqrt{re} \cdot \sqrt{re}}} \cdot im \]
      5. add-sqr-sqrt88.3%

        \[\leadsto \sqrt{\frac{0.25}{\color{blue}{re}}} \cdot im \]
    10. Applied egg-rr88.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.08 \cdot 10^{-16}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.8 \cdot 10^{-59}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq 4 \cdot 10^{+98}:\\ \;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{+129}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \end{array} \]

Alternative 4: 75.4% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.6 \cdot 10^{-18}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\

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

\mathbf{elif}\;re \leq 1.4 \cdot 10^{+97}:\\
\;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\

\mathbf{elif}\;re \leq 2.3 \cdot 10^{+129}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 5 regimes
  2. if re < -4.6000000000000002e-18

    1. Initial program 49.9%

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

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

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

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

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

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

    if -4.6000000000000002e-18 < re < 1.22e-61

    1. Initial program 66.7%

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

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

    if 1.22e-61 < re < 1.4e97

    1. Initial program 23.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0 + 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      9. associate-*r*62.2%

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

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

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

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

      \[\leadsto \color{blue}{0 + \frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Step-by-step derivation
      1. +-lft-identity62.8%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. associate-/l*62.9%

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

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

    if 1.4e97 < re < 2.2999999999999999e129

    1. Initial program 14.8%

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

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

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

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

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

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

    if 2.2999999999999999e129 < re

    1. Initial program 3.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0 + 0.5 \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      9. associate-*r*87.3%

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

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

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

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

      \[\leadsto \color{blue}{0 + \frac{0.5 \cdot im}{\sqrt{re}}} \]
    7. Step-by-step derivation
      1. +-lft-identity88.0%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      2. associate-*l/88.1%

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

      \[\leadsto \color{blue}{\frac{0.5}{\sqrt{re}} \cdot im} \]
    9. Step-by-step derivation
      1. add-sqr-sqrt87.6%

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

        \[\leadsto \color{blue}{\sqrt{\frac{0.5}{\sqrt{re}} \cdot \frac{0.5}{\sqrt{re}}}} \cdot im \]
      3. frac-times87.9%

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

        \[\leadsto \sqrt{\frac{\color{blue}{0.25}}{\sqrt{re} \cdot \sqrt{re}}} \cdot im \]
      5. add-sqr-sqrt88.3%

        \[\leadsto \sqrt{\frac{0.25}{\color{blue}{re}}} \cdot im \]
    10. Applied egg-rr88.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.6 \cdot 10^{-18}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 1.22 \cdot 10^{-61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.4 \cdot 10^{+97}:\\ \;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{+129}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \end{array} \]

Alternative 5: 63.7% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 49.9%

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

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

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

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

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

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

    if -7.79999999999999979e-17 < re

    1. Initial program 43.2%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -7.8 \cdot 10^{-17}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]

Alternative 6: 51.9% accurate, 2.0× speedup?

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

\\
0.5 \cdot \sqrt{im \cdot 2}
\end{array}
Derivation
  1. Initial program 44.8%

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

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

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

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

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

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  7. Final simplification54.0%

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

Reproduce

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