math.sqrt on complex, real part

Percentage Accurate: 40.9% → 89.7%
Time: 9.4s
Alternatives: 10
Speedup: 1.4×

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 10 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.9% accurate, 1.0× speedup?

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

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

Alternative 1: 89.7% accurate, 0.3× 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:\\ \;\;\;\;\frac{im\_m}{\sqrt{-re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \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)
   (* (/ im_m (sqrt (- re))) 0.5)
   (* 0.5 (sqrt (* 2.0 (+ 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 = (im_m / sqrt(-re)) * 0.5;
	} else {
		tmp = 0.5 * sqrt((2.0 * (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 = (im_m / Math.sqrt(-re)) * 0.5;
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (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 = (im_m / math.sqrt(-re)) * 0.5
	else:
		tmp = 0.5 * math.sqrt((2.0 * (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(Float64(im_m / sqrt(Float64(-re))) * 0.5);
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * 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 = (im_m / sqrt(-re)) * 0.5;
	else
		tmp = 0.5 * sqrt((2.0 * (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[(N[(im$95$m / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im$95$m ^ 2], $MachinePrecision]), $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:\\
\;\;\;\;\frac{im\_m}{\sqrt{-re}} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \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 7.6%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\color{blue}{\frac{{im}^{2}}{re}}\right)} \]
      4. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\frac{\color{blue}{im \cdot im}}{re}\right)} \]
      5. *-lowering-*.f6440.0

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im \cdot im}{re}}} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{\mathsf{neg}\left(\frac{im \cdot im}{re}\right)} \cdot \frac{1}{2}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{\mathsf{neg}\left(\frac{im \cdot im}{re}\right)} \cdot \frac{1}{2}} \]
      3. distribute-neg-frac2N/A

        \[\leadsto \sqrt{\color{blue}{\frac{im \cdot im}{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      4. sqrt-divN/A

        \[\leadsto \color{blue}{\frac{\sqrt{im \cdot im}}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      5. pow2N/A

        \[\leadsto \frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      6. sqrt-pow1N/A

        \[\leadsto \frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      7. metadata-evalN/A

        \[\leadsto \frac{{im}^{\color{blue}{1}}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      8. unpow1N/A

        \[\leadsto \frac{\color{blue}{im}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      9. /-lowering-/.f64N/A

        \[\leadsto \color{blue}{\frac{im}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      10. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{im}{\color{blue}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      11. neg-lowering-neg.f6451.4

        \[\leadsto \frac{im}{\sqrt{\color{blue}{-re}}} \cdot 0.5 \]
    7. Applied egg-rr51.4%

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

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

    1. Initial program 50.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. accelerator-lowering-hypot.f6490.4

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

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

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

Alternative 2: 75.2% accurate, 0.9× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -1.35 \cdot 10^{-115}:\\ \;\;\;\;0.5 \cdot \left(im\_m \cdot \sqrt{\frac{-1}{re}}\right)\\ \mathbf{elif}\;re \leq 4200000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\_m\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(im\_m, \frac{im\_m}{re}, re \cdot 4\right)}\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re -1.35e-115)
   (* 0.5 (* im_m (sqrt (/ -1.0 re))))
   (if (<= re 4200000.0)
     (* 0.5 (sqrt (* 2.0 (+ re im_m))))
     (* 0.5 (sqrt (fma im_m (/ im_m re) (* re 4.0)))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= -1.35e-115) {
		tmp = 0.5 * (im_m * sqrt((-1.0 / re)));
	} else if (re <= 4200000.0) {
		tmp = 0.5 * sqrt((2.0 * (re + im_m)));
	} else {
		tmp = 0.5 * sqrt(fma(im_m, (im_m / re), (re * 4.0)));
	}
	return tmp;
}
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= -1.35e-115)
		tmp = Float64(0.5 * Float64(im_m * sqrt(Float64(-1.0 / re))));
	elseif (re <= 4200000.0)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im_m))));
	else
		tmp = Float64(0.5 * sqrt(fma(im_m, Float64(im_m / re), Float64(re * 4.0))));
	end
	return tmp
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, -1.35e-115], N[(0.5 * N[(im$95$m * N[Sqrt[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4200000.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im$95$m * N[(im$95$m / re), $MachinePrecision] + N[(re * 4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.35 \cdot 10^{-115}:\\
\;\;\;\;0.5 \cdot \left(im\_m \cdot \sqrt{\frac{-1}{re}}\right)\\

\mathbf{elif}\;re \leq 4200000:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\_m\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(im\_m, \frac{im\_m}{re}, re \cdot 4\right)}\\


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

    1. Initial program 18.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\color{blue}{\frac{{im}^{2}}{re}}\right)} \]
      4. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\frac{\color{blue}{im \cdot im}}{re}\right)} \]
      5. *-lowering-*.f6438.4

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im \cdot im}{re}}} \]
    6. Step-by-step derivation
      1. distribute-neg-frac2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\frac{im \cdot im}{\mathsf{neg}\left(re\right)}}} \]
      2. div-invN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\left(im \cdot im\right) \cdot \frac{1}{\mathsf{neg}\left(re\right)}}} \]
      3. sqrt-prodN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\sqrt{im \cdot im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      4. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\sqrt{\color{blue}{{im}^{2}}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      5. sqrt-pow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{{im}^{\left(\frac{2}{2}\right)}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      6. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left({im}^{\color{blue}{1}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      7. unpow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      9. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}}\right) \]
      10. distribute-frac-neg2N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{1}{re}\right)}}\right) \]
      11. distribute-neg-fracN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\frac{\mathsf{neg}\left(1\right)}{re}}}\right) \]
      12. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\frac{\color{blue}{-1}}{re}}\right) \]
      13. /-lowering-/.f6441.2

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

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

    if -1.35e-115 < re < 4.2e6

    1. Initial program 61.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      3. +-lowering-+.f6443.3

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

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

    if 4.2e6 < re

    1. Initial program 48.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in im around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{4 \cdot re + \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\frac{{im}^{2}}{re} + 4 \cdot re}} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re} + 4 \cdot re} \]
      3. associate-/l*N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{im \cdot \frac{im}{re}} + 4 \cdot re} \]
      4. accelerator-lowering-fma.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{fma}\left(im, \frac{im}{re}, 4 \cdot re\right)}} \]
      5. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{fma}\left(im, \color{blue}{\frac{im}{re}}, 4 \cdot re\right)} \]
      6. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{fma}\left(im, \frac{im}{re}, \color{blue}{re \cdot 4}\right)} \]
      7. *-lowering-*.f6478.8

        \[\leadsto 0.5 \cdot \sqrt{\mathsf{fma}\left(im, \frac{im}{re}, \color{blue}{re \cdot 4}\right)} \]
    5. Simplified78.8%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\mathsf{fma}\left(im, \frac{im}{re}, re \cdot 4\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification50.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.35 \cdot 10^{-115}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{-1}{re}}\right)\\ \mathbf{elif}\;re \leq 4200000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(im, \frac{im}{re}, re \cdot 4\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 75.3% accurate, 1.2× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -1.5 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \left(im\_m \cdot \sqrt{\frac{-1}{re}}\right)\\ \mathbf{elif}\;re \leq 30000000:\\ \;\;\;\;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 -1.5e-116)
   (* 0.5 (* im_m (sqrt (/ -1.0 re))))
   (if (<= re 30000000.0) (* 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 <= -1.5e-116) {
		tmp = 0.5 * (im_m * sqrt((-1.0 / re)));
	} else if (re <= 30000000.0) {
		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 <= (-1.5d-116)) then
        tmp = 0.5d0 * (im_m * sqrt(((-1.0d0) / re)))
    else if (re <= 30000000.0d0) 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 <= -1.5e-116) {
		tmp = 0.5 * (im_m * Math.sqrt((-1.0 / re)));
	} else if (re <= 30000000.0) {
		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 <= -1.5e-116:
		tmp = 0.5 * (im_m * math.sqrt((-1.0 / re)))
	elif re <= 30000000.0:
		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 <= -1.5e-116)
		tmp = Float64(0.5 * Float64(im_m * sqrt(Float64(-1.0 / re))));
	elseif (re <= 30000000.0)
		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 <= -1.5e-116)
		tmp = 0.5 * (im_m * sqrt((-1.0 / re)));
	elseif (re <= 30000000.0)
		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, -1.5e-116], N[(0.5 * N[(im$95$m * N[Sqrt[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 30000000.0], 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 -1.5 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \left(im\_m \cdot \sqrt{\frac{-1}{re}}\right)\\

\mathbf{elif}\;re \leq 30000000:\\
\;\;\;\;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 < -1.50000000000000013e-116

    1. Initial program 18.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\color{blue}{\frac{{im}^{2}}{re}}\right)} \]
      4. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\frac{\color{blue}{im \cdot im}}{re}\right)} \]
      5. *-lowering-*.f6438.4

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im \cdot im}{re}}} \]
    6. Step-by-step derivation
      1. distribute-neg-frac2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\frac{im \cdot im}{\mathsf{neg}\left(re\right)}}} \]
      2. div-invN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\left(im \cdot im\right) \cdot \frac{1}{\mathsf{neg}\left(re\right)}}} \]
      3. sqrt-prodN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\sqrt{im \cdot im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      4. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\sqrt{\color{blue}{{im}^{2}}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      5. sqrt-pow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{{im}^{\left(\frac{2}{2}\right)}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      6. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left({im}^{\color{blue}{1}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      7. unpow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      9. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}}\right) \]
      10. distribute-frac-neg2N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{1}{re}\right)}}\right) \]
      11. distribute-neg-fracN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\frac{\mathsf{neg}\left(1\right)}{re}}}\right) \]
      12. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\frac{\color{blue}{-1}}{re}}\right) \]
      13. /-lowering-/.f6441.2

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

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

    if -1.50000000000000013e-116 < re < 3e7

    1. Initial program 61.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      3. +-lowering-+.f6443.3

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

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

    if 3e7 < re

    1. Initial program 48.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6478.5

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified78.5%

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

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

