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

Percentage Accurate: 40.5% → 89.7%
Time: 12.3s
Alternatives: 6
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 6 alternatives:

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

Initial Program: 40.5% accurate, 1.0× speedup?

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

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

Alternative 1: 89.7% accurate, 0.5× speedup?

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

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

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


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

    1. Initial program 7.6%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{\color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}^{2} - \left(-re\right) \cdot \left(-re\right)}{\sqrt{re \cdot re + im \cdot im} - \left(-re\right)}} \]
      5. hypot-define7.6%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    7. Simplified41.7%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u41.4%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)\right)} \]
      2. expm1-undefine16.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)} - 1\right)} \]
      3. associate-*r/16.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\frac{2 \cdot \left(0.5 \cdot {im}^{2}\right)}{re}}}\right)} - 1\right) \]
      4. sqrt-div16.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{2 \cdot \left(0.5 \cdot {im}^{2}\right)}}{\sqrt{re}}}\right)} - 1\right) \]
      5. associate-*r*16.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{\left(2 \cdot 0.5\right) \cdot {im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      6. metadata-eval16.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{1} \cdot {im}^{2}}}{\sqrt{re}}\right)} - 1\right) \]
      7. *-un-lft-identity16.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      8. sqrt-pow116.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{re}}\right)} - 1\right) \]
      9. metadata-eval16.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{{im}^{\color{blue}{1}}}{\sqrt{re}}\right)} - 1\right) \]
      10. pow116.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    9. Applied egg-rr16.7%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    10. Step-by-step derivation
      1. log1p-undefine16.7%

        \[\leadsto 0.5 \cdot \left(e^{\color{blue}{\log \left(1 + \frac{im}{\sqrt{re}}\right)}} - 1\right) \]
      2. rem-exp-log16.9%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(1 + \frac{im}{\sqrt{re}}\right)} - 1\right) \]
      3. +-commutative16.9%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\frac{im}{\sqrt{re}} + 1\right)} - 1\right) \]
      4. associate--l+99.6%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} + \left(1 - 1\right)\right)} \]
      5. metadata-eval99.6%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{0}\right) \]
      6. metadata-eval99.6%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{\left(-0\right)}\right) \]
      7. div099.6%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \left(-\color{blue}{\frac{0}{\sqrt{re}}}\right)\right) \]
      8. sub-neg99.6%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} - \frac{0}{\sqrt{re}}\right)} \]
      9. div-sub99.6%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im - 0}{\sqrt{re}}} \]
      10. --rgt-identity99.6%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    11. Simplified99.6%

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

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

    1. Initial program 42.8%

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

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

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

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

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

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

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

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

