math.sqrt on complex, real part

Percentage Accurate: 42.3% → 85.3%
Time: 4.7s
Alternatives: 9
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 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: 42.3% accurate, 1.0× speedup?

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

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

Alternative 1: 85.3% accurate, 0.5× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 12.1%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
      3. associate-/l*64.8%

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

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

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

    1. Initial program 45.1%

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

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

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

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

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

Alternative 2: 52.4% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -4.65 \cdot 10^{+61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq -6.8 \cdot 10^{-256}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 7.5 \cdot 10^{-79}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -4.65e+61)
   (* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5))))
   (if (<= re -6.8e-256)
     (* 0.5 (sqrt (+ (/ (* re re) im) (* 2.0 (+ re im)))))
     (if (<= re 7.5e-79)
       (* 0.5 (sqrt (* 2.0 (- re im))))
       (* 0.5 (* 2.0 (sqrt re)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -4.65e+61) {
		tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
	} else if (re <= -6.8e-256) {
		tmp = 0.5 * sqrt((((re * re) / im) + (2.0 * (re + im))));
	} else if (re <= 7.5e-79) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-4.65d+61)) then
        tmp = 0.5d0 * sqrt((2.0d0 * ((im / (re / im)) * (-0.5d0))))
    else if (re <= (-6.8d-256)) then
        tmp = 0.5d0 * sqrt((((re * re) / im) + (2.0d0 * (re + im))))
    else if (re <= 7.5d-79) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -4.65e+61) {
		tmp = 0.5 * Math.sqrt((2.0 * ((im / (re / im)) * -0.5)));
	} else if (re <= -6.8e-256) {
		tmp = 0.5 * Math.sqrt((((re * re) / im) + (2.0 * (re + im))));
	} else if (re <= 7.5e-79) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -4.65e+61:
		tmp = 0.5 * math.sqrt((2.0 * ((im / (re / im)) * -0.5)))
	elif re <= -6.8e-256:
		tmp = 0.5 * math.sqrt((((re * re) / im) + (2.0 * (re + im))))
	elif re <= 7.5e-79:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -4.65e+61)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5))));
	elseif (re <= -6.8e-256)
		tmp = Float64(0.5 * sqrt(Float64(Float64(Float64(re * re) / im) + Float64(2.0 * Float64(re + im)))));
	elseif (re <= 7.5e-79)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -4.65e+61)
		tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
	elseif (re <= -6.8e-256)
		tmp = 0.5 * sqrt((((re * re) / im) + (2.0 * (re + im))));
	elseif (re <= 7.5e-79)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -4.65e+61], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -6.8e-256], N[(0.5 * N[Sqrt[N[(N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision] + N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.5e-79], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.65 \cdot 10^{+61}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\

\mathbf{elif}\;re \leq -6.8 \cdot 10^{-256}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\

\mathbf{elif}\;re \leq 7.5 \cdot 10^{-79}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


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

    1. Initial program 10.6%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
      3. associate-/l*59.2%

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

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

    if -4.64999999999999987e61 < re < -6.8000000000000001e-256

    1. Initial program 45.5%

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

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

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

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

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

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

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

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

    if -6.8000000000000001e-256 < re < 7.49999999999999969e-79

    1. Initial program 47.9%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot im\right)}} \]
    5. Step-by-step derivation
      1. mul-1-neg56.3%

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

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

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

    if 7.49999999999999969e-79 < re

    1. Initial program 49.5%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.65 \cdot 10^{+61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq -6.8 \cdot 10^{-256}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 7.5 \cdot 10^{-79}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 3: 59.8% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{if}\;im \leq -1.1 \cdot 10^{+93}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.4 \cdot 10^{+65}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\ \mathbf{elif}\;im \leq -1.16 \cdot 10^{-54}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{-120}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* 2.0 (- re im))))))
   (if (<= im -1.1e+93)
     t_0
     (if (<= im -1.4e+65)
       (* 0.5 (sqrt (/ (* im (- im)) re)))
       (if (<= im -1.16e-54)
         t_0
         (if (<= im 1.55e-120)
           (* 0.5 (* 2.0 (sqrt re)))
           (* 0.5 (sqrt (* 2.0 (+ re im))))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((2.0 * (re - im)));
	double tmp;
	if (im <= -1.1e+93) {
		tmp = t_0;
	} else if (im <= -1.4e+65) {
		tmp = 0.5 * sqrt(((im * -im) / re));
	} else if (im <= -1.16e-54) {
		tmp = t_0;
	} else if (im <= 1.55e-120) {
		tmp = 0.5 * (2.0 * sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((2.0d0 * (re - im)))
    if (im <= (-1.1d+93)) then
        tmp = t_0
    else if (im <= (-1.4d+65)) then
        tmp = 0.5d0 * sqrt(((im * -im) / re))
    else if (im <= (-1.16d-54)) then
        tmp = t_0
    else if (im <= 1.55d-120) then
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((2.0 * (re - im)));
	double tmp;
	if (im <= -1.1e+93) {
		tmp = t_0;
	} else if (im <= -1.4e+65) {
		tmp = 0.5 * Math.sqrt(((im * -im) / re));
	} else if (im <= -1.16e-54) {
		tmp = t_0;
	} else if (im <= 1.55e-120) {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((2.0 * (re - im)))
	tmp = 0
	if im <= -1.1e+93:
		tmp = t_0
	elif im <= -1.4e+65:
		tmp = 0.5 * math.sqrt(((im * -im) / re))
	elif im <= -1.16e-54:
		tmp = t_0
	elif im <= 1.55e-120:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))))
	tmp = 0.0
	if (im <= -1.1e+93)
		tmp = t_0;
	elseif (im <= -1.4e+65)
		tmp = Float64(0.5 * sqrt(Float64(Float64(im * Float64(-im)) / re)));
	elseif (im <= -1.16e-54)
		tmp = t_0;
	elseif (im <= 1.55e-120)
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((2.0 * (re - im)));
	tmp = 0.0;
	if (im <= -1.1e+93)
		tmp = t_0;
	elseif (im <= -1.4e+65)
		tmp = 0.5 * sqrt(((im * -im) / re));
	elseif (im <= -1.16e-54)
		tmp = t_0;
	elseif (im <= 1.55e-120)
		tmp = 0.5 * (2.0 * sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -1.1e+93], t$95$0, If[LessEqual[im, -1.4e+65], N[(0.5 * N[Sqrt[N[(N[(im * (-im)), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, -1.16e-54], t$95$0, If[LessEqual[im, 1.55e-120], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{if}\;im \leq -1.1 \cdot 10^{+93}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;im \leq -1.4 \cdot 10^{+65}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\

\mathbf{elif}\;im \leq -1.16 \cdot 10^{-54}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;im \leq 1.55 \cdot 10^{-120}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if im < -1.10000000000000011e93 or -1.3999999999999999e65 < im < -1.16e-54

    1. Initial program 39.3%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot im\right)}} \]
    5. Step-by-step derivation
      1. mul-1-neg73.3%

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

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

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

    if -1.10000000000000011e93 < im < -1.3999999999999999e65

    1. Initial program 3.2%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    5. Step-by-step derivation
      1. associate-*r/83.3%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{-1 \cdot {im}^{2}}{re}}} \]
      2. neg-mul-183.3%

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{-\color{blue}{im \cdot im}}{re}} \]
      4. distribute-rgt-neg-in83.3%

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

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

    if -1.16e-54 < im < 1.5500000000000001e-120

    1. Initial program 39.0%

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

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

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

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

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

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

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

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

    if 1.5500000000000001e-120 < im

    1. Initial program 47.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out70.6%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -1.1 \cdot 10^{+93}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -1.4 \cdot 10^{+65}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\ \mathbf{elif}\;im \leq -1.16 \cdot 10^{-54}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{-120}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 4: 52.5% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -4.65 \cdot 10^{+61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq -4 \cdot 10^{-255}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{-78}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -4.65e+61)
   (* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5))))
   (if (<= re -4e-255)
     (* 0.5 (sqrt (* 2.0 im)))
     (if (<= re 6.2e-78)
       (* 0.5 (sqrt (* 2.0 (- re im))))
       (* 0.5 (* 2.0 (sqrt re)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -4.65e+61) {
		tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
	} else if (re <= -4e-255) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else if (re <= 6.2e-78) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else {
		tmp = 0.5 * (2.0 * sqrt(re));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-4.65d+61)) then
        tmp = 0.5d0 * sqrt((2.0d0 * ((im / (re / im)) * (-0.5d0))))
    else if (re <= (-4d-255)) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else if (re <= 6.2d-78) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -4.65e+61) {
		tmp = 0.5 * Math.sqrt((2.0 * ((im / (re / im)) * -0.5)));
	} else if (re <= -4e-255) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else if (re <= 6.2e-78) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -4.65e+61:
		tmp = 0.5 * math.sqrt((2.0 * ((im / (re / im)) * -0.5)))
	elif re <= -4e-255:
		tmp = 0.5 * math.sqrt((2.0 * im))
	elif re <= 6.2e-78:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	else:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -4.65e+61)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5))));
	elseif (re <= -4e-255)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	elseif (re <= 6.2e-78)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	else
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -4.65e+61)
		tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
	elseif (re <= -4e-255)
		tmp = 0.5 * sqrt((2.0 * im));
	elseif (re <= 6.2e-78)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	else
		tmp = 0.5 * (2.0 * sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -4.65e+61], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -4e-255], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.2e-78], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.65 \cdot 10^{+61}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\