Alternative 4: 75.2% accurate, 1.3× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{-115}:\\ \;\;\;\;\frac{im\_m \cdot 0.5}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 48000:\\ \;\;\;\;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 -5e-115)
   (/ (* im_m 0.5) (sqrt (- re)))
   (if (<= re 48000.0) (* 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 <= -5e-115) {
		tmp = (im_m * 0.5) / sqrt(-re);
	} else if (re <= 48000.0) {
		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 <= (-5d-115)) then
        tmp = (im_m * 0.5d0) / sqrt(-re)
    else if (re <= 48000.0d0) 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 <= -5e-115) {
		tmp = (im_m * 0.5) / Math.sqrt(-re);
	} else if (re <= 48000.0) {
		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 <= -5e-115:
		tmp = (im_m * 0.5) / math.sqrt(-re)
	elif re <= 48000.0:
		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 <= -5e-115)
		tmp = Float64(Float64(im_m * 0.5) / sqrt(Float64(-re)));
	elseif (re <= 48000.0)
		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 <= -5e-115)
		tmp = (im_m * 0.5) / sqrt(-re);
	elseif (re <= 48000.0)
		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, -5e-115], N[(N[(im$95$m * 0.5), $MachinePrecision] / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 48000.0], 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 -5 \cdot 10^{-115}:\\
\;\;\;\;\frac{im\_m \cdot 0.5}{\sqrt{-re}}\\

\mathbf{elif}\;re \leq 48000:\\
\;\;\;\;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 < -5.0000000000000003e-115

    1. Initial program 18.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\color{blue}{\frac{{im}^{2}}{re}}\right)} \]
      4. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\frac{\color{blue}{im \cdot im}}{re}\right)} \]
      5. *-lowering-*.f6438.4

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im \cdot im}{re}}} \]
    6. Step-by-step derivation
      1. distribute-neg-frac2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\frac{im \cdot im}{\mathsf{neg}\left(re\right)}}} \]
      2. div-invN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\left(im \cdot im\right) \cdot \frac{1}{\mathsf{neg}\left(re\right)}}} \]
      3. sqrt-prodN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\sqrt{im \cdot im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      4. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\sqrt{\color{blue}{{im}^{2}}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      5. sqrt-pow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{{im}^{\left(\frac{2}{2}\right)}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      6. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left({im}^{\color{blue}{1}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      7. unpow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      9. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}}\right) \]
      10. distribute-frac-neg2N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{1}{re}\right)}}\right) \]
      11. distribute-neg-fracN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\frac{\mathsf{neg}\left(1\right)}{re}}}\right) \]
      12. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\frac{\color{blue}{-1}}{re}}\right) \]
      13. /-lowering-/.f6441.2

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{-1}{re}}\right)} \]
    8. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot im\right) \cdot \sqrt{\frac{-1}{re}}} \]
      2. frac-2negN/A

        \[\leadsto \left(\frac{1}{2} \cdot im\right) \cdot \sqrt{\color{blue}{\frac{\mathsf{neg}\left(-1\right)}{\mathsf{neg}\left(re\right)}}} \]
      3. metadata-evalN/A

        \[\leadsto \left(\frac{1}{2} \cdot im\right) \cdot \sqrt{\frac{\color{blue}{1}}{\mathsf{neg}\left(re\right)}} \]
      4. sqrt-divN/A

        \[\leadsto \left(\frac{1}{2} \cdot im\right) \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{\mathsf{neg}\left(re\right)}}} \]
      5. metadata-evalN/A

        \[\leadsto \left(\frac{1}{2} \cdot im\right) \cdot \frac{\color{blue}{1}}{\sqrt{\mathsf{neg}\left(re\right)}} \]
      6. un-div-invN/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot im}{\sqrt{\mathsf{neg}\left(re\right)}}} \]
      7. /-lowering-/.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot im}{\sqrt{\mathsf{neg}\left(re\right)}}} \]
      8. *-lowering-*.f64N/A

        \[\leadsto \frac{\color{blue}{\frac{1}{2} \cdot im}}{\sqrt{\mathsf{neg}\left(re\right)}} \]
      9. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{\frac{1}{2} \cdot im}{\color{blue}{\sqrt{\mathsf{neg}\left(re\right)}}} \]
      10. neg-lowering-neg.f6441.2

        \[\leadsto \frac{0.5 \cdot im}{\sqrt{\color{blue}{-re}}} \]
    9. Applied egg-rr41.2%

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

    if -5.0000000000000003e-115 < re < 48000

    1. Initial program 61.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      3. +-lowering-+.f6443.3

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

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

    if 48000 < re

    1. Initial program 48.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6478.5

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified78.5%

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

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

