math.sqrt on complex, imaginary part, im greater than 0 branch

Percentage Accurate: 40.5% → 89.8%
Time: 6.6s
Alternatives: 8
Speedup: 1.7×

Specification

?
\[im > 0\]
\[\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 8 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.5% 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.8% accurate, 0.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(im, re\right) - re\right) \cdot 2} \cdot 0.5\\


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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
      4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
      6. lower-*.f647.2

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

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

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

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

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

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

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

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

      \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. lower-*.f6411.8

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

      \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot 0.5 \]
    8. Taylor expanded in re around inf

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\color{blue}{\sqrt{2}} \cdot im\right)\right) \cdot 0.5 \]
    10. Applied rewrites94.3%

      \[\leadsto \color{blue}{\left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot im\right)\right)} \cdot 0.5 \]
    11. Step-by-step derivation
      1. Applied rewrites95.0%

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

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

      1. Initial program 43.9%

        \[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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
        2. *-commutativeN/A

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

          \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
        4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
        6. lower-*.f6443.9

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

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

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

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

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

          \[\leadsto \sqrt{\left(\sqrt{im \cdot im + \color{blue}{re \cdot re}} - re\right) \cdot 2} \cdot \frac{1}{2} \]
        12. lower-hypot.f6490.6

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

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

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

    Alternative 2: 75.7% accurate, 0.4× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\ \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{{re}^{-1}} \cdot im\right)\\ \end{array} \end{array} \]
    (FPCore (re im)
     :precision binary64
     (if (<= re -2.9e-58)
       (* (sqrt (* -4.0 re)) 0.5)
       (if (<= re 5.3e-51)
         (* 0.5 (sqrt (* 2.0 (- im re))))
         (* 0.5 (* (sqrt (pow re -1.0)) im)))))
    double code(double re, double im) {
    	double tmp;
    	if (re <= -2.9e-58) {
    		tmp = sqrt((-4.0 * re)) * 0.5;
    	} else if (re <= 5.3e-51) {
    		tmp = 0.5 * sqrt((2.0 * (im - re)));
    	} else {
    		tmp = 0.5 * (sqrt(pow(re, -1.0)) * im);
    	}
    	return tmp;
    }
    
    real(8) function code(re, im)
        real(8), intent (in) :: re
        real(8), intent (in) :: im
        real(8) :: tmp
        if (re <= (-2.9d-58)) then
            tmp = sqrt(((-4.0d0) * re)) * 0.5d0
        else if (re <= 5.3d-51) then
            tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
        else
            tmp = 0.5d0 * (sqrt((re ** (-1.0d0))) * im)
        end if
        code = tmp
    end function
    
    public static double code(double re, double im) {
    	double tmp;
    	if (re <= -2.9e-58) {
    		tmp = Math.sqrt((-4.0 * re)) * 0.5;
    	} else if (re <= 5.3e-51) {
    		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
    	} else {
    		tmp = 0.5 * (Math.sqrt(Math.pow(re, -1.0)) * im);
    	}
    	return tmp;
    }
    
    def code(re, im):
    	tmp = 0
    	if re <= -2.9e-58:
    		tmp = math.sqrt((-4.0 * re)) * 0.5
    	elif re <= 5.3e-51:
    		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
    	else:
    		tmp = 0.5 * (math.sqrt(math.pow(re, -1.0)) * im)
    	return tmp
    
    function code(re, im)
    	tmp = 0.0
    	if (re <= -2.9e-58)
    		tmp = Float64(sqrt(Float64(-4.0 * re)) * 0.5);
    	elseif (re <= 5.3e-51)
    		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
    	else
    		tmp = Float64(0.5 * Float64(sqrt((re ^ -1.0)) * im));
    	end
    	return tmp
    end
    
    function tmp_2 = code(re, im)
    	tmp = 0.0;
    	if (re <= -2.9e-58)
    		tmp = sqrt((-4.0 * re)) * 0.5;
    	elseif (re <= 5.3e-51)
    		tmp = 0.5 * sqrt((2.0 * (im - re)));
    	else
    		tmp = 0.5 * (sqrt((re ^ -1.0)) * im);
    	end
    	tmp_2 = tmp;
    end
    
    code[re_, im_] := If[LessEqual[re, -2.9e-58], N[(N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[re, 5.3e-51], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Sqrt[N[Power[re, -1.0], $MachinePrecision]], $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\
    \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\
    
    \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\
    \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;0.5 \cdot \left(\sqrt{{re}^{-1}} \cdot im\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if re < -2.8999999999999999e-58

      1. Initial program 45.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
        2. *-commutativeN/A

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

          \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
        4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
        6. lower-*.f6445.7

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

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

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

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

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

          \[\leadsto \sqrt{\left(\sqrt{im \cdot im + \color{blue}{re \cdot re}} - re\right) \cdot 2} \cdot \frac{1}{2} \]
        12. lower-hypot.f64100.0

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

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

        \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot \frac{1}{2} \]
      6. Step-by-step derivation
        1. lower-*.f6477.7

          \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot 0.5 \]
      7. Applied rewrites77.7%

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

      if -2.8999999999999999e-58 < re < 5.29999999999999974e-51

      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 0

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

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

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

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

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

      if 5.29999999999999974e-51 < re

      1. Initial program 15.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{2 \cdot \color{blue}{\left({im}^{2} \cdot \left(\frac{-1}{8} \cdot \frac{{im}^{2}}{{re}^{3}} + \frac{1}{2} \cdot \frac{1}{re}\right)\right)}} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

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

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

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

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{fma}\left(\frac{{im}^{2}}{{re}^{3}}, \frac{-1}{8}, \frac{1}{2} \cdot \frac{1}{re}\right)} \cdot {im}^{2}\right)} \]
        5. unpow2N/A

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\frac{\color{blue}{im \cdot im}}{{re}^{3}}, \frac{-1}{8}, \frac{1}{2} \cdot \frac{1}{re}\right) \cdot {im}^{2}\right)} \]
        6. associate-/l*N/A

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(\color{blue}{im \cdot \frac{im}{{re}^{3}}}, \frac{-1}{8}, \frac{1}{2} \cdot \frac{1}{re}\right) \cdot {im}^{2}\right)} \]
        7. lower-*.f64N/A

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

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \color{blue}{\frac{im}{{re}^{3}}}, \frac{-1}{8}, \frac{1}{2} \cdot \frac{1}{re}\right) \cdot {im}^{2}\right)} \]
        9. lower-pow.f64N/A

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{\color{blue}{{re}^{3}}}, \frac{-1}{8}, \frac{1}{2} \cdot \frac{1}{re}\right) \cdot {im}^{2}\right)} \]
        10. associate-*r/N/A

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

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\color{blue}{\frac{1}{2}}}{re}\right) \cdot {im}^{2}\right)} \]
        12. lower-/.f64N/A

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \color{blue}{\frac{\frac{1}{2}}{re}}\right) \cdot {im}^{2}\right)} \]
        13. unpow2N/A

          \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \color{blue}{\left(im \cdot im\right)}\right)} \]
        14. lower-*.f6443.4

          \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, -0.125, \frac{0.5}{re}\right) \cdot \color{blue}{\left(im \cdot im\right)}\right)} \]
      5. Applied rewrites43.4%

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, -0.125, \frac{0.5}{re}\right) \cdot \left(im \cdot im\right)\right)}} \]
      6. Step-by-step derivation
        1. lift-sqrt.f64N/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\sqrt{2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \left(im \cdot im\right)\right)}} \]
        2. pow1/2N/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{{\left(2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \left(im \cdot im\right)\right)\right)}^{\frac{1}{2}}} \]
        3. sqr-powN/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\left({\left(2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \left(im \cdot im\right)\right)\right)}^{\left(\frac{\frac{1}{2}}{2}\right)} \cdot {\left(2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \left(im \cdot im\right)\right)\right)}^{\left(\frac{\frac{1}{2}}{2}\right)}\right)} \]
        4. pow2N/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{{\left({\left(2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \left(im \cdot im\right)\right)\right)}^{\left(\frac{\frac{1}{2}}{2}\right)}\right)}^{2}} \]
        5. lower-pow.f64N/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{{\left({\left(2 \cdot \left(\mathsf{fma}\left(im \cdot \frac{im}{{re}^{3}}, \frac{-1}{8}, \frac{\frac{1}{2}}{re}\right) \cdot \left(im \cdot im\right)\right)\right)}^{\left(\frac{\frac{1}{2}}{2}\right)}\right)}^{2}} \]
      7. Applied rewrites46.8%

        \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\left(\left(\mathsf{fma}\left(-0.125, \frac{im}{{re}^{3}} \cdot im, \frac{0.5}{re}\right) \cdot im\right) \cdot im\right) \cdot 2\right)}^{0.25}\right)}^{2}} \]
      8. Taylor expanded in re around inf

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

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

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

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

          \[\leadsto 0.5 \cdot \left(\sqrt{\color{blue}{\frac{1}{re}}} \cdot im\right) \]
      10. Applied rewrites73.1%

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\ \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{{re}^{-1}} \cdot im\right)\\ \end{array} \]
    5. Add Preprocessing

    Alternative 3: 75.6% accurate, 0.4× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im} - re\\ \mathbf{if}\;t\_0 \leq 0:\\ \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\ \mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot t\_0}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \end{array} \end{array} \]
    (FPCore (re im)
     :precision binary64
     (let* ((t_0 (- (sqrt (+ (* re re) (* im im))) re)))
       (if (<= t_0 0.0)
         (* (/ im (sqrt re)) 0.5)
         (if (<= t_0 2e+154)
           (* 0.5 (sqrt (* 2.0 t_0)))
           (* 0.5 (sqrt (* 2.0 (- im re))))))))
    double code(double re, double im) {
    	double t_0 = sqrt(((re * re) + (im * im))) - re;
    	double tmp;
    	if (t_0 <= 0.0) {
    		tmp = (im / sqrt(re)) * 0.5;
    	} else if (t_0 <= 2e+154) {
    		tmp = 0.5 * sqrt((2.0 * t_0));
    	} else {
    		tmp = 0.5 * sqrt((2.0 * (im - 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))) - re
        if (t_0 <= 0.0d0) then
            tmp = (im / sqrt(re)) * 0.5d0
        else if (t_0 <= 2d+154) then
            tmp = 0.5d0 * sqrt((2.0d0 * t_0))
        else
            tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
        end if
        code = tmp
    end function
    
    public static double code(double re, double im) {
    	double t_0 = Math.sqrt(((re * re) + (im * im))) - re;
    	double tmp;
    	if (t_0 <= 0.0) {
    		tmp = (im / Math.sqrt(re)) * 0.5;
    	} else if (t_0 <= 2e+154) {
    		tmp = 0.5 * Math.sqrt((2.0 * t_0));
    	} else {
    		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
    	}
    	return tmp;
    }
    
    def code(re, im):
    	t_0 = math.sqrt(((re * re) + (im * im))) - re
    	tmp = 0
    	if t_0 <= 0.0:
    		tmp = (im / math.sqrt(re)) * 0.5
    	elif t_0 <= 2e+154:
    		tmp = 0.5 * math.sqrt((2.0 * t_0))
    	else:
    		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
    	return tmp
    
    function code(re, im)
    	t_0 = Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)
    	tmp = 0.0
    	if (t_0 <= 0.0)
    		tmp = Float64(Float64(im / sqrt(re)) * 0.5);
    	elseif (t_0 <= 2e+154)
    		tmp = Float64(0.5 * sqrt(Float64(2.0 * t_0)));
    	else
    		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
    	end
    	return tmp
    end
    
    function tmp_2 = code(re, im)
    	t_0 = sqrt(((re * re) + (im * im))) - re;
    	tmp = 0.0;
    	if (t_0 <= 0.0)
    		tmp = (im / sqrt(re)) * 0.5;
    	elseif (t_0 <= 2e+154)
    		tmp = 0.5 * sqrt((2.0 * t_0));
    	else
    		tmp = 0.5 * sqrt((2.0 * (im - re)));
    	end
    	tmp_2 = tmp;
    end
    
    code[re_, im_] := Block[{t$95$0 = N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]}, If[LessEqual[t$95$0, 0.0], N[(N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[t$95$0, 2e+154], N[(0.5 * N[Sqrt[N[(2.0 * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \sqrt{re \cdot re + im \cdot im} - re\\
    \mathbf{if}\;t\_0 \leq 0:\\
    \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\
    
    \mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+154}:\\
    \;\;\;\;0.5 \cdot \sqrt{2 \cdot t\_0}\\
    
    \mathbf{else}:\\
    \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

      1. Initial program 7.2%

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

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

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

          \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
        4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
        6. lower-*.f647.2

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot \frac{1}{2} \]
      6. Step-by-step derivation
        1. lower-*.f6411.8

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

        \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot 0.5 \]
      8. Taylor expanded in re around inf

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto \left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\color{blue}{\sqrt{2}} \cdot im\right)\right) \cdot 0.5 \]
      10. Applied rewrites94.3%

        \[\leadsto \color{blue}{\left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot im\right)\right)} \cdot 0.5 \]
      11. Step-by-step derivation
        1. Applied rewrites95.0%

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

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

        1. Initial program 93.0%

          \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
        2. Add Preprocessing

        if 2.00000000000000007e154 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

        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. Taylor expanded in re around 0

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

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

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

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

          \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(im - re\right)}} \]
      12. Recombined 3 regimes into one program.
      13. Final simplification77.5%

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

      Alternative 4: 75.6% accurate, 0.4× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im} - re\\ \mathbf{if}\;t\_0 \leq 0:\\ \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\ \mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+154}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(re, re, im \cdot im\right)} - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \end{array} \end{array} \]
      (FPCore (re im)
       :precision binary64
       (let* ((t_0 (- (sqrt (+ (* re re) (* im im))) re)))
         (if (<= t_0 0.0)
           (* (/ im (sqrt re)) 0.5)
           (if (<= t_0 2e+154)
             (* 0.5 (sqrt (* 2.0 (- (sqrt (fma re re (* im im))) re))))
             (* 0.5 (sqrt (* 2.0 (- im re))))))))
      double code(double re, double im) {
      	double t_0 = sqrt(((re * re) + (im * im))) - re;
      	double tmp;
      	if (t_0 <= 0.0) {
      		tmp = (im / sqrt(re)) * 0.5;
      	} else if (t_0 <= 2e+154) {
      		tmp = 0.5 * sqrt((2.0 * (sqrt(fma(re, re, (im * im))) - re)));
      	} else {
      		tmp = 0.5 * sqrt((2.0 * (im - re)));
      	}
      	return tmp;
      }
      
      function code(re, im)
      	t_0 = Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)
      	tmp = 0.0
      	if (t_0 <= 0.0)
      		tmp = Float64(Float64(im / sqrt(re)) * 0.5);
      	elseif (t_0 <= 2e+154)
      		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(fma(re, re, Float64(im * im))) - re))));
      	else
      		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
      	end
      	return tmp
      end
      
      code[re_, im_] := Block[{t$95$0 = N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]}, If[LessEqual[t$95$0, 0.0], N[(N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[t$95$0, 2e+154], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(re * re + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := \sqrt{re \cdot re + im \cdot im} - re\\
      \mathbf{if}\;t\_0 \leq 0:\\
      \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\
      
      \mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+154}:\\
      \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(re, re, im \cdot im\right)} - re\right)}\\
      
      \mathbf{else}:\\
      \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

        1. Initial program 7.2%

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

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

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

            \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
          4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
          6. lower-*.f647.2

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

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

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

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

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

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

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

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

          \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot \frac{1}{2} \]
        6. Step-by-step derivation
          1. lower-*.f6411.8

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

          \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot 0.5 \]
        8. Taylor expanded in re around inf

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

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

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\color{blue}{\sqrt{2}} \cdot im\right)\right) \cdot 0.5 \]
        10. Applied rewrites94.3%

          \[\leadsto \color{blue}{\left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot im\right)\right)} \cdot 0.5 \]
        11. Step-by-step derivation
          1. Applied rewrites95.0%

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

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

          1. Initial program 93.0%

            \[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{\color{blue}{re \cdot re} + im \cdot im} - re\right)} \]
            3. lower-fma.f6493.0

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

            \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\color{blue}{\mathsf{fma}\left(re, re, im \cdot im\right)}} - re\right)} \]

          if 2.00000000000000007e154 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

          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. Taylor expanded in re around 0

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

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

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

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

            \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(im - re\right)}} \]
        12. Recombined 3 regimes into one program.
        13. Final simplification77.5%

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

        Alternative 5: 75.7% accurate, 0.9× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\ \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\ \end{array} \end{array} \]
        (FPCore (re im)
         :precision binary64
         (if (<= re -2.9e-58)
           (* (sqrt (* -4.0 re)) 0.5)
           (if (<= re 5.3e-51)
             (* (sqrt (fma (- (/ re im) 2.0) re (* 2.0 im))) 0.5)
             (* (/ im (sqrt re)) 0.5))))
        double code(double re, double im) {
        	double tmp;
        	if (re <= -2.9e-58) {
        		tmp = sqrt((-4.0 * re)) * 0.5;
        	} else if (re <= 5.3e-51) {
        		tmp = sqrt(fma(((re / im) - 2.0), re, (2.0 * im))) * 0.5;
        	} else {
        		tmp = (im / sqrt(re)) * 0.5;
        	}
        	return tmp;
        }
        
        function code(re, im)
        	tmp = 0.0
        	if (re <= -2.9e-58)
        		tmp = Float64(sqrt(Float64(-4.0 * re)) * 0.5);
        	elseif (re <= 5.3e-51)
        		tmp = Float64(sqrt(fma(Float64(Float64(re / im) - 2.0), re, Float64(2.0 * im))) * 0.5);
        	else
        		tmp = Float64(Float64(im / sqrt(re)) * 0.5);
        	end
        	return tmp
        end
        
        code[re_, im_] := If[LessEqual[re, -2.9e-58], N[(N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[re, 5.3e-51], N[(N[Sqrt[N[(N[(N[(re / im), $MachinePrecision] - 2.0), $MachinePrecision] * re + N[(2.0 * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\
        \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\
        
        \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\
        \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot 0.5\\
        
        \mathbf{else}:\\
        \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if re < -2.8999999999999999e-58

          1. Initial program 45.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
            2. *-commutativeN/A

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

              \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
            4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
            6. lower-*.f6445.7

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

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

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

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

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

              \[\leadsto \sqrt{\left(\sqrt{im \cdot im + \color{blue}{re \cdot re}} - re\right) \cdot 2} \cdot \frac{1}{2} \]
            12. lower-hypot.f64100.0

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

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

            \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot \frac{1}{2} \]
          6. Step-by-step derivation
            1. lower-*.f6477.7

              \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot 0.5 \]
          7. Applied rewrites77.7%

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

          if -2.8999999999999999e-58 < re < 5.29999999999999974e-51

          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. Step-by-step derivation
            1. lift-*.f64N/A

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

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

              \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
            4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
            6. lower-*.f6448.8

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)}} \cdot 0.5 \]

          if 5.29999999999999974e-51 < re

          1. Initial program 15.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
            2. *-commutativeN/A

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

              \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
            4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
            6. lower-*.f6415.8

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

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

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

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

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

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

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

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

            \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot \frac{1}{2} \]
          6. Step-by-step derivation
            1. lower-*.f6430.8

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

            \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot 0.5 \]
          8. Taylor expanded in re around inf

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

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

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

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

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

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

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

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

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

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

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

              \[\leadsto \left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{\frac{1}{2}}\right) \cdot \color{blue}{\left(\sqrt{2} \cdot im\right)}\right) \cdot \frac{1}{2} \]
            12. lower-sqrt.f6472.7

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

            \[\leadsto \color{blue}{\left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot im\right)\right)} \cdot 0.5 \]
          11. Step-by-step derivation
            1. Applied rewrites73.2%

              \[\leadsto \frac{1 \cdot \left(1 \cdot im\right)}{\color{blue}{\sqrt{re}}} \cdot 0.5 \]
          12. Recombined 3 regimes into one program.
          13. Final simplification77.3%

            \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\ \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\ \end{array} \]
          14. Add Preprocessing

          Alternative 6: 75.8% accurate, 1.2× speedup?

          \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\ \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\ \end{array} \end{array} \]
          (FPCore (re im)
           :precision binary64
           (if (<= re -2.9e-58)
             (* (sqrt (* -4.0 re)) 0.5)
             (if (<= re 5.3e-51)
               (* 0.5 (sqrt (* 2.0 (- im re))))
               (* (/ im (sqrt re)) 0.5))))
          double code(double re, double im) {
          	double tmp;
          	if (re <= -2.9e-58) {
          		tmp = sqrt((-4.0 * re)) * 0.5;
          	} else if (re <= 5.3e-51) {
          		tmp = 0.5 * sqrt((2.0 * (im - re)));
          	} else {
          		tmp = (im / sqrt(re)) * 0.5;
          	}
          	return tmp;
          }
          
          real(8) function code(re, im)
              real(8), intent (in) :: re
              real(8), intent (in) :: im
              real(8) :: tmp
              if (re <= (-2.9d-58)) then
                  tmp = sqrt(((-4.0d0) * re)) * 0.5d0
              else if (re <= 5.3d-51) then
                  tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
              else
                  tmp = (im / sqrt(re)) * 0.5d0
              end if
              code = tmp
          end function
          
          public static double code(double re, double im) {
          	double tmp;
          	if (re <= -2.9e-58) {
          		tmp = Math.sqrt((-4.0 * re)) * 0.5;
          	} else if (re <= 5.3e-51) {
          		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
          	} else {
          		tmp = (im / Math.sqrt(re)) * 0.5;
          	}
          	return tmp;
          }
          
          def code(re, im):
          	tmp = 0
          	if re <= -2.9e-58:
          		tmp = math.sqrt((-4.0 * re)) * 0.5
          	elif re <= 5.3e-51:
          		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
          	else:
          		tmp = (im / math.sqrt(re)) * 0.5
          	return tmp
          
          function code(re, im)
          	tmp = 0.0
          	if (re <= -2.9e-58)
          		tmp = Float64(sqrt(Float64(-4.0 * re)) * 0.5);
          	elseif (re <= 5.3e-51)
          		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
          	else
          		tmp = Float64(Float64(im / sqrt(re)) * 0.5);
          	end
          	return tmp
          end
          
          function tmp_2 = code(re, im)
          	tmp = 0.0;
          	if (re <= -2.9e-58)
          		tmp = sqrt((-4.0 * re)) * 0.5;
          	elseif (re <= 5.3e-51)
          		tmp = 0.5 * sqrt((2.0 * (im - re)));
          	else
          		tmp = (im / sqrt(re)) * 0.5;
          	end
          	tmp_2 = tmp;
          end
          
          code[re_, im_] := If[LessEqual[re, -2.9e-58], N[(N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[re, 5.3e-51], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]]]
          
          \begin{array}{l}
          
          \\
          \begin{array}{l}
          \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\
          \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\
          
          \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\
          \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
          
          \mathbf{else}:\\
          \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if re < -2.8999999999999999e-58

            1. Initial program 45.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
              2. *-commutativeN/A

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

                \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
              4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
              6. lower-*.f6445.7

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

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

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

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

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

                \[\leadsto \sqrt{\left(\sqrt{im \cdot im + \color{blue}{re \cdot re}} - re\right) \cdot 2} \cdot \frac{1}{2} \]
              12. lower-hypot.f64100.0

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

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

              \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot \frac{1}{2} \]
            6. Step-by-step derivation
              1. lower-*.f6477.7

                \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot 0.5 \]
            7. Applied rewrites77.7%

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

            if -2.8999999999999999e-58 < re < 5.29999999999999974e-51

            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 0

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

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

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

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

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

            if 5.29999999999999974e-51 < re

            1. Initial program 15.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
              2. *-commutativeN/A

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

                \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
              4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
              6. lower-*.f6415.8

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

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

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

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

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

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

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

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

              \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot \frac{1}{2} \]
            6. Step-by-step derivation
              1. lower-*.f6430.8

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

              \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot 0.5 \]
            8. Taylor expanded in re around inf

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{\frac{1}{2}}\right) \cdot \color{blue}{\left(\sqrt{2} \cdot im\right)}\right) \cdot \frac{1}{2} \]
              12. lower-sqrt.f6472.7

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

              \[\leadsto \color{blue}{\left(\left(\sqrt{\frac{1}{re}} \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot im\right)\right)} \cdot 0.5 \]
            11. Step-by-step derivation
              1. Applied rewrites73.2%

                \[\leadsto \frac{1 \cdot \left(1 \cdot im\right)}{\color{blue}{\sqrt{re}}} \cdot 0.5 \]
            12. Recombined 3 regimes into one program.
            13. Final simplification77.3%

              \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.9 \cdot 10^{-58}:\\ \;\;\;\;\sqrt{-4 \cdot re} \cdot 0.5\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im}{\sqrt{re}} \cdot 0.5\\ \end{array} \]
            14. Add Preprocessing

            Alternative 7: 63.8% accurate, 1.7× speedup?

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

              1. Initial program 45.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
                2. *-commutativeN/A

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

                  \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
                4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
                6. lower-*.f6445.7

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

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

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

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

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

                  \[\leadsto \sqrt{\left(\sqrt{im \cdot im + \color{blue}{re \cdot re}} - re\right) \cdot 2} \cdot \frac{1}{2} \]
                12. lower-hypot.f64100.0

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

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

                \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot \frac{1}{2} \]
              6. Step-by-step derivation
                1. lower-*.f6477.7

                  \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot 0.5 \]
              7. Applied rewrites77.7%

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

              if -1.8500000000000001e-58 < re

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

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

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

                  \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
                4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
                6. lower-*.f6434.5

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

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

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

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

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

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

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

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

                \[\leadsto \sqrt{\color{blue}{2 \cdot im}} \cdot \frac{1}{2} \]
              6. Step-by-step derivation
                1. lower-*.f6458.3

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

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

            Alternative 8: 26.6% accurate, 2.2× speedup?

            \[\begin{array}{l} \\ \sqrt{-4 \cdot re} \cdot 0.5 \end{array} \]
            (FPCore (re im) :precision binary64 (* (sqrt (* -4.0 re)) 0.5))
            double code(double re, double im) {
            	return sqrt((-4.0 * re)) * 0.5;
            }
            
            real(8) function code(re, im)
                real(8), intent (in) :: re
                real(8), intent (in) :: im
                code = sqrt(((-4.0d0) * re)) * 0.5d0
            end function
            
            public static double code(double re, double im) {
            	return Math.sqrt((-4.0 * re)) * 0.5;
            }
            
            def code(re, im):
            	return math.sqrt((-4.0 * re)) * 0.5
            
            function code(re, im)
            	return Float64(sqrt(Float64(-4.0 * re)) * 0.5)
            end
            
            function tmp = code(re, im)
            	tmp = sqrt((-4.0 * re)) * 0.5;
            end
            
            code[re_, im_] := N[(N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \sqrt{-4 \cdot re} \cdot 0.5
            \end{array}
            
            Derivation
            1. Initial program 38.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 \color{blue}{\frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}} \]
              2. *-commutativeN/A

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

                \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot 0.5} \]
              4. lift-*.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{\color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right) \cdot 2}} \cdot \frac{1}{2} \]
              6. lower-*.f6438.1

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

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

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

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

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

                \[\leadsto \sqrt{\left(\sqrt{im \cdot im + \color{blue}{re \cdot re}} - re\right) \cdot 2} \cdot \frac{1}{2} \]
              12. lower-hypot.f6478.4

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

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

              \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot \frac{1}{2} \]
            6. Step-by-step derivation
              1. lower-*.f6430.0

                \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot 0.5 \]
            7. Applied rewrites30.0%

              \[\leadsto \sqrt{\color{blue}{-4 \cdot re}} \cdot 0.5 \]
            8. Add Preprocessing

            Reproduce

            ?
            herbie shell --seed 2024323 
            (FPCore (re im)
              :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
              :precision binary64
              :pre (> im 0.0)
              (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))