math.sqrt on complex, real part

Percentage Accurate: 42.3% → 89.6%
Time: 6.2s
Alternatives: 8
Speedup: 2.0×

Specification

?
\[\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 8 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: 42.3% 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.6% accurate, 0.5× speedup?

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

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


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

    1. Initial program 12.1%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg60.1%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. *-commutative60.1%

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*61.7%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified61.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 63.4%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-*r/64.8%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}} \]
      4. distribute-rgt-neg-in64.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}} \]
      5. distribute-neg-frac64.8%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \sqrt{\frac{-im}{re}}\right)} \]
    11. Applied egg-rr55.3%

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

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

    1. Initial program 45.1%

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

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

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

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

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

Alternative 2: 82.8% accurate, 1.0× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -1.5599999999999999e65

    1. Initial program 10.9%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg44.3%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. *-commutative44.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25} - \frac{{im}^{2}}{re}} \]
      5. unpow244.3%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*44.2%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified44.2%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 56.9%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-*r/61.7%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}} \]
      4. distribute-rgt-neg-in61.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}} \]
      5. distribute-neg-frac61.7%

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

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

    if -1.5599999999999999e65 < re

    1. Initial program 47.4%

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

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

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

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

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

Alternative 3: 70.5% accurate, 1.8× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} t_0 := 2 \cdot \left(re + im\right)\\ \mathbf{if}\;re \leq -3.6 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;re \leq 3.55 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot \frac{re}{im} + t_0}\\ \mathbf{elif}\;re \leq 5 \cdot 10^{+43} \lor \neg \left(re \leq 1.12 \cdot 10^{+85}\right):\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{t_0}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 2.0 (+ re im))))
   (if (<= re -3.6e+62)
     (* 0.5 (sqrt (* (- im) (/ im re))))
     (if (<= re 3.55e-9)
       (* 0.5 (sqrt (+ (* re (/ re im)) t_0)))
       (if (or (<= re 5e+43) (not (<= re 1.12e+85)))
         (* 0.5 (* 2.0 (sqrt re)))
         (* 0.5 (sqrt t_0)))))))