Alternative 5: 75.2% accurate, 1.3× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -1.35 \cdot 10^{-115}:\\ \;\;\;\;\frac{im\_m}{\sqrt{-re}} \cdot 0.5\\ \mathbf{elif}\;re \leq 760:\\ \;\;\;\;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 -1.35e-115)
   (* (/ im_m (sqrt (- re))) 0.5)
   (if (<= re 760.0) (* 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 <= -1.35e-115) {
		tmp = (im_m / sqrt(-re)) * 0.5;
	} else if (re <= 760.0) {
		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 <= (-1.35d-115)) then
        tmp = (im_m / sqrt(-re)) * 0.5d0
    else if (re <= 760.0d0) 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 <= -1.35e-115) {
		tmp = (im_m / Math.sqrt(-re)) * 0.5;
	} else if (re <= 760.0) {
		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 <= -1.35e-115:
		tmp = (im_m / math.sqrt(-re)) * 0.5
	elif re <= 760.0:
		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 <= -1.35e-115)
		tmp = Float64(Float64(im_m / sqrt(Float64(-re))) * 0.5);
	elseif (re <= 760.0)
		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 <= -1.35e-115)
		tmp = (im_m / sqrt(-re)) * 0.5;
	elseif (re <= 760.0)
		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, -1.35e-115], N[(N[(im$95$m / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[re, 760.0], 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 -1.35 \cdot 10^{-115}:\\
\;\;\;\;\frac{im\_m}{\sqrt{-re}} \cdot 0.5\\

\mathbf{elif}\;re \leq 760:\\
\;\;\;\;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 < -1.35e-115

    1. Initial program 18.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\color{blue}{\frac{{im}^{2}}{re}}\right)} \]
      4. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\frac{\color{blue}{im \cdot im}}{re}\right)} \]
      5. *-lowering-*.f6438.4

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im \cdot im}{re}}} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{\mathsf{neg}\left(\frac{im \cdot im}{re}\right)} \cdot \frac{1}{2}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{\mathsf{neg}\left(\frac{im \cdot im}{re}\right)} \cdot \frac{1}{2}} \]
      3. distribute-neg-frac2N/A

        \[\leadsto \sqrt{\color{blue}{\frac{im \cdot im}{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      4. sqrt-divN/A

        \[\leadsto \color{blue}{\frac{\sqrt{im \cdot im}}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      5. pow2N/A

        \[\leadsto \frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      6. sqrt-pow1N/A

        \[\leadsto \frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      7. metadata-evalN/A

        \[\leadsto \frac{{im}^{\color{blue}{1}}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      8. unpow1N/A

        \[\leadsto \frac{\color{blue}{im}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot \frac{1}{2} \]
      9. /-lowering-/.f64N/A

        \[\leadsto \color{blue}{\frac{im}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      10. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{im}{\color{blue}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot \frac{1}{2} \]
      11. neg-lowering-neg.f6441.2

        \[\leadsto \frac{im}{\sqrt{\color{blue}{-re}}} \cdot 0.5 \]
    7. Applied egg-rr41.2%

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

    if -1.35e-115 < re < 760

    1. Initial program 61.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      3. +-lowering-+.f6443.3

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

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

    if 760 < re

    1. Initial program 48.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6478.5

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified78.5%

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

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

Alternative 6: 75.2% accurate, 1.3× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -5 \cdot 10^{-115}:\\ \;\;\;\;im\_m \cdot \frac{0.5}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 23000:\\ \;\;\;\;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 -5e-115)
   (* im_m (/ 0.5 (sqrt (- re))))
   (if (<= re 23000.0) (* 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 <= -5e-115) {
		tmp = im_m * (0.5 / sqrt(-re));
	} else if (re <= 23000.0) {
		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 <= (-5d-115)) then
        tmp = im_m * (0.5d0 / sqrt(-re))
    else if (re <= 23000.0d0) 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 <= -5e-115) {
		tmp = im_m * (0.5 / Math.sqrt(-re));
	} else if (re <= 23000.0) {
		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 <= -5e-115:
		tmp = im_m * (0.5 / math.sqrt(-re))
	elif re <= 23000.0:
		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 <= -5e-115)
		tmp = Float64(im_m * Float64(0.5 / sqrt(Float64(-re))));
	elseif (re <= 23000.0)
		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 <= -5e-115)
		tmp = im_m * (0.5 / sqrt(-re));
	elseif (re <= 23000.0)
		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, -5e-115], N[(im$95$m * N[(0.5 / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 23000.0], 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 -5 \cdot 10^{-115}:\\
\;\;\;\;im\_m \cdot \frac{0.5}{\sqrt{-re}}\\

\mathbf{elif}\;re \leq 23000:\\
\;\;\;\;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 < -5.0000000000000003e-115

    1. Initial program 18.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around -inf

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      2. neg-lowering-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. /-lowering-/.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\color{blue}{\frac{{im}^{2}}{re}}\right)} \]
      4. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\mathsf{neg}\left(\frac{\color{blue}{im \cdot im}}{re}\right)} \]
      5. *-lowering-*.f6438.4

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im \cdot im}{re}}} \]
    6. Step-by-step derivation
      1. distribute-neg-frac2N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\frac{im \cdot im}{\mathsf{neg}\left(re\right)}}} \]
      2. div-invN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\left(im \cdot im\right) \cdot \frac{1}{\mathsf{neg}\left(re\right)}}} \]
      3. sqrt-prodN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\sqrt{im \cdot im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      4. pow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\sqrt{\color{blue}{{im}^{2}}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      5. sqrt-pow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{{im}^{\left(\frac{2}{2}\right)}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      6. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left({im}^{\color{blue}{1}} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      7. unpow1N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{im} \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}\right)} \]
      9. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \color{blue}{\sqrt{\frac{1}{\mathsf{neg}\left(re\right)}}}\right) \]
      10. distribute-frac-neg2N/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{1}{re}\right)}}\right) \]
      11. distribute-neg-fracN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\frac{\mathsf{neg}\left(1\right)}{re}}}\right) \]
      12. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\frac{\color{blue}{-1}}{re}}\right) \]
      13. /-lowering-/.f6441.2

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{-1}{re}}\right)} \]
    8. Step-by-step derivation
      1. frac-2negN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\color{blue}{\frac{\mathsf{neg}\left(-1\right)}{\mathsf{neg}\left(re\right)}}}\right) \]
      2. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \sqrt{\frac{\color{blue}{1}}{\mathsf{neg}\left(re\right)}}\right) \]
      3. sqrt-divN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \color{blue}{\frac{\sqrt{1}}{\sqrt{\mathsf{neg}\left(re\right)}}}\right) \]
      4. metadata-evalN/A

        \[\leadsto \frac{1}{2} \cdot \left(im \cdot \frac{\color{blue}{1}}{\sqrt{\mathsf{neg}\left(re\right)}}\right) \]
      5. div-invN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\frac{im}{\sqrt{\mathsf{neg}\left(re\right)}}} \]
      6. associate-*r/N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2} \cdot im}{\sqrt{\mathsf{neg}\left(re\right)}}} \]
      7. associate-*l/N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot im} \]
      8. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2}}{\sqrt{\mathsf{neg}\left(re\right)}} \cdot im} \]
      9. /-lowering-/.f64N/A

        \[\leadsto \color{blue}{\frac{\frac{1}{2}}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot im \]
      10. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \frac{\frac{1}{2}}{\color{blue}{\sqrt{\mathsf{neg}\left(re\right)}}} \cdot im \]
      11. neg-lowering-neg.f6441.2

        \[\leadsto \frac{0.5}{\sqrt{\color{blue}{-re}}} \cdot im \]
    9. Applied egg-rr41.2%

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

    if -5.0000000000000003e-115 < re < 23000

    1. Initial program 61.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      3. +-lowering-+.f6443.3

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

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

    if 23000 < re

    1. Initial program 48.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6478.5

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified78.5%

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

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

