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

Percentage Accurate: 40.6% → 90.1%
Time: 4.2s
Alternatives: 9
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)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    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}

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 9 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.6% 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)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

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

Alternative 1: 90.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right) \leq 0:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \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 (<= (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)) 0.0)
   (* (pow re -0.5) (* im 0.5))
   (* (sqrt (* (- (hypot im re) re) 2.0)) 0.5)))
double code(double re, double im) {
	double tmp;
	if ((2.0 * (sqrt(((re * re) + (im * im))) - re)) <= 0.0) {
		tmp = pow(re, -0.5) * (im * 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 ((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)) <= 0.0) {
		tmp = Math.pow(re, -0.5) * (im * 0.5);
	} else {
		tmp = Math.sqrt(((Math.hypot(im, re) - re) * 2.0)) * 0.5;
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (2.0 * (math.sqrt(((re * re) + (im * im))) - re)) <= 0.0:
		tmp = math.pow(re, -0.5) * (im * 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(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)) <= 0.0)
		tmp = Float64((re ^ -0.5) * Float64(im * 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 ((2.0 * (sqrt(((re * re) + (im * im))) - re)) <= 0.0)
		tmp = (re ^ -0.5) * (im * 0.5);
	else
		tmp = sqrt(((hypot(im, re) - re) * 2.0)) * 0.5;
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $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}\;2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right) \leq 0:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\

\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 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)) < 0.0

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{{re}^{-1}} \]
      10. sqrt-pow1N/A

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\frac{-1}{2}} \]
      12. lower-pow.f6492.8

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{-0.5}} \]
    5. Applied rewrites92.8%

      \[\leadsto \color{blue}{\left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{-0.5}} \]
    6. Step-by-step derivation
      1. lift-pow.f64N/A

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

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

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

        \[\leadsto {re}^{\frac{-1}{2}} \cdot \color{blue}{\left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right)} \]
      5. lift-pow.f6492.8

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

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

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

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

        \[\leadsto {re}^{\frac{-1}{2}} \cdot \left(im \cdot \color{blue}{\frac{1}{2}}\right) \]
      10. lower-*.f6492.8

        \[\leadsto {re}^{-0.5} \cdot \left(im \cdot \color{blue}{0.5}\right) \]
    7. Applied rewrites92.8%

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

    if 0.0 < (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
    4. Applied rewrites89.0%

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

Alternative 2: 79.0% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -4 \cdot 10^{+149}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq -4.9 \cdot 10^{-82}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)} - re\right)}\\ \mathbf{elif}\;re \leq 950000000:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -4e+149)
   (* 0.5 (sqrt (* -4.0 re)))
   (if (<= re -4.9e-82)
     (* 0.5 (sqrt (* 2.0 (- (sqrt (fma im im (* re re))) re))))
     (if (<= re 950000000.0)
       (* (sqrt (fma (- (/ re im) 2.0) re (+ im im))) 0.5)
       (* (pow re -0.5) (* im 0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -4e+149) {
		tmp = 0.5 * sqrt((-4.0 * re));
	} else if (re <= -4.9e-82) {
		tmp = 0.5 * sqrt((2.0 * (sqrt(fma(im, im, (re * re))) - re)));
	} else if (re <= 950000000.0) {
		tmp = sqrt(fma(((re / im) - 2.0), re, (im + im))) * 0.5;
	} else {
		tmp = pow(re, -0.5) * (im * 0.5);
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (re <= -4e+149)
		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
	elseif (re <= -4.9e-82)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(fma(im, im, Float64(re * re))) - re))));
	elseif (re <= 950000000.0)
		tmp = Float64(sqrt(fma(Float64(Float64(re / im) - 2.0), re, Float64(im + im))) * 0.5);
	else
		tmp = Float64((re ^ -0.5) * Float64(im * 0.5));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[re, -4e+149], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -4.9e-82], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(im * im + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 950000000.0], N[(N[Sqrt[N[(N[(N[(re / im), $MachinePrecision] - 2.0), $MachinePrecision] * re + N[(im + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{+149}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\

\mathbf{elif}\;re \leq -4.9 \cdot 10^{-82}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)} - re\right)}\\

\mathbf{elif}\;re \leq 950000000:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -4.0000000000000002e149

    1. Initial program 4.2%

      \[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}{-4 \cdot re}} \]
    4. Step-by-step derivation
      1. lower-*.f6486.8

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    5. Applied rewrites86.8%

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

    if -4.0000000000000002e149 < re < -4.9000000000000003e-82

    1. Initial program 85.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{\color{blue}{re \cdot re} + im \cdot im} - re\right)} \]
      3. lift-*.f64N/A

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

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

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(im, im, \color{blue}{re \cdot re}\right)} - re\right)} \]
      10. lift-*.f6485.8

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

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

    if -4.9000000000000003e-82 < re < 9.5e8

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
    4. Applied rewrites84.5%

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

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

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

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

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

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
      7. lower-*.f6481.0

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

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

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
      2. count-2-revN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot \frac{1}{2} \]
      3. lower-+.f6481.0

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5 \]
    9. Applied rewrites81.0%

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

    if 9.5e8 < re

    1. Initial program 10.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(\left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    4. Step-by-step derivation
      1. associate-*r*N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{{re}^{-1}} \]
      10. sqrt-pow1N/A

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\frac{-1}{2}} \]
      12. lower-pow.f6482.1

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{-0.5}} \]
    5. Applied rewrites82.1%

      \[\leadsto \color{blue}{\left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{-0.5}} \]
    6. Step-by-step derivation
      1. lift-pow.f64N/A

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

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

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

        \[\leadsto {re}^{\frac{-1}{2}} \cdot \color{blue}{\left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right)} \]
      5. lift-pow.f6482.1

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

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

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

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

        \[\leadsto {re}^{\frac{-1}{2}} \cdot \left(im \cdot \color{blue}{\frac{1}{2}}\right) \]
      10. lower-*.f6482.1

        \[\leadsto {re}^{-0.5} \cdot \left(im \cdot \color{blue}{0.5}\right) \]
    7. Applied rewrites82.1%

      \[\leadsto \color{blue}{{re}^{-0.5} \cdot \left(im \cdot 0.5\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification82.8%

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

Alternative 3: 78.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -4 \cdot 10^{+149}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq -4.9 \cdot 10^{-82}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)} - re\right)}\\ \mathbf{elif}\;re \leq 950000000:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -4e+149)
   (* 0.5 (sqrt (* -4.0 re)))
   (if (<= re -4.9e-82)
     (* 0.5 (sqrt (* 2.0 (- (sqrt (fma im im (* re re))) re))))
     (if (<= re 950000000.0)
       (* (sqrt (fma (- (/ re im) 2.0) re (+ im im))) 0.5)
       (* (* 0.5 im) (/ 1.0 (sqrt re)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -4e+149) {
		tmp = 0.5 * sqrt((-4.0 * re));
	} else if (re <= -4.9e-82) {
		tmp = 0.5 * sqrt((2.0 * (sqrt(fma(im, im, (re * re))) - re)));
	} else if (re <= 950000000.0) {
		tmp = sqrt(fma(((re / im) - 2.0), re, (im + im))) * 0.5;
	} else {
		tmp = (0.5 * im) * (1.0 / sqrt(re));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (re <= -4e+149)
		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
	elseif (re <= -4.9e-82)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(fma(im, im, Float64(re * re))) - re))));
	elseif (re <= 950000000.0)
		tmp = Float64(sqrt(fma(Float64(Float64(re / im) - 2.0), re, Float64(im + im))) * 0.5);
	else
		tmp = Float64(Float64(0.5 * im) * Float64(1.0 / sqrt(re)));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[re, -4e+149], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -4.9e-82], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(im * im + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 950000000.0], N[(N[Sqrt[N[(N[(N[(re / im), $MachinePrecision] - 2.0), $MachinePrecision] * re + N[(im + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] * N[(1.0 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{+149}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\

\mathbf{elif}\;re \leq -4.9 \cdot 10^{-82}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)} - re\right)}\\

\mathbf{elif}\;re \leq 950000000:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if re < -4.0000000000000002e149

    1. Initial program 4.2%

      \[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}{-4 \cdot re}} \]
    4. Step-by-step derivation
      1. lower-*.f6486.8

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    5. Applied rewrites86.8%

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

    if -4.0000000000000002e149 < re < -4.9000000000000003e-82

    1. Initial program 85.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{\color{blue}{re \cdot re} + im \cdot im} - re\right)} \]
      3. lift-*.f64N/A

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

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

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

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

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

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

        \[\leadsto \frac{1}{2} \cdot \sqrt{2 \cdot \left(\sqrt{\mathsf{fma}\left(im, im, \color{blue}{re \cdot re}\right)} - re\right)} \]
      10. lift-*.f6485.8

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

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

    if -4.9000000000000003e-82 < re < 9.5e8

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
    4. Applied rewrites84.5%

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

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

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

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

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

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
      7. lower-*.f6481.0

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

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

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
      2. count-2-revN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot \frac{1}{2} \]
      3. lower-+.f6481.0

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5 \]
    9. Applied rewrites81.0%

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

    if 9.5e8 < re

    1. Initial program 10.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(\left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    4. Step-by-step derivation
      1. associate-*r*N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{{re}^{-1}} \]
      10. sqrt-pow1N/A

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\frac{-1}{2}} \]
      12. lower-pow.f6482.1

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{-0.5}} \]
    5. Applied rewrites82.1%

      \[\leadsto \color{blue}{\left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{-0.5}} \]
    6. Taylor expanded in re around 0

      \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}} \]
    7. Step-by-step derivation
      1. sqrt-divN/A

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}} \]
      4. lower-sqrt.f6482.0

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}} \]
    8. Applied rewrites82.0%

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

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

