math.sqrt on complex, real part

Percentage Accurate: 41.0% → 89.1%
Time: 11.1s
Alternatives: 6
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 6 alternatives:

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

Initial Program: 41.0% 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.1% accurate, 0.5× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re + \sqrt{re \cdot re + im_m \cdot im_m} \leq 0:\\ \;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + 2 \cdot \log im_m\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im_m\right)\right)}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= (+ re (sqrt (+ (* re re) (* im_m im_m)))) 0.0)
   (* 0.5 (exp (* 0.5 (+ (log (/ -1.0 re)) (* 2.0 (log im_m))))))
   (sqrt (* 0.5 (+ re (hypot re im_m))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if ((re + sqrt(((re * re) + (im_m * im_m)))) <= 0.0) {
		tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + (2.0 * log(im_m)))));
	} else {
		tmp = sqrt((0.5 * (re + hypot(re, im_m))));
	}
	return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if ((re + Math.sqrt(((re * re) + (im_m * im_m)))) <= 0.0) {
		tmp = 0.5 * Math.exp((0.5 * (Math.log((-1.0 / re)) + (2.0 * Math.log(im_m)))));
	} else {
		tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im_m))));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if (re + math.sqrt(((re * re) + (im_m * im_m)))) <= 0.0:
		tmp = 0.5 * math.exp((0.5 * (math.log((-1.0 / re)) + (2.0 * math.log(im_m)))))
	else:
		tmp = math.sqrt((0.5 * (re + math.hypot(re, im_m))))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im_m * im_m)))) <= 0.0)
		tmp = Float64(0.5 * exp(Float64(0.5 * Float64(log(Float64(-1.0 / re)) + Float64(2.0 * log(im_m))))));
	else
		tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im_m))));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if ((re + sqrt(((re * re) + (im_m * im_m)))) <= 0.0)
		tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + (2.0 * log(im_m)))));
	else
		tmp = sqrt((0.5 * (re + hypot(re, im_m))));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Exp[N[(0.5 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[(2.0 * N[Log[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im$95$m ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re + \sqrt{re \cdot re + im_m \cdot im_m} \leq 0:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + 2 \cdot \log im_m\right)}\\

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


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

    1. Initial program 8.2%

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

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

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

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

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

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

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

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

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

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

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

      \[\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/217.2%

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

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right) \cdot 0.5}} \]
      3. *-commutative16.4%

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

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

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

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

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

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

    1. Initial program 50.6%

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

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

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

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

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

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

        \[\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--50.6%

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

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

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

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

      \[\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. add-sqr-sqrt89.4%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      6. add-sqr-sqrt90.1%

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

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

        \[\leadsto \sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot \color{blue}{0.25}} \]
    6. Applied egg-rr90.1%

      \[\leadsto \color{blue}{\sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25}} \]
    7. Step-by-step derivation
      1. associate-*l*90.1%

        \[\leadsto \sqrt{\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \left(2 \cdot 0.25\right)}} \]
      2. metadata-eval90.1%

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

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

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

Alternative 2: 84.7% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{\left(re + \sqrt{re \cdot re + im_m \cdot im_m}\right) \cdot 2} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-{im_m}^{2}}{re}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im_m\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 10.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg10.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. +-commutative10.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-neg10.7%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub10.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--10.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
      8. sub-neg10.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-neg10.7%

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

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

      \[\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/210.7%

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

        \[\leadsto 0.5 \cdot \color{blue}{e^{\log \left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right) \cdot 0.5}} \]
      3. *-commutative10.7%

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

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

      \[\leadsto 0.5 \cdot e^{\color{blue}{\left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)} \cdot 0.5} \]
    8. Step-by-step derivation
      1. log-pow58.3%

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

      \[\leadsto 0.5 \cdot e^{\color{blue}{\left(\log \left(\frac{-1}{re}\right) + 2 \cdot \log im\right)} \cdot 0.5} \]
    10. Taylor expanded in re around 0 0.0%

      \[\leadsto 0.5 \cdot \color{blue}{e^{0.5 \cdot \left(\log -1 + \left(-1 \cdot \log re + 2 \cdot \log im\right)\right)}} \]
    11. Step-by-step derivation
      1. mul-1-neg0.0%

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

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

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

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

        \[\leadsto 0.5 \cdot e^{\left(\left(\log -1 + \color{blue}{\left(-\log re\right)}\right) + 2 \cdot \log im\right) \cdot 0.5} \]
      6. sub-neg0.0%

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{-1}{re} \cdot {im}^{2}\right)}^{0.5}} \]
      11. unpow1/251.3%

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt{\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 48.1%

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

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

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

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

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

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

        \[\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--48.1%

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

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

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

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

      \[\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. add-sqr-sqrt86.9%

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
      6. add-sqr-sqrt87.5%

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

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

        \[\leadsto \sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot \color{blue}{0.25}} \]
    6. Applied egg-rr87.5%

      \[\leadsto \color{blue}{\sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25}} \]
    7. Step-by-step derivation
      1. associate-*l*87.5%

        \[\leadsto \sqrt{\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \left(2 \cdot 0.25\right)}} \]
      2. metadata-eval87.5%

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

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

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

