math.sqrt on complex, real part

Percentage Accurate: 41.7% → 88.3%
Time: 10.1s
Alternatives: 6
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 6 alternatives:

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

Initial Program: 41.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: 88.3% accurate, 0.4× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq -8.8 \cdot 10^{-32}:\\ \;\;\;\;\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 -8.8e-32)
   (* (/ 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 <= -8.8e-32) {
		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 <= -8.8e-32) {
		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 <= -8.8e-32:
		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 (re <= -8.8e-32)
		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 <= -8.8e-32)
		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[re, -8.8e-32], 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 \leq -8.8 \cdot 10^{-32}:\\
\;\;\;\;\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 re < -8.7999999999999999e-32

    1. Initial program 15.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. lower-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. lower-/.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. lower-*.f6441.8

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
    5. Applied rewrites41.8%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{-\frac{im \cdot im}{re}} \cdot 0.5} \]
      7. lift-sqrt.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \frac{im}{\sqrt{\color{blue}{-re}}} \cdot 0.5 \]
    7. Applied rewrites51.8%

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

    if -8.7999999999999999e-32 < re

    1. Initial program 51.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. lower-hypot.f6495.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -8.8 \cdot 10^{-32}:\\ \;\;\;\;\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: 76.8% accurate, 1.3× speedup?

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

\mathbf{elif}\;re \leq 175000000:\\
\;\;\;\;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.25000000000000008e-38

    1. Initial program 16.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 \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. lower-neg.f64N/A

        \[\leadsto \frac{1}{2} \cdot \sqrt{\color{blue}{\mathsf{neg}\left(\frac{{im}^{2}}{re}\right)}} \]
      3. lower-/.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. lower-*.f6440.6

        \[\leadsto 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}} \]
    5. Applied rewrites40.6%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{-\frac{im \cdot im}{re}} \cdot 0.5} \]
      7. lift-sqrt.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.25000000000000008e-38 < re < 1.75e8

    1. Initial program 59.4%

      \[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. lower-*.f64N/A

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

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

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

    if 1.75e8 < re

    1. Initial program 37.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. lower-sqrt.f6480.2

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Applied rewrites80.2%

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

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

Alternative 3: 64.7% accurate, 1.3× speedup?

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

\mathbf{elif}\;re \leq 175000000:\\
\;\;\;\;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.7999999999999996e143

    1. Initial program 3.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. lift-*.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      9. lower-*.f643.8

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

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im, im, re \cdot re\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. lower-neg.f6427.8

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

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

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

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

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

        \[\leadsto \color{blue}{{\left(2 \cdot \left(re + \left(\mathsf{neg}\left(re\right)\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
      5. lift-*.f64N/A

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

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

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

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

        \[\leadsto {\left(2 \cdot \color{blue}{0}\right)}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      10. metadata-evalN/A

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

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

        \[\leadsto \color{blue}{0} \]
    9. Applied rewrites27.8%

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

    if -5.7999999999999996e143 < re < 1.75e8

    1. Initial program 53.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 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. lower-*.f64N/A

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

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

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

    if 1.75e8 < re

    1. Initial program 37.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. lower-sqrt.f6480.2

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Applied rewrites80.2%

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

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

Alternative 4: 65.0% accurate, 1.4× speedup?

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

\mathbf{elif}\;re \leq 175000000:\\
\;\;\;\;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 < -1.74999999999999996e177

    1. Initial program 2.1%

      \[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. lift-*.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
      9. lower-*.f642.1

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

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im, im, re \cdot re\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. lower-neg.f6431.4

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

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

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

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

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

        \[\leadsto \color{blue}{{\left(2 \cdot \left(re + \left(\mathsf{neg}\left(re\right)\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
      5. lift-*.f64N/A

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

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

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

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

        \[\leadsto {\left(2 \cdot \color{blue}{0}\right)}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      10. metadata-evalN/A

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

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

        \[\leadsto \color{blue}{0} \]
    9. Applied rewrites31.4%

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

    if -1.74999999999999996e177 < re < 1.75e8

    1. Initial program 51.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 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. lower-*.f6437.6

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

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

    if 1.75e8 < re

    1. Initial program 37.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. lower-sqrt.f6480.2

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Applied rewrites80.2%

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

Alternative 5: 30.9% accurate, 2.8× speedup?

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

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

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


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

    1. Initial program 36.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. lift-*.f64N/A

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im, im, re \cdot re\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. lower-neg.f6411.2

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

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

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

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

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

        \[\leadsto \color{blue}{{\left(2 \cdot \left(re + \left(\mathsf{neg}\left(re\right)\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
      5. lift-*.f64N/A

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

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

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

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

        \[\leadsto {\left(2 \cdot \color{blue}{0}\right)}^{\frac{1}{2}} \cdot \frac{1}{2} \]
      10. metadata-evalN/A

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

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

        \[\leadsto \color{blue}{0} \]
    9. Applied rewrites11.2%

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

    if -4.999999999999985e-310 < re

    1. Initial program 48.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 \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. lower-sqrt.f6454.9

        \[\leadsto \color{blue}{\sqrt{re}} \]
    5. Applied rewrites54.9%

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

Alternative 6: 6.0% 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 42.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. lift-*.f64N/A

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \frac{1}{2}} \]
    9. lower-*.f6442.7

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

    \[\leadsto \color{blue}{\sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im, im, re \cdot re\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. lower-neg.f646.9

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

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

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

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

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

      \[\leadsto \color{blue}{{\left(2 \cdot \left(re + \left(\mathsf{neg}\left(re\right)\right)\right)\right)}^{\frac{1}{2}}} \cdot \frac{1}{2} \]
    5. lift-*.f64N/A

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

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

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

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

      \[\leadsto {\left(2 \cdot \color{blue}{0}\right)}^{\frac{1}{2}} \cdot \frac{1}{2} \]
    10. metadata-evalN/A

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

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

      \[\leadsto \color{blue}{0} \]
  9. Applied rewrites6.9%

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

Developer Target 1: 48.8% 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 2024219 
(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)))))