im = abs(im);
double code(double re, double im) {
	double t_0 = 2.0 * (re + im);
	double tmp;
	if (re <= -3.6e+62) {
		tmp = 0.5 * sqrt((-im * (im / re)));
	} else if (re <= 3.55e-9) {
		tmp = 0.5 * sqrt(((re * (re / im)) + t_0));
	} else if ((re <= 5e+43) || !(re <= 1.12e+85)) {
		tmp = 0.5 * (2.0 * sqrt(re));
	} else {
		tmp = 0.5 * sqrt(t_0);
	}
	return tmp;
}
NOTE: im should be positive before calling this function
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 = 2.0d0 * (re + im)
    if (re <= (-3.6d+62)) then
        tmp = 0.5d0 * sqrt((-im * (im / re)))
    else if (re <= 3.55d-9) then
        tmp = 0.5d0 * sqrt(((re * (re / im)) + t_0))
    else if ((re <= 5d+43) .or. (.not. (re <= 1.12d+85))) then
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    else
        tmp = 0.5d0 * sqrt(t_0)
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double t_0 = 2.0 * (re + im);
	double tmp;
	if (re <= -3.6e+62) {
		tmp = 0.5 * Math.sqrt((-im * (im / re)));
	} else if (re <= 3.55e-9) {
		tmp = 0.5 * Math.sqrt(((re * (re / im)) + t_0));
	} else if ((re <= 5e+43) || !(re <= 1.12e+85)) {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt(t_0);
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	t_0 = 2.0 * (re + im)
	tmp = 0
	if re <= -3.6e+62:
		tmp = 0.5 * math.sqrt((-im * (im / re)))
	elif re <= 3.55e-9:
		tmp = 0.5 * math.sqrt(((re * (re / im)) + t_0))
	elif (re <= 5e+43) or not (re <= 1.12e+85):
		tmp = 0.5 * (2.0 * math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt(t_0)
	return tmp
im = abs(im)
function code(re, im)
	t_0 = Float64(2.0 * Float64(re + im))
	tmp = 0.0
	if (re <= -3.6e+62)
		tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re))));
	elseif (re <= 3.55e-9)
		tmp = Float64(0.5 * sqrt(Float64(Float64(re * Float64(re / im)) + t_0)));
	elseif ((re <= 5e+43) || !(re <= 1.12e+85))
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(t_0));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	t_0 = 2.0 * (re + im);
	tmp = 0.0;
	if (re <= -3.6e+62)
		tmp = 0.5 * sqrt((-im * (im / re)));
	elseif (re <= 3.55e-9)
		tmp = 0.5 * sqrt(((re * (re / im)) + t_0));
	elseif ((re <= 5e+43) || ~((re <= 1.12e+85)))
		tmp = 0.5 * (2.0 * sqrt(re));
	else
		tmp = 0.5 * sqrt(t_0);
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := Block[{t$95$0 = N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -3.6e+62], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.55e-9], N[(0.5 * N[Sqrt[N[(N[(re * N[(re / im), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 5e+43], N[Not[LessEqual[re, 1.12e+85]], $MachinePrecision]], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
t_0 := 2 \cdot \left(re + im\right)\\
\mathbf{if}\;re \leq -3.6 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\

\mathbf{elif}\;re \leq 3.55 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot \frac{re}{im} + t_0}\\

\mathbf{elif}\;re \leq 5 \cdot 10^{+43} \lor \neg \left(re \leq 1.12 \cdot 10^{+85}\right):\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{t_0}\\


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

    1. Initial program 10.8%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg43.4%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. *-commutative43.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25} - \frac{{im}^{2}}{re}} \]
      5. unpow243.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*43.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified43.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 55.8%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-*r/60.5%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}} \]
      4. distribute-rgt-neg-in60.5%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}} \]
      5. distribute-neg-frac60.5%

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

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

    if -3.6e62 < re < 3.54999999999999994e-9

    1. Initial program 49.2%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{re \cdot re}}{im} + \left(2 \cdot im + 2 \cdot re\right)} \]
      2. distribute-lft-out44.6%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{re}{\frac{im}{re}}} + 2 \cdot \left(im + re\right)} \]
      2. associate-/r/44.5%

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

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

    if 3.54999999999999994e-9 < re < 5.0000000000000004e43 or 1.11999999999999993e85 < re

    1. Initial program 42.5%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt86.0%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified86.0%

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

    if 5.0000000000000004e43 < re < 1.11999999999999993e85

    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. +-commutative61.6%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out46.1%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.6 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;re \leq 3.55 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot \frac{re}{im} + 2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 5 \cdot 10^{+43} \lor \neg \left(re \leq 1.12 \cdot 10^{+85}\right):\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 4: 70.6% accurate, 1.8× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq -2.35 \cdot 10^{+63}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-9} \lor \neg \left(re \leq 1.7 \cdot 10^{+47}\right) \land re \leq 1.4 \cdot 10^{+84}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re -2.35e+63)
   (* 0.5 (sqrt (/ (- im) (/ re im))))
   (if (or (<= re 3.3e-9) (and (not (<= re 1.7e+47)) (<= re 1.4e+84)))
     (* 0.5 (sqrt (* 2.0 (+ re im))))
     (* 0.5 (* 2.0 (sqrt re))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= -2.35e+63) {
		tmp = 0.5 * sqrt((-im / (re / im)));
	} else if ((re <= 3.3e-9) || (!(re <= 1.7e+47) && (re <= 1.4e+84))) {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-2.35d+63)) then
        tmp = 0.5d0 * sqrt((-im / (re / im)))
    else if ((re <= 3.3d-9) .or. (.not. (re <= 1.7d+47)) .and. (re <= 1.4d+84)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= -2.35e+63) {
		tmp = 0.5 * Math.sqrt((-im / (re / im)));
	} else if ((re <= 3.3e-9) || (!(re <= 1.7e+47) && (re <= 1.4e+84))) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= -2.35e+63:
		tmp = 0.5 * math.sqrt((-im / (re / im)))
	elif (re <= 3.3e-9) or (not (re <= 1.7e+47) and (re <= 1.4e+84)):
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= -2.35e+63)
		tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im))));
	elseif ((re <= 3.3e-9) || (!(re <= 1.7e+47) && (re <= 1.4e+84)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -2.35e+63)
		tmp = 0.5 * sqrt((-im / (re / im)));
	elseif ((re <= 3.3e-9) || (~((re <= 1.7e+47)) && (re <= 1.4e+84)))
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, -2.35e+63], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 3.3e-9], And[N[Not[LessEqual[re, 1.7e+47]], $MachinePrecision], LessEqual[re, 1.4e+84]]], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.35 \cdot 10^{+63}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\