Alternative 4: 76.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 950000000:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -6.2e-15)
   (* 0.5 (sqrt (* -4.0 re)))
   (if (<= re 950000000.0)
     (* (sqrt (fma (- (/ re im) 2.0) re (+ im im))) 0.5)
     (* (* 0.5 im) (/ 1.0 (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -6.2e-15) {
		tmp = 0.5 * sqrt((-4.0 * re));
	} else if (re <= 950000000.0) {
		tmp = sqrt(fma(((re / im) - 2.0), re, (im + im))) * 0.5;
	} else {
		tmp = (0.5 * im) * (1.0 / sqrt(re));
	}
	return tmp;
}
function code(re, im)
	tmp = 0.0
	if (re <= -6.2e-15)
		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
	elseif (re <= 950000000.0)
		tmp = Float64(sqrt(fma(Float64(Float64(re / im) - 2.0), re, Float64(im + im))) * 0.5);
	else
		tmp = Float64(Float64(0.5 * im) * Float64(1.0 / sqrt(re)));
	end
	return tmp
end
code[re_, im_] := If[LessEqual[re, -6.2e-15], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 950000000.0], N[(N[Sqrt[N[(N[(N[(re / im), $MachinePrecision] - 2.0), $MachinePrecision] * re + N[(im + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] * N[(1.0 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\

\mathbf{elif}\;re \leq 950000000:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5\\

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\


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

    1. Initial program 54.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 -inf

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    5. Applied rewrites81.3%

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

    if -6.1999999999999998e-15 < re < 9.5e8

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
    4. Applied rewrites86.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 + re \cdot \left(\frac{re}{im} - 2\right)}} \cdot \frac{1}{2} \]
    6. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
      2. count-2-revN/A

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot \frac{1}{2} \]
      3. lower-+.f6478.6

        \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5 \]
    9. Applied rewrites78.6%

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

    if 9.5e8 < re

    1. Initial program 10.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(\left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
    4. Step-by-step derivation
      1. associate-*r*N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{{re}^{-1}} \]
      10. sqrt-pow1N/A

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\frac{-1}{2}} \]
      12. lower-pow.f6482.1

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{-0.5}} \]
    5. Applied rewrites82.1%

      \[\leadsto \color{blue}{\left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{-0.5}} \]
    6. Taylor expanded in re around 0

      \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}} \]
    7. Step-by-step derivation
      1. sqrt-divN/A

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

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}} \]
      4. lower-sqrt.f6482.0

        \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}} \]
    8. Applied rewrites82.0%

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

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