Alternative 2: 75.6% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1 \cdot 10^{+36}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{-97}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.35 \cdot 10^{-65} \lor \neg \left(re \leq 850000\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(re + im\right) - re\right)}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1e+36)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 2.1e-97)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (if (or (<= re 1.35e-65) (not (<= re 850000.0)))
       (* 0.5 (/ im (sqrt re)))
       (* 0.5 (sqrt (* 2.0 (- (+ re im) re))))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1e+36) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 2.1e-97) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if ((re <= 1.35e-65) || !(re <= 850000.0)) {
		tmp = 0.5 * (im / sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * ((re + im) - re)));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-1d+36)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 2.1d-97) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if ((re <= 1.35d-65) .or. (.not. (re <= 850000.0d0))) then
        tmp = 0.5d0 * (im / sqrt(re))
    else
        tmp = 0.5d0 * sqrt((2.0d0 * ((re + im) - re)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1e+36) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 2.1e-97) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if ((re <= 1.35e-65) || !(re <= 850000.0)) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * ((re + im) - re)));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1e+36:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 2.1e-97:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif (re <= 1.35e-65) or not (re <= 850000.0):
		tmp = 0.5 * (im / math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * ((re + im) - re)))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1e+36)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 2.1e-97)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif ((re <= 1.35e-65) || !(re <= 850000.0))
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(re + im) - re))));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1e+36)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 2.1e-97)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif ((re <= 1.35e-65) || ~((re <= 850000.0)))
		tmp = 0.5 * (im / sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * ((re + im) - re)));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1e+36], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.1e-97], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.35e-65], N[Not[LessEqual[re, 850000.0]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(re + im), $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

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

\mathbf{elif}\;re \leq 1.35 \cdot 10^{-65} \lor \neg \left(re \leq 850000\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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


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

    1. Initial program 41.9%

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

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

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

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

    if -1.00000000000000004e36 < re < 2.1000000000000001e-97

    1. Initial program 60.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 86.6%

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

    if 2.1000000000000001e-97 < re < 1.3499999999999999e-65 or 8.5e5 < re

    1. Initial program 8.1%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{\color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}^{2} - \left(-re\right) \cdot \left(-re\right)}{\sqrt{re \cdot re + im \cdot im} - \left(-re\right)}} \]
      5. hypot-define6.4%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    7. Simplified47.0%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u46.5%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)\right)} \]
      2. expm1-undefine29.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)} - 1\right)} \]
      3. associate-*r/29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\frac{2 \cdot \left(0.5 \cdot {im}^{2}\right)}{re}}}\right)} - 1\right) \]
      4. sqrt-div29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{2 \cdot \left(0.5 \cdot {im}^{2}\right)}}{\sqrt{re}}}\right)} - 1\right) \]
      5. associate-*r*29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{\left(2 \cdot 0.5\right) \cdot {im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      6. metadata-eval29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{1} \cdot {im}^{2}}}{\sqrt{re}}\right)} - 1\right) \]
      7. *-un-lft-identity29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      8. sqrt-pow138.6%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{re}}\right)} - 1\right) \]
      9. metadata-eval38.6%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{{im}^{\color{blue}{1}}}{\sqrt{re}}\right)} - 1\right) \]
      10. pow138.6%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    9. Applied egg-rr38.6%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    10. Step-by-step derivation
      1. log1p-undefine38.6%

        \[\leadsto 0.5 \cdot \left(e^{\color{blue}{\log \left(1 + \frac{im}{\sqrt{re}}\right)}} - 1\right) \]
      2. rem-exp-log39.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(1 + \frac{im}{\sqrt{re}}\right)} - 1\right) \]
      3. +-commutative39.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\frac{im}{\sqrt{re}} + 1\right)} - 1\right) \]
      4. associate--l+81.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} + \left(1 - 1\right)\right)} \]
      5. metadata-eval81.7%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{0}\right) \]
      6. metadata-eval81.7%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{\left(-0\right)}\right) \]
      7. div081.7%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \left(-\color{blue}{\frac{0}{\sqrt{re}}}\right)\right) \]
      8. sub-neg81.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} - \frac{0}{\sqrt{re}}\right)} \]
      9. div-sub81.7%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im - 0}{\sqrt{re}}} \]
      10. --rgt-identity81.7%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    11. Simplified81.7%

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

    if 1.3499999999999999e-65 < re < 8.5e5

    1. Initial program 24.1%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{\color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}^{2} - \left(-re\right) \cdot \left(-re\right)}{\sqrt{re \cdot re + im \cdot im} - \left(-re\right)}} \]
      5. hypot-define24.1%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1 \cdot 10^{+36}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.1 \cdot 10^{-97}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.35 \cdot 10^{-65} \lor \neg \left(re \leq 850000\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(re + im\right) - re\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 75.1% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.65 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.55 \cdot 10^{-78} \lor \neg \left(re \leq 1.35 \cdot 10^{-65}\right) \land re \leq 57:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1.65e+34)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (or (<= re 1.55e-78) (and (not (<= re 1.35e-65)) (<= re 57.0)))
     (* 0.5 (sqrt (* 2.0 im)))
     (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.65e+34) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 1.55e-78) || (!(re <= 1.35e-65) && (re <= 57.0))) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im / 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 <= (-1.65d+34)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if ((re <= 1.55d-78) .or. (.not. (re <= 1.35d-65)) .and. (re <= 57.0d0)) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -1.65e+34) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 1.55e-78) || (!(re <= 1.35e-65) && (re <= 57.0))) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.65e+34:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif (re <= 1.55e-78) or (not (re <= 1.35e-65) and (re <= 57.0)):
		tmp = 0.5 * math.sqrt((2.0 * im))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.65e+34)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif ((re <= 1.55e-78) || (!(re <= 1.35e-65) && (re <= 57.0)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -1.65e+34)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif ((re <= 1.55e-78) || (~((re <= 1.35e-65)) && (re <= 57.0)))
		tmp = 0.5 * sqrt((2.0 * im));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.65e+34], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.55e-78], And[N[Not[LessEqual[re, 1.35e-65]], $MachinePrecision], LessEqual[re, 57.0]]], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 1.55 \cdot 10^{-78} \lor \neg \left(re \leq 1.35 \cdot 10^{-65}\right) \land re \leq 57:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\


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

    1. Initial program 41.9%

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

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

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

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

    if -1.64999999999999994e34 < re < 1.55000000000000009e-78 or 1.3499999999999999e-65 < re < 57

    1. Initial program 54.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. sub-neg54.2%

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

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

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

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

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

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

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

    if 1.55000000000000009e-78 < re < 1.3499999999999999e-65 or 57 < re

    1. Initial program 8.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. sub-neg8.2%

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    7. Simplified48.2%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u47.7%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)} - 1\right)} \]
      3. associate-*r/30.4%

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{2 \cdot \left(0.5 \cdot {im}^{2}\right)}}{\sqrt{re}}}\right)} - 1\right) \]
      5. associate-*r*30.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{\left(2 \cdot 0.5\right) \cdot {im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      6. metadata-eval30.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{1} \cdot {im}^{2}}}{\sqrt{re}}\right)} - 1\right) \]
      7. *-un-lft-identity30.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      8. sqrt-pow139.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{re}}\right)} - 1\right) \]
      9. metadata-eval39.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{{im}^{\color{blue}{1}}}{\sqrt{re}}\right)} - 1\right) \]
      10. pow139.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    9. Applied egg-rr39.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    10. Step-by-step derivation
      1. log1p-undefine39.5%

        \[\leadsto 0.5 \cdot \left(e^{\color{blue}{\log \left(1 + \frac{im}{\sqrt{re}}\right)}} - 1\right) \]
      2. rem-exp-log40.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(1 + \frac{im}{\sqrt{re}}\right)} - 1\right) \]
      3. +-commutative40.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\frac{im}{\sqrt{re}} + 1\right)} - 1\right) \]
      4. associate--l+82.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} + \left(1 - 1\right)\right)} \]
      5. metadata-eval82.5%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{0}\right) \]
      6. metadata-eval82.5%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{\left(-0\right)}\right) \]
      7. div082.5%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \left(-\color{blue}{\frac{0}{\sqrt{re}}}\right)\right) \]
      8. sub-neg82.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} - \frac{0}{\sqrt{re}}\right)} \]
      9. div-sub82.5%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    11. Simplified82.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.65 \cdot 10^{+34}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.55 \cdot 10^{-78} \lor \neg \left(re \leq 1.35 \cdot 10^{-65}\right) \land re \leq 57:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 75.5% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -1.05 \cdot 10^{+36}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 3.8 \cdot 10^{-97}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 2.75 \cdot 10^{-65} \lor \neg \left(re \leq 0.051\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -1.05e+36)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (<= re 3.8e-97)
     (* 0.5 (sqrt (* 2.0 (- im re))))
     (if (or (<= re 2.75e-65) (not (<= re 0.051)))
       (* 0.5 (/ im (sqrt re)))
       (* 0.5 (sqrt (* 2.0 im)))))))