Alternative 7: 64.5% accurate, 1.3× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -2.1 \cdot 10^{+122}:\\ \;\;\;\;0\\ \mathbf{elif}\;re \leq 550000:\\ \;\;\;\;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.1e+122)
   0.0
   (if (<= re 550000.0) (* 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.1e+122) {
		tmp = 0.0;
	} else if (re <= 550000.0) {
		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.1d+122)) then
        tmp = 0.0d0
    else if (re <= 550000.0d0) 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.1e+122) {
		tmp = 0.0;
	} else if (re <= 550000.0) {
		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.1e+122:
		tmp = 0.0
	elif re <= 550000.0:
		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.1e+122)
		tmp = 0.0;
	elseif (re <= 550000.0)
		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.1e+122)
		tmp = 0.0;
	elseif (re <= 550000.0)
		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.1e+122], 0.0, If[LessEqual[re, 550000.0], 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.1 \cdot 10^{+122}:\\
\;\;\;\;0\\

\mathbf{elif}\;re \leq 550000:\\
\;\;\;\;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.10000000000000016e122

    1. Initial program 6.3%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      3. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
      4. *-lowering-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
      5. +-commutativeN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
      6. +-lowering-+.f64N/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
      7. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\sqrt{re \cdot re + im \cdot im}}\right)} \cdot \frac{1}{2} \]
      8. accelerator-lowering-fma.f64N/A

        \[\leadsto \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}}\right)} \cdot \frac{1}{2} \]
      9. *-lowering-*.f646.3

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

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

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{-1 \cdot re}\right)} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(\mathsf{neg}\left(re\right)\right)}\right)} \cdot \frac{1}{2} \]
      2. neg-lowering-neg.f6424.1

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

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(-re\right)}\right)} \cdot 0.5 \]
    8. Step-by-step derivation
      1. unsub-negN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re - re\right)}} \cdot \frac{1}{2} \]
      2. +-inversesN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{0}} \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \sqrt{\color{blue}{0}} \cdot \frac{1}{2} \]
      4. +-inversesN/A

        \[\leadsto \sqrt{\color{blue}{re - re}} \cdot \frac{1}{2} \]
      5. unsub-negN/A

        \[\leadsto \sqrt{\color{blue}{re + \left(\mathsf{neg}\left(re\right)\right)}} \cdot \frac{1}{2} \]
      6. pow1/2N/A

        \[\leadsto \color{blue}{{\left(re + \left(\mathsf{neg}\left(re\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
      7. unsub-negN/A

        \[\leadsto {\color{blue}{\left(re - re\right)}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      8. +-inversesN/A

        \[\leadsto {\color{blue}{0}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      9. metadata-evalN/A

        \[\leadsto \color{blue}{0} \cdot \frac{1}{2} \]
      10. metadata-eval24.1

        \[\leadsto \color{blue}{0} \]
    9. Applied egg-rr24.1%

      \[\leadsto \color{blue}{0} \]

    if -2.10000000000000016e122 < re < 5.5e5

    1. Initial program 52.6%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot \left(im + re\right)}} \]
      3. +-lowering-+.f6436.7

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

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

    if 5.5e5 < re

    1. Initial program 48.8%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6478.5

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified78.5%

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

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

Alternative 8: 64.7% accurate, 1.4× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -5.8 \cdot 10^{+152}:\\ \;\;\;\;0\\ \mathbf{elif}\;re \leq 1.8:\\ \;\;\;\;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 -5.8e+152)
   0.0
   (if (<= re 1.8) (* 0.5 (sqrt (* im_m 2.0))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= -5.8e+152) {
		tmp = 0.0;
	} else if (re <= 1.8) {
		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 <= (-5.8d+152)) then
        tmp = 0.0d0
    else if (re <= 1.8d0) 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 <= -5.8e+152) {
		tmp = 0.0;
	} else if (re <= 1.8) {
		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 <= -5.8e+152:
		tmp = 0.0
	elif re <= 1.8:
		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 <= -5.8e+152)
		tmp = 0.0;
	elseif (re <= 1.8)
		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 <= -5.8e+152)
		tmp = 0.0;
	elseif (re <= 1.8)
		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, -5.8e+152], 0.0, If[LessEqual[re, 1.8], 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 -5.8 \cdot 10^{+152}:\\
\;\;\;\;0\\

\mathbf{elif}\;re \leq 1.8:\\
\;\;\;\;0.5 \cdot \sqrt{im\_m \cdot 2}\\

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


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

    1. Initial program 5.5%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      3. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
      4. *-lowering-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
      5. +-commutativeN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
      6. +-lowering-+.f64N/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
      7. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\sqrt{re \cdot re + im \cdot im}}\right)} \cdot \frac{1}{2} \]
      8. accelerator-lowering-fma.f64N/A

        \[\leadsto \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}}\right)} \cdot \frac{1}{2} \]
      9. *-lowering-*.f645.5

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

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

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{-1 \cdot re}\right)} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(\mathsf{neg}\left(re\right)\right)}\right)} \cdot \frac{1}{2} \]
      2. neg-lowering-neg.f6427.8

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

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(-re\right)}\right)} \cdot 0.5 \]
    8. Step-by-step derivation
      1. unsub-negN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re - re\right)}} \cdot \frac{1}{2} \]
      2. +-inversesN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{0}} \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \sqrt{\color{blue}{0}} \cdot \frac{1}{2} \]
      4. +-inversesN/A

        \[\leadsto \sqrt{\color{blue}{re - re}} \cdot \frac{1}{2} \]
      5. unsub-negN/A

        \[\leadsto \sqrt{\color{blue}{re + \left(\mathsf{neg}\left(re\right)\right)}} \cdot \frac{1}{2} \]
      6. pow1/2N/A

        \[\leadsto \color{blue}{{\left(re + \left(\mathsf{neg}\left(re\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
      7. unsub-negN/A

        \[\leadsto {\color{blue}{\left(re - re\right)}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      8. +-inversesN/A

        \[\leadsto {\color{blue}{0}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      9. metadata-evalN/A

        \[\leadsto \color{blue}{0} \cdot \frac{1}{2} \]
      10. metadata-eval27.8

        \[\leadsto \color{blue}{0} \]
    9. Applied egg-rr27.8%

      \[\leadsto \color{blue}{0} \]

    if -5.7999999999999997e152 < re < 1.80000000000000004

    1. Initial program 51.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{im \cdot 2}} \]
      2. *-lowering-*.f6434.6

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

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

    if 1.80000000000000004 < re

    1. Initial program 48.9%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6477.6

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified77.6%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 9: 31.4% accurate, 2.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;re \leq -1 \cdot 10^{-309}:\\
\;\;\;\;0\\

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


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

    1. Initial program 30.7%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      2. *-lowering-*.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      3. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
      4. *-lowering-*.f64N/A

        \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
      5. +-commutativeN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
      6. +-lowering-+.f64N/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
      7. sqrt-lowering-sqrt.f64N/A

        \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\sqrt{re \cdot re + im \cdot im}}\right)} \cdot \frac{1}{2} \]
      8. accelerator-lowering-fma.f64N/A

        \[\leadsto \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}}\right)} \cdot \frac{1}{2} \]
      9. *-lowering-*.f6430.7

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

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

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{-1 \cdot re}\right)} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(\mathsf{neg}\left(re\right)\right)}\right)} \cdot \frac{1}{2} \]
      2. neg-lowering-neg.f6410.1

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

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(-re\right)}\right)} \cdot 0.5 \]
    8. Step-by-step derivation
      1. unsub-negN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re - re\right)}} \cdot \frac{1}{2} \]
      2. +-inversesN/A

        \[\leadsto \sqrt{2 \cdot \color{blue}{0}} \cdot \frac{1}{2} \]
      3. metadata-evalN/A

        \[\leadsto \sqrt{\color{blue}{0}} \cdot \frac{1}{2} \]
      4. +-inversesN/A

        \[\leadsto \sqrt{\color{blue}{re - re}} \cdot \frac{1}{2} \]
      5. unsub-negN/A

        \[\leadsto \sqrt{\color{blue}{re + \left(\mathsf{neg}\left(re\right)\right)}} \cdot \frac{1}{2} \]
      6. pow1/2N/A

        \[\leadsto \color{blue}{{\left(re + \left(\mathsf{neg}\left(re\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
      7. unsub-negN/A

        \[\leadsto {\color{blue}{\left(re - re\right)}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      8. +-inversesN/A

        \[\leadsto {\color{blue}{0}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      9. metadata-evalN/A

        \[\leadsto \color{blue}{0} \cdot \frac{1}{2} \]
      10. metadata-eval10.1

        \[\leadsto \color{blue}{0} \]
    9. Applied egg-rr10.1%

      \[\leadsto \color{blue}{0} \]

    if -1.000000000000002e-309 < re

    1. Initial program 58.1%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in re around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(\sqrt{re} \cdot {\left(\sqrt{2}\right)}^{2}\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(\sqrt{2}\right)}^{2} \cdot \sqrt{re}\right)} \]
      2. unpow2N/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right)} \cdot \sqrt{re}\right) \]
      3. rem-square-sqrtN/A

        \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{2} \cdot \sqrt{re}\right) \]
      4. associate-*r*N/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot 2\right) \cdot \sqrt{re}} \]
      5. metadata-evalN/A

        \[\leadsto \color{blue}{1} \cdot \sqrt{re} \]
      6. *-lft-identityN/A

        \[\leadsto \color{blue}{\sqrt{re}} \]
      7. sqrt-lowering-sqrt.f6447.7

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Simplified47.7%

      \[\leadsto \color{blue}{\sqrt{re}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 10: 6.1% accurate, 47.0× speedup?

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

\\
0
\end{array}
Derivation
  1. Initial program 44.7%

    \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
    2. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
    3. sqrt-lowering-sqrt.f64N/A

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
    4. *-lowering-*.f64N/A

      \[\leadsto \sqrt{\color{blue}{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}} \cdot \frac{1}{2} \]
    5. +-commutativeN/A

      \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
    6. +-lowering-+.f64N/A

      \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}} \cdot \frac{1}{2} \]
    7. sqrt-lowering-sqrt.f64N/A

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\sqrt{re \cdot re + im \cdot im}}\right)} \cdot \frac{1}{2} \]
    8. accelerator-lowering-fma.f64N/A

      \[\leadsto \sqrt{2 \cdot \left(re + \sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}}\right)} \cdot \frac{1}{2} \]
    9. *-lowering-*.f6444.7

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

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

    \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{-1 \cdot re}\right)} \cdot \frac{1}{2} \]
  6. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(\mathsf{neg}\left(re\right)\right)}\right)} \cdot \frac{1}{2} \]
    2. neg-lowering-neg.f646.4

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

    \[\leadsto \sqrt{2 \cdot \left(re + \color{blue}{\left(-re\right)}\right)} \cdot 0.5 \]
  8. Step-by-step derivation
    1. unsub-negN/A

      \[\leadsto \sqrt{2 \cdot \color{blue}{\left(re - re\right)}} \cdot \frac{1}{2} \]
    2. +-inversesN/A

      \[\leadsto \sqrt{2 \cdot \color{blue}{0}} \cdot \frac{1}{2} \]
    3. metadata-evalN/A

      \[\leadsto \sqrt{\color{blue}{0}} \cdot \frac{1}{2} \]
    4. +-inversesN/A

      \[\leadsto \sqrt{\color{blue}{re - re}} \cdot \frac{1}{2} \]
    5. unsub-negN/A

      \[\leadsto \sqrt{\color{blue}{re + \left(\mathsf{neg}\left(re\right)\right)}} \cdot \frac{1}{2} \]
    6. pow1/2N/A

      \[\leadsto \color{blue}{{\left(re + \left(\mathsf{neg}\left(re\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
    7. unsub-negN/A

      \[\leadsto {\color{blue}{\left(re - re\right)}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
    8. +-inversesN/A

      \[\leadsto {\color{blue}{0}}^{\frac{1}{2}} \cdot \frac{1}{2} \]
    9. metadata-evalN/A

      \[\leadsto \color{blue}{0} \cdot \frac{1}{2} \]
    10. metadata-eval6.4

      \[\leadsto \color{blue}{0} \]
  9. Applied egg-rr6.4%

    \[\leadsto \color{blue}{0} \]
  10. Add Preprocessing

Developer Target 1: 47.9% accurate, 0.6× 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 2024199 
(FPCore (re im)
  :name "math.sqrt on complex, real part"
  :precision binary64

  :alt
  (! :herbie-platform default (if (< re 0) (* 1/2 (* (sqrt 2) (sqrt (/ (* im im) (- (modulus re im) re))))) (* 1/2 (sqrt (* 2 (+ (modulus re im) re))))))

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