\mathbf{elif}\;re \leq 3.3 \cdot 10^{-9} \lor \neg \left(re \leq 1.7 \cdot 10^{+47}\right) \land re \leq 1.4 \cdot 10^{+84}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -2.3500000000000001e63

    1. Initial program 10.8%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg43.4%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. *-commutative43.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25} - \frac{{im}^{2}}{re}} \]
      5. unpow243.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*43.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified43.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 55.8%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-*r/60.5%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}} \]
      4. distribute-rgt-neg-in60.5%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}} \]
      5. distribute-neg-frac60.5%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \frac{-im}{re}}} \]
    10. Taylor expanded in im around 0 55.8%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{{im}^{2}}{re}}} \]
      2. distribute-frac-neg55.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{-{im}^{2}}{re}}} \]
      3. unpow255.8%

        \[\leadsto 0.5 \cdot \sqrt{\frac{-\color{blue}{im \cdot im}}{re}} \]
      4. distribute-lft-neg-in55.8%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{-im}{\frac{re}{im}}}} \]
      6. distribute-neg-frac60.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im}{\frac{re}{im}}}} \]
    12. Simplified60.4%

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

    if -2.3500000000000001e63 < re < 3.30000000000000018e-9 or 1.6999999999999999e47 < re < 1.39999999999999991e84

    1. Initial program 50.1%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out45.3%

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

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

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

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

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

    if 3.30000000000000018e-9 < re < 1.6999999999999999e47 or 1.39999999999999991e84 < re

    1. Initial program 42.5%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt86.0%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified86.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.35 \cdot 10^{+63}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-9} \lor \neg \left(re \leq 1.7 \cdot 10^{+47}\right) \land re \leq 1.4 \cdot 10^{+84}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 5: 70.6% accurate, 1.8× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq -3.4 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;re \leq 3.75 \cdot 10^{-9} \lor \neg \left(re \leq 1.9 \cdot 10^{+46}\right) \land re \leq 1.25 \cdot 10^{+84}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re -3.4e+62)
   (* 0.5 (sqrt (* (- im) (/ im re))))
   (if (or (<= re 3.75e-9) (and (not (<= re 1.9e+46)) (<= re 1.25e+84)))
     (* 0.5 (sqrt (* 2.0 (+ re im))))
     (* 0.5 (* 2.0 (sqrt re))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= -3.4e+62) {
		tmp = 0.5 * sqrt((-im * (im / re)));
	} else if ((re <= 3.75e-9) || (!(re <= 1.9e+46) && (re <= 1.25e+84))) {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-3.4d+62)) then
        tmp = 0.5d0 * sqrt((-im * (im / re)))
    else if ((re <= 3.75d-9) .or. (.not. (re <= 1.9d+46)) .and. (re <= 1.25d+84)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= -3.4e+62) {
		tmp = 0.5 * Math.sqrt((-im * (im / re)));
	} else if ((re <= 3.75e-9) || (!(re <= 1.9e+46) && (re <= 1.25e+84))) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= -3.4e+62:
		tmp = 0.5 * math.sqrt((-im * (im / re)))
	elif (re <= 3.75e-9) or (not (re <= 1.9e+46) and (re <= 1.25e+84)):
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= -3.4e+62)
		tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re))));
	elseif ((re <= 3.75e-9) || (!(re <= 1.9e+46) && (re <= 1.25e+84)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -3.4e+62)
		tmp = 0.5 * sqrt((-im * (im / re)));
	elseif ((re <= 3.75e-9) || (~((re <= 1.9e+46)) && (re <= 1.25e+84)))
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, -3.4e+62], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 3.75e-9], And[N[Not[LessEqual[re, 1.9e+46]], $MachinePrecision], LessEqual[re, 1.25e+84]]], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.4 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\

\mathbf{elif}\;re \leq 3.75 \cdot 10^{-9} \lor \neg \left(re \leq 1.9 \cdot 10^{+46}\right) \land re \leq 1.25 \cdot 10^{+84}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if re < -3.40000000000000014e62

    1. Initial program 10.8%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} + -1 \cdot \frac{{im}^{2}}{re}}} \]
      2. mul-1-neg43.4%

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{0.25 \cdot \frac{{im}^{4}}{{re}^{3}} - \frac{{im}^{2}}{re}}} \]
      4. *-commutative43.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25} - \frac{{im}^{2}}{re}} \]
      5. unpow243.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{\color{blue}{im \cdot im}}{re}} \]
      6. associate-/l*43.4%

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{\frac{im}{\frac{re}{im}}}} \]
    6. Simplified43.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \frac{im}{\frac{re}{im}}}} \]
    7. Taylor expanded in im around 0 55.8%

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

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

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
      3. associate-*r/60.5%

        \[\leadsto 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}} \]
      4. distribute-rgt-neg-in60.5%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}} \]
      5. distribute-neg-frac60.5%

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

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

    if -3.40000000000000014e62 < re < 3.74999999999999966e-9 or 1.9e46 < re < 1.25e84

    1. Initial program 50.1%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out45.3%

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

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

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

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

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

    if 3.74999999999999966e-9 < re < 1.9e46 or 1.25e84 < re

    1. Initial program 42.5%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt86.0%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified86.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.4 \cdot 10^{+62}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;re \leq 3.75 \cdot 10^{-9} \lor \neg \left(re \leq 1.9 \cdot 10^{+46}\right) \land re \leq 1.25 \cdot 10^{+84}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 6: 64.7% accurate, 1.9× speedup?