Alternative 3: 79.7% accurate, 1.0× speedup?

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

\\
\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im_m\right)\right)}
\end{array}
Derivation
  1. Initial program 42.7%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
  2. Step-by-step derivation
    1. sqr-neg42.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. +-commutative42.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-neg42.7%

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
    6. cancel-sign-sub42.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--42.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
    8. sub-neg42.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-neg42.7%

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

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

    \[\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. add-sqr-sqrt75.9%

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)} \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}} \]
    6. add-sqr-sqrt76.4%

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

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

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

    \[\leadsto \color{blue}{\sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot 0.25}} \]
  7. Step-by-step derivation
    1. associate-*l*76.4%

      \[\leadsto \sqrt{\color{blue}{\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot \left(2 \cdot 0.25\right)}} \]
    2. metadata-eval76.4%

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

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

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

Alternative 4: 66.7% accurate, 1.8× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -2.6 \cdot 10^{-25}:\\ \;\;\;\;-1 + \left(0.5 \cdot \sqrt{im_m \cdot 2} + 1\right)\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{-84}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im_m\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re -2.6e-25)
   (+ -1.0 (+ (* 0.5 (sqrt (* im_m 2.0))) 1.0))
   (if (<= re 4.8e-84) (* 0.5 (sqrt (* 2.0 (+ re im_m)))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= -2.6e-25) {
		tmp = -1.0 + ((0.5 * sqrt((im_m * 2.0))) + 1.0);
	} else if (re <= 4.8e-84) {
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	} else {
		tmp = sqrt(re);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (re <= (-2.6d-25)) then
        tmp = (-1.0d0) + ((0.5d0 * sqrt((im_m * 2.0d0))) + 1.0d0)
    else if (re <= 4.8d-84) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im_m)))
    else
        tmp = sqrt(re)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= -2.6e-25) {
		tmp = -1.0 + ((0.5 * Math.sqrt((im_m * 2.0))) + 1.0);
	} else if (re <= 4.8e-84) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im_m)));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= -2.6e-25:
		tmp = -1.0 + ((0.5 * math.sqrt((im_m * 2.0))) + 1.0)
	elif re <= 4.8e-84:
		tmp = 0.5 * math.sqrt((2.0 * (re + im_m)))
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= -2.6e-25)
		tmp = Float64(-1.0 + Float64(Float64(0.5 * sqrt(Float64(im_m * 2.0))) + 1.0));
	elseif (re <= 4.8e-84)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im_m))));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= -2.6e-25)
		tmp = -1.0 + ((0.5 * sqrt((im_m * 2.0))) + 1.0);
	elseif (re <= 4.8e-84)
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, -2.6e-25], N[(-1.0 + N[(N[(0.5 * N[Sqrt[N[(im$95$m * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.8e-84], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

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

\mathbf{elif}\;re \leq 4.8 \cdot 10^{-84}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im_m\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


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

    1. Initial program 13.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg13.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. +-commutative13.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-neg13.8%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub13.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--13.8%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
      8. sub-neg13.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-neg13.8%

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

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

      \[\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. +-commutative31.4%

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\color{blue}{im} \cdot 2} \cdot 0.5\right)\right) \]
    8. Step-by-step derivation
      1. expm1-udef14.6%

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

        \[\leadsto e^{\color{blue}{\log \left(1 + \sqrt{im \cdot 2} \cdot 0.5\right)}} - 1 \]
      3. rem-exp-log15.3%

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

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

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

        \[\leadsto \left(0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} + 1\right) - 1 \]
    9. Applied egg-rr15.3%

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

    if -2.6e-25 < re < 4.80000000000000035e-84

    1. Initial program 56.0%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg56.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. +-commutative56.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-neg56.0%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub56.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--56.0%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
      8. sub-neg56.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-neg56.0%

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

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

      \[\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 41.7%

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

    if 4.80000000000000035e-84 < re

    1. Initial program 49.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg49.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. +-commutative49.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-neg49.4%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub49.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--49.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
      8. sub-neg49.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-neg49.4%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{re \cdot re + im \cdot im}}\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 72.4%

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*73.8%

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identity73.8%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    7. Simplified73.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification43.3%

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

Alternative 5: 63.5% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.8 \cdot 10^{-81}:\\
\;\;\;\;0.5 \cdot \sqrt{im_m \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\


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

    1. Initial program 40.1%

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

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

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

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

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

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

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

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

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

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

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

      \[\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 30.1%

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

    if 2.7999999999999999e-81 < re

    1. Initial program 49.4%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Step-by-step derivation
      1. sqr-neg49.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. +-commutative49.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-neg49.4%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
      6. cancel-sign-sub49.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--49.4%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
      8. sub-neg49.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-neg49.4%

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{re \cdot re + im \cdot im}}\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 72.4%

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

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

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

        \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*73.8%

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

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identity73.8%

        \[\leadsto \color{blue}{\sqrt{re}} \]
    7. Simplified73.8%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification42.1%

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

Alternative 6: 26.4% accurate, 2.1× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \sqrt{re} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 (sqrt re))
im_m = fabs(im);
double code(double re, double im_m) {
	return sqrt(re);
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = sqrt(re)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return Math.sqrt(re);
}
im_m = math.fabs(im)
def code(re, im_m):
	return math.sqrt(re)
im_m = abs(im)
function code(re, im_m)
	return sqrt(re)
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = sqrt(re);
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
\sqrt{re}
\end{array}
Derivation
  1. Initial program 42.7%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
  2. Step-by-step derivation
    1. sqr-neg42.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. +-commutative42.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-neg42.7%

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot 2 + \sqrt{im \cdot im + re \cdot re} \cdot 2}} \]
    6. cancel-sign-sub42.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--42.7%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(re - \left(-\sqrt{im \cdot im + re \cdot re}\right)\right)}} \]
    8. sub-neg42.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-neg42.7%

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

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

    \[\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 24.5%

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

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

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

      \[\leadsto 0.5 \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
    4. associate-*r*24.9%

      \[\leadsto \color{blue}{\left(0.5 \cdot 2\right) \cdot \sqrt{re}} \]
    5. metadata-eval24.9%

      \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
    6. *-lft-identity24.9%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  7. Simplified24.9%

    \[\leadsto \color{blue}{\sqrt{re}} \]
  8. Final simplification24.9%

    \[\leadsto \sqrt{re} \]
  9. Add Preprocessing

Developer target: 48.2% 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 2024024 
(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)))))