math.sqrt on complex, real part

Percentage Accurate: 40.7% → 90.3%
Time: 8.9s
Alternatives: 4
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

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

Initial Program: 40.7% 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: 90.3% accurate, 0.7× 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 \frac{im\_m}{\sqrt{-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 (<= (+ re (sqrt (+ (* re re) (* im_m im_m)))) 0.0)
   (* 0.5 (/ im_m (sqrt (- re))))
   (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 * (im_m / sqrt(-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 ((re + Math.sqrt(((re * re) + (im_m * im_m)))) <= 0.0) {
		tmp = 0.5 * (im_m / Math.sqrt(-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 (re + math.sqrt(((re * re) + (im_m * im_m)))) <= 0.0:
		tmp = 0.5 * (im_m / math.sqrt(-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 (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im_m * im_m)))) <= 0.0)
		tmp = Float64(0.5 * Float64(im_m / sqrt(Float64(-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 ((re + sqrt(((re * re) + (im_m * im_m)))) <= 0.0)
		tmp = 0.5 * (im_m / sqrt(-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[(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[(im$95$m / N[Sqrt[(-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}\;re + \sqrt{re \cdot re + im\_m \cdot im\_m} \leq 0:\\
\;\;\;\;0.5 \cdot \frac{im\_m}{\sqrt{-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 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 6.2%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \color{blue}{{\left(\left(\sqrt{2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)} \cdot \sqrt{2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)}\right) \cdot \sqrt{2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)}\right)}^{0.3333333333333333}} \]
      3. add-sqr-sqrt39.8%

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

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

        \[\leadsto 0.5 \cdot {\left({\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{\color{blue}{\left(3 \cdot 0.3333333333333333\right)}} \cdot \sqrt{2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)}\right)}^{0.3333333333333333} \]
      6. pow1/239.8%

        \[\leadsto 0.5 \cdot {\left({\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{\left(3 \cdot 0.3333333333333333\right)} \cdot \color{blue}{{\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{0.5}}\right)}^{0.3333333333333333} \]
      7. pow-prod-up39.8%

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

        \[\leadsto 0.5 \cdot {\left({\color{blue}{\left(\left(\frac{{im}^{2}}{re} \cdot -0.5\right) \cdot 2\right)}}^{\left(3 \cdot 0.3333333333333333 + 0.5\right)}\right)}^{0.3333333333333333} \]
      9. associate-*l*39.8%

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

        \[\leadsto 0.5 \cdot {\left({\left(\frac{{im}^{2}}{re} \cdot \color{blue}{-1}\right)}^{\left(3 \cdot 0.3333333333333333 + 0.5\right)}\right)}^{0.3333333333333333} \]
      11. metadata-eval39.8%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{{im}^{2}}{re} \cdot -1\right)}^{\left(\color{blue}{1} + 0.5\right)}\right)}^{0.3333333333333333} \]
      12. metadata-eval39.8%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{{im}^{2}}{re} \cdot -1\right)}^{\color{blue}{1.5}}\right)}^{0.3333333333333333} \]
    9. Applied egg-rr39.8%

      \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\frac{{im}^{2}}{re} \cdot -1\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    10. Step-by-step derivation
      1. unpow1/342.3%

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

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt[3]{{\left(\frac{-{im}^{2}}{re}\right)}^{1.5}}} \]
    12. Step-by-step derivation
      1. pow1/339.8%

        \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\frac{-{im}^{2}}{re}\right)}^{1.5}\right)}^{0.3333333333333333}} \]
      2. pow-pow51.2%

        \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{-{im}^{2}}{re}\right)}^{\left(1.5 \cdot 0.3333333333333333\right)}} \]
      3. metadata-eval51.2%

        \[\leadsto 0.5 \cdot {\left(\frac{-{im}^{2}}{re}\right)}^{\color{blue}{0.5}} \]
      4. pow1/251.2%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{-\left(-{im}^{2}\right)}}{\sqrt{-re}}} \]
      7. remove-double-neg54.8%

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

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{-re}} \]
      10. add-sqr-sqrt53.8%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{-re}} \]
    13. Applied egg-rr53.8%

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

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

    1. Initial program 48.6%

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

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

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

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

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

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

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

        \[\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-unprod93.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. *-commutative93.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. *-commutative93.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-sqr93.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-sqrt93.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. *-commutative93.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-eval93.4%

        \[\leadsto \sqrt{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right) \cdot \color{blue}{0.25}} \]
    6. Applied egg-rr93.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*93.4%

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

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

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

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

