math.sqrt on complex, real part

Percentage Accurate: 41.9% → 84.5%
Time: 12.0s
Alternatives: 7
Speedup: 1.9×

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 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 41.9% 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: 84.5% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
   (* 0.5 (exp (* 0.5 (+ (log (/ -1.0 re)) (log (pow im 2.0))))))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
	double tmp;
	if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + log(pow(im, 2.0)))));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	return tmp;
}
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.exp((0.5 * (Math.log((-1.0 / re)) + Math.log(Math.pow(im, 2.0)))));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0:
		tmp = 0.5 * math.exp((0.5 * (math.log((-1.0 / re)) + math.log(math.pow(im, 2.0)))))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	return tmp
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 * exp(Float64(0.5 * Float64(log(Float64(-1.0 / re)) + log((im ^ 2.0))))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0)
		tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + log((im ^ 2.0)))));
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
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[Exp[N[(0.5 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision]), $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}

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\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 8.8%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)} \]
      5. distribute-rgt-in8.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub8.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--8.8%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg8.8%

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

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

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. pow1/28.8%

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

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

        \[\leadsto 0.5 \cdot {\left(2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}^{0.5} \]
      4. pow-to-exp8.8%

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

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

        \[\leadsto 0.5 \cdot e^{\log \left(2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)\right) \cdot 0.5} \]
    6. Applied egg-rr8.8%

      \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right) \cdot 0.5}} \]
    7. Taylor expanded in re around -inf 63.0%

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

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

    1. Initial program 42.3%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub42.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--42.3%

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

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

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

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

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

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

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

Alternative 2: 84.6% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{\frac{{im}^{4}}{{re}^{2}}}{re} \cdot 0.25 - im \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} \]
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
   (*
    0.5
    (sqrt (- (* (/ (/ (pow im 4.0) (pow re 2.0)) re) 0.25) (* im (/ im re)))))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re 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(((((pow(im, 4.0) / pow(re, 2.0)) / re) * 0.25) - (im * (im / re))));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	return tmp;
}
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(((((Math.pow(im, 4.0) / Math.pow(re, 2.0)) / re) * 0.25) - (im * (im / re))));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
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(((((math.pow(im, 4.0) / math.pow(re, 2.0)) / re) * 0.25) - (im * (im / re))))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	return tmp
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 * sqrt(Float64(Float64(Float64(Float64((im ^ 4.0) / (re ^ 2.0)) / re) * 0.25) - Float64(im * Float64(im / re)))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return tmp
end
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 ^ 4.0) / (re ^ 2.0)) / re) * 0.25) - (im * (im / re))));
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
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[Sqrt[N[(N[(N[(N[(N[Power[im, 4.0], $MachinePrecision] / N[Power[re, 2.0], $MachinePrecision]), $MachinePrecision] / re), $MachinePrecision] * 0.25), $MachinePrecision] - N[(im * N[(im / re), $MachinePrecision]), $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}

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{\frac{{im}^{4}}{{re}^{2}}}{re} \cdot 0.25 - im \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 (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 8.8%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)} \]
      5. distribute-rgt-in8.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub8.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--8.8%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg8.8%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{im \cdot \frac{im}{re}}} \]
    9. Applied egg-rr56.2%

      \[\leadsto 0.5 \cdot \sqrt{\frac{{im}^{4}}{{re}^{3}} \cdot 0.25 - \color{blue}{im \cdot \frac{im}{re}}} \]
    10. Step-by-step derivation
      1. *-un-lft-identity56.2%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\left(\frac{1}{re} \cdot \frac{{im}^{4}}{\color{blue}{{re}^{2}}}\right) \cdot 0.25 - im \cdot \frac{im}{re}} \]
    11. Applied egg-rr57.8%

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

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

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

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

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

    1. Initial program 42.3%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub42.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--42.3%

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

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

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

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

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

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

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