double code(double re, double im) {
	double tmp;
	if (re <= -1.05e+36) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if (re <= 3.8e-97) {
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	} else if ((re <= 2.75e-65) || !(re <= 0.051)) {
		tmp = 0.5 * (im / 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 (re <= (-1.05d+36)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if (re <= 3.8d-97) then
        tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
    else if ((re <= 2.75d-65) .or. (.not. (re <= 0.051d0))) then
        tmp = 0.5d0 * (im / 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 (re <= -1.05e+36) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if (re <= 3.8e-97) {
		tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
	} else if ((re <= 2.75e-65) || !(re <= 0.051)) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -1.05e+36:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif re <= 3.8e-97:
		tmp = 0.5 * math.sqrt((2.0 * (im - re)))
	elif (re <= 2.75e-65) or not (re <= 0.051):
		tmp = 0.5 * (im / math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -1.05e+36)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif (re <= 3.8e-97)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))));
	elseif ((re <= 2.75e-65) || !(re <= 0.051))
		tmp = Float64(0.5 * Float64(im / 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 (re <= -1.05e+36)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif (re <= 3.8e-97)
		tmp = 0.5 * sqrt((2.0 * (im - re)));
	elseif ((re <= 2.75e-65) || ~((re <= 0.051)))
		tmp = 0.5 * (im / sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -1.05e+36], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.8e-97], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 2.75e-65], N[Not[LessEqual[re, 0.051]], $MachinePrecision]], N[(0.5 * N[(im / 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}\;re \leq -1.05 \cdot 10^{+36}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\

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

\mathbf{elif}\;re \leq 2.75 \cdot 10^{-65} \lor \neg \left(re \leq 0.051\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

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


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

    1. Initial program 41.9%

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

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

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

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

    if -1.05000000000000002e36 < re < 3.8000000000000001e-97

    1. Initial program 60.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 86.6%

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

    if 3.8000000000000001e-97 < re < 2.7499999999999999e-65 or 0.0509999999999999967 < re

    1. Initial program 8.1%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{\color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}^{2} - \left(-re\right) \cdot \left(-re\right)}{\sqrt{re \cdot re + im \cdot im} - \left(-re\right)}} \]
      5. hypot-define6.4%

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    7. Simplified47.0%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u46.5%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)\right)} \]
      2. expm1-undefine29.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)} - 1\right)} \]
      3. associate-*r/29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\color{blue}{\frac{2 \cdot \left(0.5 \cdot {im}^{2}\right)}{re}}}\right)} - 1\right) \]
      4. sqrt-div29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{2 \cdot \left(0.5 \cdot {im}^{2}\right)}}{\sqrt{re}}}\right)} - 1\right) \]
      5. associate-*r*29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{\left(2 \cdot 0.5\right) \cdot {im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      6. metadata-eval29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{1} \cdot {im}^{2}}}{\sqrt{re}}\right)} - 1\right) \]
      7. *-un-lft-identity29.7%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      8. sqrt-pow138.6%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{re}}\right)} - 1\right) \]
      9. metadata-eval38.6%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{{im}^{\color{blue}{1}}}{\sqrt{re}}\right)} - 1\right) \]
      10. pow138.6%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    9. Applied egg-rr38.6%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    10. Step-by-step derivation
      1. log1p-undefine38.6%

        \[\leadsto 0.5 \cdot \left(e^{\color{blue}{\log \left(1 + \frac{im}{\sqrt{re}}\right)}} - 1\right) \]
      2. rem-exp-log39.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(1 + \frac{im}{\sqrt{re}}\right)} - 1\right) \]
      3. +-commutative39.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\frac{im}{\sqrt{re}} + 1\right)} - 1\right) \]
      4. associate--l+81.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} + \left(1 - 1\right)\right)} \]
      5. metadata-eval81.7%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{0}\right) \]
      6. metadata-eval81.7%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{\left(-0\right)}\right) \]
      7. div081.7%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \left(-\color{blue}{\frac{0}{\sqrt{re}}}\right)\right) \]
      8. sub-neg81.7%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} - \frac{0}{\sqrt{re}}\right)} \]
      9. div-sub81.7%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{im - 0}{\sqrt{re}}} \]
      10. --rgt-identity81.7%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    11. Simplified81.7%

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

    if 2.7499999999999999e-65 < re < 0.0509999999999999967

    1. Initial program 24.1%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{\color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}^{2} - \left(-re\right) \cdot \left(-re\right)}{\sqrt{re \cdot re + im \cdot im} - \left(-re\right)}} \]
      5. hypot-define24.1%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.05 \cdot 10^{+36}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 3.8 \cdot 10^{-97}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 2.75 \cdot 10^{-65} \lor \neg \left(re \leq 0.051\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 64.5% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 1.55 \cdot 10^{-78} \lor \neg \left(re \leq 1.35 \cdot 10^{-65}\right) \land re \leq 0.038:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re 1.55e-78) (and (not (<= re 1.35e-65)) (<= re 0.038)))
   (* 0.5 (sqrt (* 2.0 im)))
   (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if ((re <= 1.55e-78) || (!(re <= 1.35e-65) && (re <= 0.038))) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im / 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 <= 1.55d-78) .or. (.not. (re <= 1.35d-65)) .and. (re <= 0.038d0)) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else
        tmp = 0.5d0 * (im / sqrt(re))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((re <= 1.55e-78) || (!(re <= 1.35e-65) && (re <= 0.038))) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im / Math.sqrt(re));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= 1.55e-78) or (not (re <= 1.35e-65) and (re <= 0.038)):
		tmp = 0.5 * math.sqrt((2.0 * im))
	else:
		tmp = 0.5 * (im / math.sqrt(re))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= 1.55e-78) || (!(re <= 1.35e-65) && (re <= 0.038)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= 1.55e-78) || (~((re <= 1.35e-65)) && (re <= 0.038)))
		tmp = 0.5 * sqrt((2.0 * im));
	else
		tmp = 0.5 * (im / sqrt(re));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, 1.55e-78], And[N[Not[LessEqual[re, 1.35e-65]], $MachinePrecision], LessEqual[re, 0.038]]], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.55 \cdot 10^{-78} \lor \neg \left(re \leq 1.35 \cdot 10^{-65}\right) \land re \leq 0.038:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 1.55000000000000009e-78 or 1.3499999999999999e-65 < re < 0.0379999999999999991

    1. Initial program 50.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. sub-neg50.5%

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

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

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

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

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

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

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

    if 1.55000000000000009e-78 < re < 1.3499999999999999e-65 or 0.0379999999999999991 < re

    1. Initial program 8.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. sub-neg8.2%

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

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

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

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    7. Simplified48.2%

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u47.7%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot \frac{0.5 \cdot {im}^{2}}{re}}\right)} - 1\right)} \]
      3. associate-*r/30.4%

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{2 \cdot \left(0.5 \cdot {im}^{2}\right)}}{\sqrt{re}}}\right)} - 1\right) \]
      5. associate-*r*30.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{\left(2 \cdot 0.5\right) \cdot {im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      6. metadata-eval30.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{1} \cdot {im}^{2}}}{\sqrt{re}}\right)} - 1\right) \]
      7. *-un-lft-identity30.4%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\sqrt{\color{blue}{{im}^{2}}}}{\sqrt{re}}\right)} - 1\right) \]
      8. sqrt-pow139.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{{im}^{\left(\frac{2}{2}\right)}}}{\sqrt{re}}\right)} - 1\right) \]
      9. metadata-eval39.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{{im}^{\color{blue}{1}}}{\sqrt{re}}\right)} - 1\right) \]
      10. pow139.5%

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{\color{blue}{im}}{\sqrt{re}}\right)} - 1\right) \]
    9. Applied egg-rr39.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)} \]
    10. Step-by-step derivation
      1. log1p-undefine39.5%

        \[\leadsto 0.5 \cdot \left(e^{\color{blue}{\log \left(1 + \frac{im}{\sqrt{re}}\right)}} - 1\right) \]
      2. rem-exp-log40.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(1 + \frac{im}{\sqrt{re}}\right)} - 1\right) \]
      3. +-commutative40.8%

        \[\leadsto 0.5 \cdot \left(\color{blue}{\left(\frac{im}{\sqrt{re}} + 1\right)} - 1\right) \]
      4. associate--l+82.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} + \left(1 - 1\right)\right)} \]
      5. metadata-eval82.5%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{0}\right) \]
      6. metadata-eval82.5%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \color{blue}{\left(-0\right)}\right) \]
      7. div082.5%

        \[\leadsto 0.5 \cdot \left(\frac{im}{\sqrt{re}} + \left(-\color{blue}{\frac{0}{\sqrt{re}}}\right)\right) \]
      8. sub-neg82.5%

        \[\leadsto 0.5 \cdot \color{blue}{\left(\frac{im}{\sqrt{re}} - \frac{0}{\sqrt{re}}\right)} \]
      9. div-sub82.5%

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

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    11. Simplified82.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.55 \cdot 10^{-78} \lor \neg \left(re \leq 1.35 \cdot 10^{-65}\right) \land re \leq 0.038:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 52.6% accurate, 2.0× speedup?

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

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

    \[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. sub-neg38.6%

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{{\color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}^{2} - \left(-re\right) \cdot \left(-re\right)}{\sqrt{re \cdot re + im \cdot im} - \left(-re\right)}} \]
    5. hypot-define25.1%

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

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

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

    \[\leadsto 0.5 \cdot \sqrt{2 \cdot im} \]
  7. Add Preprocessing

Reproduce

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