Alternative 2: 74.8% accurate, 1.7× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} t_0 := 0.5 \cdot \frac{im\_m}{\sqrt{-re}}\\ \mathbf{if}\;re \leq -2.6 \cdot 10^{+92}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq -1.12 \cdot 10^{+79}:\\ \;\;\;\;\sqrt{im\_m \cdot 0.5}\\ \mathbf{elif}\;re \leq -3.1 \cdot 10^{-79}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;re \leq 3.5 \cdot 10^{-12}:\\ \;\;\;\;\sqrt{0.5 \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
 (let* ((t_0 (* 0.5 (/ im_m (sqrt (- re))))))
   (if (<= re -2.6e+92)
     t_0
     (if (<= re -1.12e+79)
       (sqrt (* im_m 0.5))
       (if (<= re -3.1e-79)
         t_0
         (if (<= re 3.5e-12) (sqrt (* 0.5 (+ re im_m))) (sqrt re)))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double t_0 = 0.5 * (im_m / sqrt(-re));
	double tmp;
	if (re <= -2.6e+92) {
		tmp = t_0;
	} else if (re <= -1.12e+79) {
		tmp = sqrt((im_m * 0.5));
	} else if (re <= -3.1e-79) {
		tmp = t_0;
	} else if (re <= 3.5e-12) {
		tmp = sqrt((0.5 * (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) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * (im_m / sqrt(-re))
    if (re <= (-2.6d+92)) then
        tmp = t_0
    else if (re <= (-1.12d+79)) then
        tmp = sqrt((im_m * 0.5d0))
    else if (re <= (-3.1d-79)) then
        tmp = t_0
    else if (re <= 3.5d-12) then
        tmp = sqrt((0.5d0 * (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 t_0 = 0.5 * (im_m / Math.sqrt(-re));
	double tmp;
	if (re <= -2.6e+92) {
		tmp = t_0;
	} else if (re <= -1.12e+79) {
		tmp = Math.sqrt((im_m * 0.5));
	} else if (re <= -3.1e-79) {
		tmp = t_0;
	} else if (re <= 3.5e-12) {
		tmp = Math.sqrt((0.5 * (re + im_m)));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	t_0 = 0.5 * (im_m / math.sqrt(-re))
	tmp = 0
	if re <= -2.6e+92:
		tmp = t_0
	elif re <= -1.12e+79:
		tmp = math.sqrt((im_m * 0.5))
	elif re <= -3.1e-79:
		tmp = t_0
	elif re <= 3.5e-12:
		tmp = math.sqrt((0.5 * (re + im_m)))
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	t_0 = Float64(0.5 * Float64(im_m / sqrt(Float64(-re))))
	tmp = 0.0
	if (re <= -2.6e+92)
		tmp = t_0;
	elseif (re <= -1.12e+79)
		tmp = sqrt(Float64(im_m * 0.5));
	elseif (re <= -3.1e-79)
		tmp = t_0;
	elseif (re <= 3.5e-12)
		tmp = sqrt(Float64(0.5 * Float64(re + im_m)));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	t_0 = 0.5 * (im_m / sqrt(-re));
	tmp = 0.0;
	if (re <= -2.6e+92)
		tmp = t_0;
	elseif (re <= -1.12e+79)
		tmp = sqrt((im_m * 0.5));
	elseif (re <= -3.1e-79)
		tmp = t_0;
	elseif (re <= 3.5e-12)
		tmp = sqrt((0.5 * (re + im_m)));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := Block[{t$95$0 = N[(0.5 * N[(im$95$m / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -2.6e+92], t$95$0, If[LessEqual[re, -1.12e+79], N[Sqrt[N[(im$95$m * 0.5), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, -3.1e-79], t$95$0, If[LessEqual[re, 3.5e-12], N[Sqrt[N[(0.5 * N[(re + im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
t_0 := 0.5 \cdot \frac{im\_m}{\sqrt{-re}}\\
\mathbf{if}\;re \leq -2.6 \cdot 10^{+92}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq -1.12 \cdot 10^{+79}:\\
\;\;\;\;\sqrt{im\_m \cdot 0.5}\\

\mathbf{elif}\;re \leq -3.1 \cdot 10^{-79}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;re \leq 3.5 \cdot 10^{-12}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\_m\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -2.5999999999999999e92 or -1.12e79 < re < -3.0999999999999999e-79

    1. Initial program 12.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot {\left({\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{\color{blue}{\left(3 \cdot 0.3333333333333333\right)}} \cdot \sqrt{2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)}\right)}^{0.3333333333333333} \]
      6. pow1/236.1%

        \[\leadsto 0.5 \cdot {\left({\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{\left(3 \cdot 0.3333333333333333\right)} \cdot \color{blue}{{\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{0.5}}\right)}^{0.3333333333333333} \]
      7. pow-prod-up36.1%

        \[\leadsto 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(\frac{{im}^{2}}{re} \cdot -0.5\right)\right)}^{\left(3 \cdot 0.3333333333333333 + 0.5\right)}\right)}}^{0.3333333333333333} \]
      8. *-commutative36.1%

        \[\leadsto 0.5 \cdot {\left({\color{blue}{\left(\left(\frac{{im}^{2}}{re} \cdot -0.5\right) \cdot 2\right)}}^{\left(3 \cdot 0.3333333333333333 + 0.5\right)}\right)}^{0.3333333333333333} \]
      9. associate-*l*36.1%

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

        \[\leadsto 0.5 \cdot {\left({\left(\frac{{im}^{2}}{re} \cdot \color{blue}{-1}\right)}^{\left(3 \cdot 0.3333333333333333 + 0.5\right)}\right)}^{0.3333333333333333} \]
      11. metadata-eval36.1%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{{im}^{2}}{re} \cdot -1\right)}^{\left(\color{blue}{1} + 0.5\right)}\right)}^{0.3333333333333333} \]
      12. metadata-eval36.1%

        \[\leadsto 0.5 \cdot {\left({\left(\frac{{im}^{2}}{re} \cdot -1\right)}^{\color{blue}{1.5}}\right)}^{0.3333333333333333} \]
    9. Applied egg-rr36.1%

      \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\frac{{im}^{2}}{re} \cdot -1\right)}^{1.5}\right)}^{0.3333333333333333}} \]
    10. Step-by-step derivation
      1. unpow1/337.5%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt[3]{{\left(\frac{\color{blue}{-{im}^{2}}}{re}\right)}^{1.5}} \]
    11. Simplified37.5%

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt[3]{{\left(\frac{-{im}^{2}}{re}\right)}^{1.5}}} \]
    12. Step-by-step derivation
      1. pow1/336.1%

        \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\frac{-{im}^{2}}{re}\right)}^{1.5}\right)}^{0.3333333333333333}} \]
      2. pow-pow46.7%

        \[\leadsto 0.5 \cdot \color{blue}{{\left(\frac{-{im}^{2}}{re}\right)}^{\left(1.5 \cdot 0.3333333333333333\right)}} \]
      3. metadata-eval46.7%

        \[\leadsto 0.5 \cdot {\left(\frac{-{im}^{2}}{re}\right)}^{\color{blue}{0.5}} \]
      4. pow1/246.7%

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

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

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{-\left(-{im}^{2}\right)}}{\sqrt{-re}}} \]
      7. remove-double-neg55.6%

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

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{im} \cdot \sqrt{im}}}{\sqrt{-re}} \]
      10. add-sqr-sqrt50.0%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{-re}} \]
    13. Applied egg-rr50.0%

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

    if -2.5999999999999999e92 < re < -1.12e79

    1. Initial program 61.8%

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

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

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

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

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

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

        \[\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 re around 0 59.7%

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{im} \cdot \left(\sqrt{2} \cdot 0.5\right)\right)\right)} \]
      2. expm1-udef54.0%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{im} \cdot \left(\sqrt{2} \cdot 0.5\right)\right)} - 1} \]
      3. add-sqr-sqrt54.0%

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

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

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

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

        \[\leadsto e^{\mathsf{log1p}\left(\sqrt{im \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \left(0.5 \cdot 0.5\right)\right)}}\right)} - 1 \]
      8. rem-square-sqrt54.0%

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

        \[\leadsto e^{\mathsf{log1p}\left(\sqrt{im \cdot \left(2 \cdot \color{blue}{0.25}\right)}\right)} - 1 \]
      10. metadata-eval54.0%

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{im \cdot 0.5}\right)} - 1} \]
    10. Step-by-step derivation
      1. expm1-def54.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{im \cdot 0.5}\right)\right)} \]
      2. expm1-log1p60.0%

        \[\leadsto \color{blue}{\sqrt{im \cdot 0.5}} \]
    11. Simplified60.0%

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

    if -3.0999999999999999e-79 < re < 3.5e-12

    1. Initial program 59.9%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{re \cdot re + im \cdot im}}\right)} \]
    3. Simplified92.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. add-sqr-sqrt91.5%

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

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

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

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

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

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

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

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

      \[\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*92.2%

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

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

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

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

    if 3.5e-12 < re

    1. Initial program 39.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{re}} \]
    7. Simplified82.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.6 \cdot 10^{+92}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -1.12 \cdot 10^{+79}:\\ \;\;\;\;\sqrt{im \cdot 0.5}\\ \mathbf{elif}\;re \leq -3.1 \cdot 10^{-79}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 3.5 \cdot 10^{-12}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 64.3% accurate, 2.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq 3.2 \cdot 10^{-49}:\\ \;\;\;\;\sqrt{im\_m \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{re}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re 3.2e-49) (sqrt (* im_m 0.5)) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= 3.2e-49) {
		tmp = sqrt((im_m * 0.5));
	} 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 <= 3.2d-49) then
        tmp = sqrt((im_m * 0.5d0))
    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 <= 3.2e-49) {
		tmp = Math.sqrt((im_m * 0.5));
	} else {
		tmp = Math.sqrt(re);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= 3.2e-49:
		tmp = math.sqrt((im_m * 0.5))
	else:
		tmp = math.sqrt(re)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= 3.2e-49)
		tmp = sqrt(Float64(im_m * 0.5));
	else
		tmp = sqrt(re);
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= 3.2e-49)
		tmp = sqrt((im_m * 0.5));
	else
		tmp = sqrt(re);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, 3.2e-49], N[Sqrt[N[(im$95$m * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.2 \cdot 10^{-49}:\\
\;\;\;\;\sqrt{im\_m \cdot 0.5}\\

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


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

    1. Initial program 39.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\mathsf{log1p}\left(\sqrt{im \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \left(0.5 \cdot 0.5\right)\right)}}\right)} - 1 \]
      8. rem-square-sqrt16.3%

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

        \[\leadsto e^{\mathsf{log1p}\left(\sqrt{im \cdot \left(2 \cdot \color{blue}{0.25}\right)}\right)} - 1 \]
      10. metadata-eval16.3%

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

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

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

        \[\leadsto \color{blue}{\sqrt{im \cdot 0.5}} \]
    11. Simplified25.3%

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

    if 3.20000000000000002e-49 < re

    1. Initial program 45.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 26.3% 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 41.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{re}} \]
  7. Simplified29.2%

    \[\leadsto \color{blue}{\sqrt{re}} \]
  8. Final simplification29.2%

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

Developer target: 48.1% 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 2024026 
(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)))))