\[\begin{array}{l} im = |im|\\ \\ \begin{array}{l} \mathbf{if}\;re \leq 3.7 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 1.4 \cdot 10^{+47} \lor \neg \left(re \leq 1.6 \cdot 10^{+84}\right):\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im)
 :precision binary64
 (if (<= re 3.7e-9)
   (* 0.5 (sqrt (* 2.0 im)))
   (if (or (<= re 1.4e+47) (not (<= re 1.6e+84)))
     (* 0.5 (* 2.0 (sqrt re)))
     (* 0.5 (sqrt (* 2.0 (+ re im)))))))
im = abs(im);
double code(double re, double im) {
	double tmp;
	if (re <= 3.7e-9) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else if ((re <= 1.4e+47) || !(re <= 1.6e+84)) {
		tmp = 0.5 * (2.0 * sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	}
	return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= 3.7d-9) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else if ((re <= 1.4d+47) .or. (.not. (re <= 1.6d+84))) then
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    end if
    code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
	double tmp;
	if (re <= 3.7e-9) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else if ((re <= 1.4e+47) || !(re <= 1.6e+84)) {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	}
	return tmp;
}
im = abs(im)
def code(re, im):
	tmp = 0
	if re <= 3.7e-9:
		tmp = 0.5 * math.sqrt((2.0 * im))
	elif (re <= 1.4e+47) or not (re <= 1.6e+84):
		tmp = 0.5 * (2.0 * math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	return tmp
im = abs(im)
function code(re, im)
	tmp = 0.0
	if (re <= 3.7e-9)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	elseif ((re <= 1.4e+47) || !(re <= 1.6e+84))
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	end
	return tmp
end
im = abs(im)
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= 3.7e-9)
		tmp = 0.5 * sqrt((2.0 * im));
	elseif ((re <= 1.4e+47) || ~((re <= 1.6e+84)))
		tmp = 0.5 * (2.0 * sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	end
	tmp_2 = tmp;
end
NOTE: im should be positive before calling this function
code[re_, im_] := If[LessEqual[re, 3.7e-9], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.4e+47], N[Not[LessEqual[re, 1.6e+84]], $MachinePrecision]], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.7 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{elif}\;re \leq 1.4 \cdot 10^{+47} \lor \neg \left(re \leq 1.6 \cdot 10^{+84}\right):\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\

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


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

    1. Initial program 39.6%

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

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

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

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

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

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

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

    if 3.7e-9 < re < 1.39999999999999994e47 or 1.60000000000000005e84 < re

    1. Initial program 42.5%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      2. rem-square-sqrt86.0%

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    6. Simplified86.0%

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

    if 1.39999999999999994e47 < re < 1.60000000000000005e84

    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. +-commutative61.6%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out46.1%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 3.7 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 1.4 \cdot 10^{+47} \lor \neg \left(re \leq 1.6 \cdot 10^{+84}\right):\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 7: 65.0% accurate, 2.0× speedup?

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


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

    1. Initial program 39.6%

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

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

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

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

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

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

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

    if 2.65000000000000015e-9 < re

    1. Initial program 44.9%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 2.65 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 8: 53.0% accurate, 2.0× speedup?

\[\begin{array}{l} im = |im|\\ \\ 0.5 \cdot \sqrt{2 \cdot im} \end{array} \]
NOTE: im should be positive before calling this function
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
im = abs(im);
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * im));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.5d0 * sqrt((2.0d0 * im))
end function
im = Math.abs(im);
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * im));
}
im = abs(im)
def code(re, im):
	return 0.5 * math.sqrt((2.0 * im))
im = abs(im)
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * im)))
end
im = abs(im)
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * im));
end
NOTE: im should be positive before calling this function
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Derivation
  1. Initial program 41.3%

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

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

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

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

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

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

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

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

Developer target: 48.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im}\\ \mathbf{if}\;re < 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (sqrt (+ (* re re) (* im im)))))
   (if (< re 0.0)
     (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
     (* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
	double t_0 = sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * sqrt((2.0 * (t_0 + 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 = sqrt(((re * re) + (im * im)))
    if (re < 0.0d0) then
        tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = Math.sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
	}
	return tmp;
}
def code(re, im):
	t_0 = math.sqrt(((re * re) + (im * im)))
	tmp = 0
	if re < 0.0:
		tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re))))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (t_0 + re)))
	return tmp
function code(re, im)
	t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im)))
	tmp = 0.0
	if (re < 0.0)
		tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re)))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = sqrt(((re * re) + (im * im)));
	tmp = 0.0;
	if (re < 0.0)
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	else
		tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2023189 
(FPCore (re im)
  :name "math.sqrt on complex, real part"
  :precision binary64

  :herbie-target
  (if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))