Alternative 5: 76.6% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 2800:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -6.2e-15)
   (* 0.5 (sqrt (* -4.0 re)))
   (if (<= re 2800.0)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (* (* 0.5 im) (/ 1.0 (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -6.2e-15) {
		tmp = 0.5 * sqrt((-4.0 * re));
	} else if (re <= 2800.0) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else {
		tmp = (0.5 * im) * (1.0 / sqrt(re));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-6.2d-15)) then
        tmp = 0.5d0 * sqrt(((-4.0d0) * re))
    else if (re <= 2800.0d0) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else
        tmp = (0.5d0 * im) * (1.0d0 / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -6.2e-15) {
		tmp = 0.5 * Math.sqrt((-4.0 * re));
	} else if (re <= 2800.0) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else {
		tmp = (0.5 * im) * (1.0 / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -6.2e-15:
		tmp = 0.5 * math.sqrt((-4.0 * re))
	elif re <= 2800.0:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	else:
		tmp = (0.5 * im) * (1.0 / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -6.2e-15)
		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
	elseif (re <= 2800.0)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	else
		tmp = Float64(Float64(0.5 * im) * Float64(1.0 / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -6.2e-15)
		tmp = 0.5 * sqrt((-4.0 * re));
	elseif (re <= 2800.0)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	else
		tmp = (0.5 * im) * (1.0 / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -6.2e-15], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2800.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] * N[(1.0 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\

\mathbf{elif}\;re \leq 2800:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\


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

    1. Initial program 54.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 -inf

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

        \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
    5. Applied rewrites81.3%

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

    if -6.1999999999999998e-15 < re < 2800

    1. Initial program 56.1%

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

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

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

      if 2800 < re

      1. Initial program 10.5%

        \[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(\left(im \cdot \left(\sqrt{\frac{1}{2}} \cdot \sqrt{2}\right)\right) \cdot \sqrt{\frac{1}{re}}\right)} \]
      4. Step-by-step derivation
        1. associate-*r*N/A

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

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

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

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

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

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

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

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

          \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{{re}^{-1}} \]
        10. sqrt-pow1N/A

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

          \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\frac{-1}{2}} \]
        12. lower-pow.f6481.0

          \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{\color{blue}{-0.5}} \]
      5. Applied rewrites81.0%

        \[\leadsto \color{blue}{\left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot {re}^{-0.5}} \]
      6. Taylor expanded in re around 0

        \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}} \]
      7. Step-by-step derivation
        1. sqrt-divN/A

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

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

          \[\leadsto \left(\frac{1}{2} \cdot \left(1 \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}} \]
        4. lower-sqrt.f6481.0

          \[\leadsto \left(0.5 \cdot \left(1 \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}} \]
      8. Applied rewrites81.0%

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 2800:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot \frac{1}{\sqrt{re}}\\ \end{array} \]
    7. Add Preprocessing

    Alternative 6: 76.6% accurate, 1.1× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 2800:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{1}{\sqrt{re}} \cdot im\right) \cdot 0.5\\ \end{array} \end{array} \]
    (FPCore (re im)
     :precision binary64
     (if (<= re -6.2e-15)
       (* 0.5 (sqrt (* -4.0 re)))
       (if (<= re 2800.0)
         (* 0.5 (sqrt (* 2.0 (- im re))))
         (* (* (/ 1.0 (sqrt re)) im) 0.5))))
    double code(double re, double im) {
    	double tmp;
    	if (re <= -6.2e-15) {
    		tmp = 0.5 * sqrt((-4.0 * re));
    	} else if (re <= 2800.0) {
    		tmp = 0.5 * sqrt((2.0 * (im - re)));
    	} else {
    		tmp = ((1.0 / sqrt(re)) * im) * 0.5;
    	}
    	return tmp;
    }
    
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(re, im)
    use fmin_fmax_functions
        real(8), intent (in) :: re
        real(8), intent (in) :: im
        real(8) :: tmp
        if (re <= (-6.2d-15)) then
            tmp = 0.5d0 * sqrt(((-4.0d0) * re))
        else if (re <= 2800.0d0) then
            tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
        else
            tmp = ((1.0d0 / sqrt(re)) * im) * 0.5d0
        end if
        code = tmp
    end function
    
    public static double code(double re, double im) {
    	double tmp;
    	if (re <= -6.2e-15) {
    		tmp = 0.5 * Math.sqrt((-4.0 * re));
    	} else if (re <= 2800.0) {
    		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
    	} else {
    		tmp = ((1.0 / Math.sqrt(re)) * im) * 0.5;
    	}
    	return tmp;
    }
    
    def code(re, im):
    	tmp = 0
    	if re <= -6.2e-15:
    		tmp = 0.5 * math.sqrt((-4.0 * re))
    	elif re <= 2800.0:
    		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
    	else:
    		tmp = ((1.0 / math.sqrt(re)) * im) * 0.5
    	return tmp
    
    function code(re, im)
    	tmp = 0.0
    	if (re <= -6.2e-15)
    		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
    	elseif (re <= 2800.0)
    		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
    	else
    		tmp = Float64(Float64(Float64(1.0 / sqrt(re)) * im) * 0.5);
    	end
    	return tmp
    end
    
    function tmp_2 = code(re, im)
    	tmp = 0.0;
    	if (re <= -6.2e-15)
    		tmp = 0.5 * sqrt((-4.0 * re));
    	elseif (re <= 2800.0)
    		tmp = 0.5 * sqrt((2.0 * (im - re)));
    	else
    		tmp = ((1.0 / sqrt(re)) * im) * 0.5;
    	end
    	tmp_2 = tmp;
    end
    
    code[re_, im_] := If[LessEqual[re, -6.2e-15], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2800.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(N[(1.0 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision] * im), $MachinePrecision] * 0.5), $MachinePrecision]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\
    \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\
    
    \mathbf{elif}\;re \leq 2800:\\
    \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;\left(\frac{1}{\sqrt{re}} \cdot im\right) \cdot 0.5\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if re < -6.1999999999999998e-15

      1. Initial program 54.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 -inf

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

          \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
      5. Applied rewrites81.3%

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

      if -6.1999999999999998e-15 < re < 2800

      1. Initial program 56.1%

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

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

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

        if 2800 < re

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

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

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

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

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

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

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

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

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

            \[\leadsto \color{blue}{\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \cdot \frac{1}{2}} \]
        4. Applied rewrites33.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 + re \cdot \left(\frac{re}{im} - 2\right)}} \cdot \frac{1}{2} \]
        6. Step-by-step derivation
          1. *-commutativeN/A

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

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

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

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

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

            \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
          7. lower-*.f6421.9

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

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

            \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, 2 \cdot im\right)} \cdot \frac{1}{2} \]
          2. count-2-revN/A

            \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot \frac{1}{2} \]
          3. lower-+.f6421.9

            \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5 \]
        9. Applied rewrites21.9%

          \[\leadsto \sqrt{\mathsf{fma}\left(\frac{re}{im} - 2, re, im + im\right)} \cdot 0.5 \]
        10. 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} \]
        11. Step-by-step derivation
          1. *-commutativeN/A

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

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

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

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

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

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

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

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

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

            \[\leadsto \left(\frac{1}{\sqrt{re}} \cdot im\right) \cdot \frac{1}{2} \]
          11. lower-sqrt.f6480.9

            \[\leadsto \left(\frac{1}{\sqrt{re}} \cdot im\right) \cdot 0.5 \]
        12. Applied rewrites80.9%

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

        \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 2800:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{1}{\sqrt{re}} \cdot im\right) \cdot 0.5\\ \end{array} \]
      7. Add Preprocessing

      Alternative 7: 70.9% accurate, 1.1× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 67000000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{re}}\\ \end{array} \end{array} \]
      (FPCore (re im)
       :precision binary64
       (if (<= re -6.2e-15)
         (* 0.5 (sqrt (* -4.0 re)))
         (if (<= re 67000000.0)
           (* 0.5 (sqrt (* 2.0 (- im re))))
           (* 0.5 (sqrt (* im (/ im re)))))))
      double code(double re, double im) {
      	double tmp;
      	if (re <= -6.2e-15) {
      		tmp = 0.5 * sqrt((-4.0 * re));
      	} else if (re <= 67000000.0) {
      		tmp = 0.5 * sqrt((2.0 * (im - re)));
      	} else {
      		tmp = 0.5 * sqrt((im * (im / re)));
      	}
      	return tmp;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      real(8) function code(re, im)
      use fmin_fmax_functions
          real(8), intent (in) :: re
          real(8), intent (in) :: im
          real(8) :: tmp
          if (re <= (-6.2d-15)) then
              tmp = 0.5d0 * sqrt(((-4.0d0) * re))
          else if (re <= 67000000.0d0) then
              tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
          else
              tmp = 0.5d0 * sqrt((im * (im / re)))
          end if
          code = tmp
      end function
      
      public static double code(double re, double im) {
      	double tmp;
      	if (re <= -6.2e-15) {
      		tmp = 0.5 * Math.sqrt((-4.0 * re));
      	} else if (re <= 67000000.0) {
      		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
      	} else {
      		tmp = 0.5 * Math.sqrt((im * (im / re)));
      	}
      	return tmp;
      }
      
      def code(re, im):
      	tmp = 0
      	if re <= -6.2e-15:
      		tmp = 0.5 * math.sqrt((-4.0 * re))
      	elif re <= 67000000.0:
      		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
      	else:
      		tmp = 0.5 * math.sqrt((im * (im / re)))
      	return tmp
      
      function code(re, im)
      	tmp = 0.0
      	if (re <= -6.2e-15)
      		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
      	elseif (re <= 67000000.0)
      		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
      	else
      		tmp = Float64(0.5 * sqrt(Float64(im * Float64(im / re))));
      	end
      	return tmp
      end
      
      function tmp_2 = code(re, im)
      	tmp = 0.0;
      	if (re <= -6.2e-15)
      		tmp = 0.5 * sqrt((-4.0 * re));
      	elseif (re <= 67000000.0)
      		tmp = 0.5 * sqrt((2.0 * (im - re)));
      	else
      		tmp = 0.5 * sqrt((im * (im / re)));
      	end
      	tmp_2 = tmp;
      end
      
      code[re_, im_] := If[LessEqual[re, -6.2e-15], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 67000000.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\
      \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\
      
      \mathbf{elif}\;re \leq 67000000:\\
      \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
      
      \mathbf{else}:\\
      \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{re}}\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 3 regimes
      2. if re < -6.1999999999999998e-15

        1. Initial program 54.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 -inf

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

            \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
        5. Applied rewrites81.3%

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

        if -6.1999999999999998e-15 < re < 6.7e7

        1. Initial program 56.1%

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

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

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

          if 6.7e7 < re

          1. Initial program 10.5%

            \[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}{\frac{{im}^{2}}{re}}} \]
          4. Step-by-step derivation
            1. lower-/.f64N/A

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

              \[\leadsto \frac{1}{2} \cdot \sqrt{\frac{im \cdot im}{re}} \]
            3. lift-*.f6449.2

              \[\leadsto 0.5 \cdot \sqrt{\frac{im \cdot im}{re}} \]
          5. Applied rewrites49.2%

            \[\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{\frac{im \cdot im}{re}} \]
            2. lift-/.f64N/A

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

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

              \[\leadsto \frac{1}{2} \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
            5. lower-/.f6460.5

              \[\leadsto 0.5 \cdot \sqrt{im \cdot \frac{im}{\color{blue}{re}}} \]
          7. Applied rewrites60.5%

            \[\leadsto 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{im}{re}}} \]
        5. Recombined 3 regimes into one program.
        6. Final simplification75.4%

          \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{elif}\;re \leq 67000000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{re}}\\ \end{array} \]
        7. Add Preprocessing

        Alternative 8: 64.5% accurate, 1.7× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
        (FPCore (re im)
         :precision binary64
         (if (<= re -6.2e-15) (* 0.5 (sqrt (* -4.0 re))) (* 0.5 (sqrt (* 2.0 im)))))
        double code(double re, double im) {
        	double tmp;
        	if (re <= -6.2e-15) {
        		tmp = 0.5 * sqrt((-4.0 * re));
        	} else {
        		tmp = 0.5 * sqrt((2.0 * im));
        	}
        	return tmp;
        }
        
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(re, im)
        use fmin_fmax_functions
            real(8), intent (in) :: re
            real(8), intent (in) :: im
            real(8) :: tmp
            if (re <= (-6.2d-15)) then
                tmp = 0.5d0 * sqrt(((-4.0d0) * re))
            else
                tmp = 0.5d0 * sqrt((2.0d0 * im))
            end if
            code = tmp
        end function
        
        public static double code(double re, double im) {
        	double tmp;
        	if (re <= -6.2e-15) {
        		tmp = 0.5 * Math.sqrt((-4.0 * re));
        	} else {
        		tmp = 0.5 * Math.sqrt((2.0 * im));
        	}
        	return tmp;
        }
        
        def code(re, im):
        	tmp = 0
        	if re <= -6.2e-15:
        		tmp = 0.5 * math.sqrt((-4.0 * re))
        	else:
        		tmp = 0.5 * math.sqrt((2.0 * im))
        	return tmp
        
        function code(re, im)
        	tmp = 0.0
        	if (re <= -6.2e-15)
        		tmp = Float64(0.5 * sqrt(Float64(-4.0 * re)));
        	else
        		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
        	end
        	return tmp
        end
        
        function tmp_2 = code(re, im)
        	tmp = 0.0;
        	if (re <= -6.2e-15)
        		tmp = 0.5 * sqrt((-4.0 * re));
        	else
        		tmp = 0.5 * sqrt((2.0 * im));
        	end
        	tmp_2 = tmp;
        end
        
        code[re_, im_] := If[LessEqual[re, -6.2e-15], N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        \mathbf{if}\;re \leq -6.2 \cdot 10^{-15}:\\
        \;\;\;\;0.5 \cdot \sqrt{-4 \cdot re}\\
        
        \mathbf{else}:\\
        \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if re < -6.1999999999999998e-15

          1. Initial program 54.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 -inf

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

              \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
          5. Applied rewrites81.3%

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

          if -6.1999999999999998e-15 < re

          1. Initial program 42.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}{im}} \]
          4. Step-by-step derivation
            1. Applied rewrites62.4%

              \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{im}} \]
          5. Recombined 2 regimes into one program.
          6. Final simplification66.9%

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

          Alternative 9: 25.9% accurate, 2.2× speedup?

          \[\begin{array}{l} \\ 0.5 \cdot \sqrt{-4 \cdot re} \end{array} \]
          (FPCore (re im) :precision binary64 (* 0.5 (sqrt (* -4.0 re))))
          double code(double re, double im) {
          	return 0.5 * sqrt((-4.0 * re));
          }
          
          module fmin_fmax_functions
              implicit none
              private
              public fmax
              public fmin
          
              interface fmax
                  module procedure fmax88
                  module procedure fmax44
                  module procedure fmax84
                  module procedure fmax48
              end interface
              interface fmin
                  module procedure fmin88
                  module procedure fmin44
                  module procedure fmin84
                  module procedure fmin48
              end interface
          contains
              real(8) function fmax88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(4) function fmax44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
              end function
              real(8) function fmax84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmax48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
              end function
              real(8) function fmin88(x, y) result (res)
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(4) function fmin44(x, y) result (res)
                  real(4), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
              end function
              real(8) function fmin84(x, y) result(res)
                  real(8), intent (in) :: x
                  real(4), intent (in) :: y
                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
              end function
              real(8) function fmin48(x, y) result(res)
                  real(4), intent (in) :: x
                  real(8), intent (in) :: y
                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
              end function
          end module
          
          real(8) function code(re, im)
          use fmin_fmax_functions
              real(8), intent (in) :: re
              real(8), intent (in) :: im
              code = 0.5d0 * sqrt(((-4.0d0) * re))
          end function
          
          public static double code(double re, double im) {
          	return 0.5 * Math.sqrt((-4.0 * re));
          }
          
          def code(re, im):
          	return 0.5 * math.sqrt((-4.0 * re))
          
          function code(re, im)
          	return Float64(0.5 * sqrt(Float64(-4.0 * re)))
          end
          
          function tmp = code(re, im)
          	tmp = 0.5 * sqrt((-4.0 * re));
          end
          
          code[re_, im_] := N[(0.5 * N[Sqrt[N[(-4.0 * re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          0.5 \cdot \sqrt{-4 \cdot re}
          \end{array}
          
          Derivation
          1. Initial program 45.5%

            \[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}{-4 \cdot re}} \]
          4. Step-by-step derivation
            1. lower-*.f6425.3

              \[\leadsto 0.5 \cdot \sqrt{-4 \cdot \color{blue}{re}} \]
          5. Applied rewrites25.3%

            \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-4 \cdot re}} \]
          6. Final simplification25.3%

            \[\leadsto 0.5 \cdot \sqrt{-4 \cdot re} \]
          7. Add Preprocessing

          Reproduce

          ?
          herbie shell --seed 2025084 
          (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)))))