Alternative 3: 84.3% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
   (* 0.5 (sqrt (/ (pow im 2.0) (- re))))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re 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((pow(im, 2.0) / -re));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	return tmp;
}
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((Math.pow(im, 2.0) / -re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
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((math.pow(im, 2.0) / -re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	return tmp
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 * sqrt(Float64((im ^ 2.0) / Float64(-re))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return tmp
end
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 ^ 2.0) / -re));
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	end
	tmp_2 = tmp;
end
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[Sqrt[N[(N[Power[im, 2.0], $MachinePrecision] / (-re)), $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}

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-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 (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 8.8%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)} \]
      5. distribute-rgt-in8.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub8.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--8.8%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg8.8%

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

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

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

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

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

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

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

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

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

    1. Initial program 42.3%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub42.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--42.3%

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

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

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

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

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

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

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

Alternative 4: 49.1% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.9 \cdot 10^{+124}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\

\mathbf{elif}\;re \leq 8 \cdot 10^{-45}:\\
\;\;\;\;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 3 regimes
  2. if re < -1.8999999999999999e124

    1. Initial program 5.7%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub5.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--5.7%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg5.7%

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

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

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

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

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

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

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

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

    if -1.8999999999999999e124 < re < 7.99999999999999987e-45

    1. Initial program 44.5%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub44.5%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--44.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg44.5%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{re \cdot re + im \cdot im}}\right)} \]
      11. hypot-define81.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. Add Preprocessing
    5. Taylor expanded in re around 0 38.7%

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

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

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

    if 7.99999999999999987e-45 < re

    1. Initial program 45.0%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)} \]
      5. distribute-rgt-in45.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub45.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--45.0%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg45.0%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{re \cdot re + im \cdot im}}\right)} \]
      11. hypot-define100.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. Add Preprocessing
    5. Taylor expanded in im around 0 70.6%

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

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

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

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

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

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

Alternative 5: 44.3% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.65 \cdot 10^{+235}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\

\mathbf{elif}\;re \leq 4.2 \cdot 10^{-46}:\\
\;\;\;\;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 3 regimes
  2. if re < -1.65e235

    1. Initial program 2.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. add-sqr-sqrt2.4%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot re\right)}} \]
    6. Step-by-step derivation
      1. distribute-rgt1-in31.0%

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

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

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

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

    if -1.65e235 < re < 4.19999999999999975e-46

    1. Initial program 40.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)} \]
      5. distribute-rgt-in40.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub40.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--40.4%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg40.4%

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

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

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

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

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

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

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

    if 4.19999999999999975e-46 < re

    1. Initial program 45.0%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)} \]
      5. distribute-rgt-in45.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub45.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--45.0%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg45.0%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{re \cdot re + im \cdot im}}\right)} \]
      11. hypot-define100.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. Add Preprocessing
    5. Taylor expanded in im around 0 70.6%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.65 \cdot 10^{+235}:\\ \;\;\;\;0.5 \cdot \sqrt{0}\\ \mathbf{elif}\;re \leq 4.2 \cdot 10^{-46}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 29.2% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;im \leq 5.5 \cdot 10^{-205}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\

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


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

    1. Initial program 38.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. add-sqr-sqrt37.9%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot re\right)}} \]
    6. Step-by-step derivation
      1. distribute-rgt1-in8.0%

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

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

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

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

    if 5.4999999999999996e-205 < im

    1. Initial program 39.7%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub39.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 - \left(-\sqrt{im \cdot im + re \cdot re}\right) \cdot 2}} \]
      7. distribute-rgt-out--39.7%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \left(-\left(-\sqrt{im \cdot im + re \cdot re}\right)\right)\right)}} \]
      9. remove-double-neg39.7%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq 5.5 \cdot 10^{-205}:\\ \;\;\;\;0.5 \cdot \sqrt{0}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 5.8% accurate, 2.1× speedup?

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

\\
0.5 \cdot \sqrt{0}
\end{array}
Derivation
  1. Initial program 39.2%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt38.7%

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

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

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

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

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot re\right)}} \]
  6. Step-by-step derivation
    1. distribute-rgt1-in5.8%

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{0} \cdot re\right)} \]
    3. mul0-lft5.8%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{0}} \]
  7. Simplified5.8%

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{0}} \]
  8. Final simplification5.8%

    \[\leadsto 0.5 \cdot \sqrt{0} \]
  9. Add Preprocessing

Developer target: 48.7% 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 2024062 
(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)))))