\mathbf{elif}\;re \leq -4 \cdot 10^{-255}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{elif}\;re \leq 6.2 \cdot 10^{-78}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\


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

    1. Initial program 10.6%

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)} \]
      3. associate-/l*59.2%

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

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

    if -4.64999999999999987e61 < re < -4e-255

    1. Initial program 45.5%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative52.4%

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

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

    if -4e-255 < re < 6.20000000000000035e-78

    1. Initial program 47.9%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot im\right)}} \]
    5. Step-by-step derivation
      1. mul-1-neg56.3%

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

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

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

    if 6.20000000000000035e-78 < re

    1. Initial program 49.5%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.65 \cdot 10^{+61}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq -4 \cdot 10^{-255}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{-78}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]

Alternative 5: 60.7% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -1.1 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 3.6 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -1.1e-55)
   (* 0.5 (sqrt (* 2.0 (- re im))))
   (if (<= im 3.6e-121)
     (* 0.5 (* 2.0 (sqrt re)))
     (* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
	double tmp;
	if (im <= -1.1e-55) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else if (im <= 3.6e-121) {
		tmp = 0.5 * (2.0 * sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-1.1d-55)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else if (im <= 3.6d-121) then
        tmp = 0.5d0 * (2.0d0 * sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (im <= -1.1e-55) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else if (im <= 3.6e-121) {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -1.1e-55:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	elif im <= 3.6e-121:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + im)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -1.1e-55)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	elseif (im <= 3.6e-121)
		tmp = Float64(0.5 * Float64(2.0 * sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= -1.1e-55)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	elseif (im <= 3.6e-121)
		tmp = 0.5 * (2.0 * sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * (re + im)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -1.1e-55], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 3.6e-121], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.1 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{elif}\;im \leq 3.6 \cdot 10^{-121}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -1.1e-55

    1. Initial program 36.4%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot im\right)}} \]
    5. Step-by-step derivation
      1. mul-1-neg67.6%

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

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

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

    if -1.1e-55 < im < 3.59999999999999984e-121

    1. Initial program 39.0%

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

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

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

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

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

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

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

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

    if 3.59999999999999984e-121 < im

    1. Initial program 47.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im + 2 \cdot re}} \]
    5. Step-by-step derivation
      1. distribute-lft-out70.6%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -1.1 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 3.6 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]

Alternative 6: 59.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -1.32 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{-120}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -1.32e-55)
   (* 0.5 (sqrt (* im -2.0)))
   (if (<= im 2.8e-120) (* 0.5 (* 2.0 (sqrt re))) (* 0.5 (sqrt (* 2.0 im))))))
double code(double re, double im) {
	double tmp;
	if (im <= -1.32e-55) {
		tmp = 0.5 * sqrt((im * -2.0));
	} else if (im <= 2.8e-120) {
		tmp = 0.5 * (2.0 * sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-1.32d-55)) then
        tmp = 0.5d0 * sqrt((im * (-2.0d0)))
    else if (im <= 2.8d-120) then
        tmp = 0.5d0 * (2.0d0 * sqrt(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 (im <= -1.32e-55) {
		tmp = 0.5 * Math.sqrt((im * -2.0));
	} else if (im <= 2.8e-120) {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -1.32e-55:
		tmp = 0.5 * math.sqrt((im * -2.0))
	elif im <= 2.8e-120:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -1.32e-55)
		tmp = Float64(0.5 * sqrt(Float64(im * -2.0)));
	elseif (im <= 2.8e-120)
		tmp = Float64(0.5 * Float64(2.0 * sqrt(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 (im <= -1.32e-55)
		tmp = 0.5 * sqrt((im * -2.0));
	elseif (im <= 2.8e-120)
		tmp = 0.5 * (2.0 * sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -1.32e-55], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 2.8e-120], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -1.32 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\

\mathbf{elif}\;im \leq 2.8 \cdot 10^{-120}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -1.31999999999999993e-55

    1. Initial program 36.4%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative66.6%

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

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

    if -1.31999999999999993e-55 < im < 2.79999999999999994e-120

    1. Initial program 39.0%

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

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

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

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

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

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

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

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

    if 2.79999999999999994e-120 < im

    1. Initial program 47.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative69.3%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -1.32 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{-120}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 7: 60.1% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -8.5 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 4 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -8.5e-55)
   (* 0.5 (sqrt (* 2.0 (- re im))))
   (if (<= im 4e-121) (* 0.5 (* 2.0 (sqrt re))) (* 0.5 (sqrt (* 2.0 im))))))
double code(double re, double im) {
	double tmp;
	if (im <= -8.5e-55) {
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	} else if (im <= 4e-121) {
		tmp = 0.5 * (2.0 * sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-8.5d-55)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
    else if (im <= 4d-121) then
        tmp = 0.5d0 * (2.0d0 * sqrt(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 (im <= -8.5e-55) {
		tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
	} else if (im <= 4e-121) {
		tmp = 0.5 * (2.0 * Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -8.5e-55:
		tmp = 0.5 * math.sqrt((2.0 * (re - im)))
	elif im <= 4e-121:
		tmp = 0.5 * (2.0 * math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -8.5e-55)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im))));
	elseif (im <= 4e-121)
		tmp = Float64(0.5 * Float64(2.0 * sqrt(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 (im <= -8.5e-55)
		tmp = 0.5 * sqrt((2.0 * (re - im)));
	elseif (im <= 4e-121)
		tmp = 0.5 * (2.0 * sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -8.5e-55], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 4e-121], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -8.5 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\

\mathbf{elif}\;im \leq 4 \cdot 10^{-121}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if im < -8.49999999999999968e-55

    1. Initial program 36.4%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + -1 \cdot im\right)}} \]
    5. Step-by-step derivation
      1. mul-1-neg67.6%

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

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

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

    if -8.49999999999999968e-55 < im < 3.9999999999999999e-121

    1. Initial program 39.0%

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

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

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

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

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

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

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

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

    if 3.9999999999999999e-121 < im

    1. Initial program 47.6%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative69.3%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification61.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -8.5 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 4 \cdot 10^{-121}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 8: 53.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;im \leq -4 \cdot 10^{-311}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= im -4e-311) (* 0.5 (sqrt (* im -2.0))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
	double tmp;
	if (im <= -4e-311) {
		tmp = 0.5 * sqrt((im * -2.0));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (im <= (-4d-311)) then
        tmp = 0.5d0 * sqrt((im * (-2.0d0)))
    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 (im <= -4e-311) {
		tmp = 0.5 * Math.sqrt((im * -2.0));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if im <= -4e-311:
		tmp = 0.5 * math.sqrt((im * -2.0))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (im <= -4e-311)
		tmp = Float64(0.5 * sqrt(Float64(im * -2.0)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (im <= -4e-311)
		tmp = 0.5 * sqrt((im * -2.0));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[im, -4e-311], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;im \leq -4 \cdot 10^{-311}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if im < -3.99999999999979e-311

    1. Initial program 36.7%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative52.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot -2}} \]
    6. Simplified52.1%

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

    if -3.99999999999979e-311 < im

    1. Initial program 45.7%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{2 \cdot im}} \]
    5. Step-by-step derivation
      1. *-commutative55.4%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot 2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification53.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \leq -4 \cdot 10^{-311}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 9: 26.7% accurate, 2.0× speedup?

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

\\
0.5 \cdot \sqrt{im \cdot -2}
\end{array}
Derivation
  1. Initial program 41.3%

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

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

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

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

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-2 \cdot im}} \]
  5. Step-by-step derivation
    1. *-commutative25.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot -2}} \]
  6. Simplified25.4%

    \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot -2}} \]
  7. Final simplification25.4%

    \[\leadsto 0.5 \cdot \sqrt{im \cdot -2} \]

Developer target: 48.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im}\\ \mathbf{if}\;re < 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (sqrt (+ (* re re) (* im im)))))
   (if (< re 0.0)
     (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
     (* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
	double t_0 = sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = sqrt(((re * re) + (im * im)))
    if (re < 0.0d0) then
        tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = Math.sqrt(((re * re) + (im * im)));
	double tmp;
	if (re < 0.0) {
		tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
	}
	return tmp;
}
def code(re, im):
	t_0 = math.sqrt(((re * re) + (im * im)))
	tmp = 0
	if re < 0.0:
		tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re))))
	else:
		tmp = 0.5 * math.sqrt((2.0 * (t_0 + re)))
	return tmp
function code(re, im)
	t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im)))
	tmp = 0.0
	if (re < 0.0)
		tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re)))));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = sqrt(((re * re) + (im * im)));
	tmp = 0.0;
	if (re < 0.0)
		tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
	else
		tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2023189 
(FPCore (re im)
  :name "math.sqrt on complex, real part"
  :precision binary64

  :herbie-target
  (if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

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