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

Percentage Accurate: 41.4% → 90.1%
Time: 6.5s
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: 41.4% 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: 90.1% 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 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 5.4%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im}{\frac{re}{im}}\right)}} \]
    5. Taylor expanded in im around 0 99.6%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{re}}\right)} \]
      2. unpow1/299.6%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. unpow-199.6%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left({re}^{-1}\right)}}^{0.5}\right) \]
      4. exp-to-pow93.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left(e^{\log re \cdot -1}\right)}}^{0.5}\right) \]
      5. *-commutative93.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-1 \cdot \log re}}\right)}^{0.5}\right) \]
      6. neg-mul-193.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-\log re}}\right)}^{0.5}\right) \]
      7. exp-prod93.6%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      8. distribute-lft-neg-out93.6%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      9. distribute-rgt-neg-in93.6%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      10. metadata-eval93.6%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      11. exp-to-pow99.6%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot {re}^{-0.5}\right)} \]
    8. Step-by-step derivation
      1. add-sqr-sqrt99.2%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \left(\sqrt{im} \cdot {re}^{-0.5}\right)\right)} \]
      3. add-sqr-sqrt98.8%

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

        \[\leadsto 0.5 \cdot \left(\sqrt{im} \cdot \left(\sqrt{im} \cdot \color{blue}{\sqrt{{re}^{-0.5} \cdot {re}^{-0.5}}}\right)\right) \]
      5. pow-prod-up98.9%

        \[\leadsto 0.5 \cdot \left(\sqrt{im} \cdot \left(\sqrt{im} \cdot \sqrt{\color{blue}{{re}^{\left(-0.5 + -0.5\right)}}}\right)\right) \]
      6. metadata-eval98.9%

        \[\leadsto 0.5 \cdot \left(\sqrt{im} \cdot \left(\sqrt{im} \cdot \sqrt{{re}^{\color{blue}{-1}}}\right)\right) \]
      7. inv-pow98.9%

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

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

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

        \[\leadsto 0.5 \cdot \left(\sqrt{im} \cdot \color{blue}{\frac{\sqrt{im}}{\sqrt{re}}}\right) \]
      11. associate-*r/99.3%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{im} \cdot \sqrt{im}}{\sqrt{re}}} \]
      12. add-sqr-sqrt99.6%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    9. Applied egg-rr99.6%

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

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

    1. Initial program 48.1%

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

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

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

    \[\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} \]

Alternative 2: 75.5% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -6700:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4.6 \cdot 10^{-29} \lor \neg \left(re \leq 3.15 \cdot 10^{+68}\right) \land re \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -6700.0)
   (* 0.5 (sqrt (* 2.0 (* re -2.0))))
   (if (or (<= re 4.6e-29) (and (not (<= re 3.15e+68)) (<= re 3.2e+113)))
     (* 0.5 (sqrt (* 2.0 im)))
     (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
	double tmp;
	if (re <= -6700.0) {
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 4.6e-29) || (!(re <= 3.15e+68) && (re <= 3.2e+113))) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im * pow(re, -0.5));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-6700.0d0)) then
        tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
    else if ((re <= 4.6d-29) .or. (.not. (re <= 3.15d+68)) .and. (re <= 3.2d+113)) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -6700.0) {
		tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
	} else if ((re <= 4.6e-29) || (!(re <= 3.15e+68) && (re <= 3.2e+113))) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -6700.0:
		tmp = 0.5 * math.sqrt((2.0 * (re * -2.0)))
	elif (re <= 4.6e-29) or (not (re <= 3.15e+68) and (re <= 3.2e+113)):
		tmp = 0.5 * math.sqrt((2.0 * im))
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -6700.0)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0))));
	elseif ((re <= 4.6e-29) || (!(re <= 3.15e+68) && (re <= 3.2e+113)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -6700.0)
		tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
	elseif ((re <= 4.6e-29) || (~((re <= 3.15e+68)) && (re <= 3.2e+113)))
		tmp = 0.5 * sqrt((2.0 * im));
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -6700.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 4.6e-29], And[N[Not[LessEqual[re, 3.15e+68]], $MachinePrecision], LessEqual[re, 3.2e+113]]], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 4.6 \cdot 10^{-29} \lor \neg \left(re \leq 3.15 \cdot 10^{+68}\right) \land re \leq 3.2 \cdot 10^{+113}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

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


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

    1. Initial program 38.3%

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

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

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

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

    if -6700 < re < 4.59999999999999982e-29 or 3.15000000000000013e68 < re < 3.1999999999999998e113

    1. Initial program 57.8%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u79.7%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2} \cdot \sqrt{im}\right)\right)} \]
      2. expm1-udef57.5%

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{2 \cdot im}}\right)} - 1\right) \]
    4. Applied egg-rr57.5%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)} - 1\right)} \]
    5. Step-by-step derivation
      1. expm1-def79.9%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)\right)} \]
      2. expm1-log1p84.0%

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{2 \cdot im}} \]
      3. *-commutative84.0%

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

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

    if 4.59999999999999982e-29 < re < 3.15000000000000013e68 or 3.1999999999999998e113 < re

    1. Initial program 14.7%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im}{\frac{re}{im}}\right)}} \]
    5. Taylor expanded in im around 0 76.5%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{re}}\right)} \]
      2. unpow1/276.5%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. unpow-176.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left({re}^{-1}\right)}}^{0.5}\right) \]
      4. exp-to-pow71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left(e^{\log re \cdot -1}\right)}}^{0.5}\right) \]
      5. *-commutative71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-1 \cdot \log re}}\right)}^{0.5}\right) \]
      6. neg-mul-171.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-\log re}}\right)}^{0.5}\right) \]
      7. exp-prod71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      8. distribute-lft-neg-out71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      9. distribute-rgt-neg-in71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      10. metadata-eval71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      11. exp-to-pow76.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -6700:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4.6 \cdot 10^{-29} \lor \neg \left(re \leq 3.15 \cdot 10^{+68}\right) \land re \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 3: 75.7% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;re \leq 8 \cdot 10^{+65} \lor \neg \left(re \leq 3.4 \cdot 10^{+113}\right):\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\

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


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

    1. Initial program 25.1%

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

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

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

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

    if -3.1000000000000003e70 < re < 8.79999999999999933e-30

    1. Initial program 63.6%

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

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

    if 8.79999999999999933e-30 < re < 7.9999999999999999e65 or 3.40000000000000019e113 < re

    1. Initial program 14.7%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im}{\frac{re}{im}}\right)}} \]
    5. Taylor expanded in im around 0 76.5%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{re}}\right)} \]
      2. unpow1/276.5%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. unpow-176.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left({re}^{-1}\right)}}^{0.5}\right) \]
      4. exp-to-pow71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left(e^{\log re \cdot -1}\right)}}^{0.5}\right) \]
      5. *-commutative71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-1 \cdot \log re}}\right)}^{0.5}\right) \]
      6. neg-mul-171.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-\log re}}\right)}^{0.5}\right) \]
      7. exp-prod71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      8. distribute-lft-neg-out71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      9. distribute-rgt-neg-in71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      10. metadata-eval71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      11. exp-to-pow76.5%

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

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

    if 7.9999999999999999e65 < re < 3.40000000000000019e113

    1. Initial program 18.1%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u68.8%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2} \cdot \sqrt{im}\right)\right)} \]
      2. expm1-udef69.3%

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

        \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{2 \cdot im}}\right)} - 1\right) \]
    4. Applied egg-rr69.3%

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)} - 1\right)} \]
    5. Step-by-step derivation
      1. expm1-def68.8%

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

        \[\leadsto 0.5 \cdot \color{blue}{\sqrt{2 \cdot im}} \]
      3. *-commutative74.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.1 \cdot 10^{+70}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 8.8 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 8 \cdot 10^{+65} \lor \neg \left(re \leq 3.4 \cdot 10^{+113}\right):\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternative 4: 64.0% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 3.7 \cdot 10^{-31} \lor \neg \left(re \leq 3.15 \cdot 10^{+68}\right) \land re \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (or (<= re 3.7e-31) (and (not (<= re 3.15e+68)) (<= re 3.2e+113)))
   (* 0.5 (sqrt (* 2.0 im)))
   (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
	double tmp;
	if ((re <= 3.7e-31) || (!(re <= 3.15e+68) && (re <= 3.2e+113))) {
		tmp = 0.5 * sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im * pow(re, -0.5));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if ((re <= 3.7d-31) .or. (.not. (re <= 3.15d+68)) .and. (re <= 3.2d+113)) then
        tmp = 0.5d0 * sqrt((2.0d0 * im))
    else
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if ((re <= 3.7e-31) || (!(re <= 3.15e+68) && (re <= 3.2e+113))) {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	} else {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if (re <= 3.7e-31) or (not (re <= 3.15e+68) and (re <= 3.2e+113)):
		tmp = 0.5 * math.sqrt((2.0 * im))
	else:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	return tmp
function code(re, im)
	tmp = 0.0
	if ((re <= 3.7e-31) || (!(re <= 3.15e+68) && (re <= 3.2e+113)))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if ((re <= 3.7e-31) || (~((re <= 3.15e+68)) && (re <= 3.2e+113)))
		tmp = 0.5 * sqrt((2.0 * im));
	else
		tmp = 0.5 * (im * (re ^ -0.5));
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[Or[LessEqual[re, 3.7e-31], And[N[Not[LessEqual[re, 3.15e+68]], $MachinePrecision], LessEqual[re, 3.2e+113]]], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.7 \cdot 10^{-31} \lor \neg \left(re \leq 3.15 \cdot 10^{+68}\right) \land re \leq 3.2 \cdot 10^{+113}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 3.6999999999999998e-31 or 3.15000000000000013e68 < re < 3.1999999999999998e113

    1. Initial program 51.9%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u63.1%

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)} - 1\right)} \]
    5. Step-by-step derivation
      1. expm1-def63.3%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)\right)} \]
      2. expm1-log1p66.6%

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

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

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

    if 3.6999999999999998e-31 < re < 3.15000000000000013e68 or 3.1999999999999998e113 < re

    1. Initial program 14.7%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im}{\frac{re}{im}}\right)}} \]
    5. Taylor expanded in im around 0 76.5%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{re}}\right)} \]
      2. unpow1/276.5%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. unpow-176.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left({re}^{-1}\right)}}^{0.5}\right) \]
      4. exp-to-pow71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left(e^{\log re \cdot -1}\right)}}^{0.5}\right) \]
      5. *-commutative71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-1 \cdot \log re}}\right)}^{0.5}\right) \]
      6. neg-mul-171.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-\log re}}\right)}^{0.5}\right) \]
      7. exp-prod71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      8. distribute-lft-neg-out71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      9. distribute-rgt-neg-in71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      10. metadata-eval71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      11. exp-to-pow76.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 3.7 \cdot 10^{-31} \lor \neg \left(re \leq 3.15 \cdot 10^{+68}\right) \land re \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternative 5: 64.1% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 7.2 \cdot 10^{-30} \lor \neg \left(re \leq 5.5 \cdot 10^{+65}\right) \land re \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;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 7.2e-30) (and (not (<= re 5.5e+65)) (<= re 3.2e+113)))
   (* 0.5 (sqrt (* 2.0 im)))
   (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
	double tmp;
	if ((re <= 7.2e-30) || (!(re <= 5.5e+65) && (re <= 3.2e+113))) {
		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 <= 7.2d-30) .or. (.not. (re <= 5.5d+65)) .and. (re <= 3.2d+113)) 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 <= 7.2e-30) || (!(re <= 5.5e+65) && (re <= 3.2e+113))) {
		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 <= 7.2e-30) or (not (re <= 5.5e+65) and (re <= 3.2e+113)):
		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 <= 7.2e-30) || (!(re <= 5.5e+65) && (re <= 3.2e+113)))
		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 <= 7.2e-30) || (~((re <= 5.5e+65)) && (re <= 3.2e+113)))
		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, 7.2e-30], And[N[Not[LessEqual[re, 5.5e+65]], $MachinePrecision], LessEqual[re, 3.2e+113]]], 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 7.2 \cdot 10^{-30} \lor \neg \left(re \leq 5.5 \cdot 10^{+65}\right) \land re \leq 3.2 \cdot 10^{+113}:\\
\;\;\;\;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 < 7.2000000000000006e-30 or 5.4999999999999996e65 < re < 3.1999999999999998e113

    1. Initial program 51.9%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)} \]
    3. Step-by-step derivation
      1. expm1-log1p-u63.1%

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

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

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)} - 1\right)} \]
    5. Step-by-step derivation
      1. expm1-def63.3%

        \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)\right)} \]
      2. expm1-log1p66.6%

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

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

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

    if 7.2000000000000006e-30 < re < 5.4999999999999996e65 or 3.1999999999999998e113 < re

    1. Initial program 14.7%

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

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(0.5 \cdot \frac{im}{\frac{re}{im}}\right)}} \]
    5. Taylor expanded in im around 0 76.5%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot \sqrt{\frac{1}{re}}\right)} \]
      2. unpow1/276.5%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{{\left(\frac{1}{re}\right)}^{0.5}}\right) \]
      3. unpow-176.5%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left({re}^{-1}\right)}}^{0.5}\right) \]
      4. exp-to-pow71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\color{blue}{\left(e^{\log re \cdot -1}\right)}}^{0.5}\right) \]
      5. *-commutative71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-1 \cdot \log re}}\right)}^{0.5}\right) \]
      6. neg-mul-171.7%

        \[\leadsto 0.5 \cdot \left(im \cdot {\left(e^{\color{blue}{-\log re}}\right)}^{0.5}\right) \]
      7. exp-prod71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot \color{blue}{e^{\left(-\log re\right) \cdot 0.5}}\right) \]
      8. distribute-lft-neg-out71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{-\log re \cdot 0.5}}\right) \]
      9. distribute-rgt-neg-in71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\color{blue}{\log re \cdot \left(-0.5\right)}}\right) \]
      10. metadata-eval71.7%

        \[\leadsto 0.5 \cdot \left(im \cdot e^{\log re \cdot \color{blue}{-0.5}}\right) \]
      11. exp-to-pow76.5%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(im \cdot {re}^{-0.5}\right)} \]
    8. Step-by-step derivation
      1. add-sqr-sqrt76.2%

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

        \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{im} \cdot \left(\sqrt{im} \cdot {re}^{-0.5}\right)\right)} \]
      3. add-sqr-sqrt76.0%

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

        \[\leadsto 0.5 \cdot \left(\sqrt{im} \cdot \left(\sqrt{im} \cdot \color{blue}{\sqrt{{re}^{-0.5} \cdot {re}^{-0.5}}}\right)\right) \]
      5. pow-prod-up76.1%

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

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

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

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

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

        \[\leadsto 0.5 \cdot \left(\sqrt{im} \cdot \color{blue}{\frac{\sqrt{im}}{\sqrt{re}}}\right) \]
      11. associate-*r/76.2%

        \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{im} \cdot \sqrt{im}}{\sqrt{re}}} \]
      12. add-sqr-sqrt76.4%

        \[\leadsto 0.5 \cdot \frac{\color{blue}{im}}{\sqrt{re}} \]
    9. Applied egg-rr76.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 7.2 \cdot 10^{-30} \lor \neg \left(re \leq 5.5 \cdot 10^{+65}\right) \land re \leq 3.2 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]

Alternative 6: 52.8% 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 42.9%

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

    \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{im}\right)} \]
  3. Step-by-step derivation
    1. expm1-log1p-u54.3%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2} \cdot \sqrt{im}\right)\right)} \]
    2. expm1-udef44.3%

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

      \[\leadsto 0.5 \cdot \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{2 \cdot im}}\right)} - 1\right) \]
  4. Applied egg-rr44.3%

    \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)} - 1\right)} \]
  5. Step-by-step derivation
    1. expm1-def54.4%

      \[\leadsto 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{2 \cdot im}\right)\right)} \]
    2. expm1-log1p57.3%

      \[\leadsto 0.5 \cdot \color{blue}{\sqrt{2 \cdot im}} \]
    3. *-commutative57.3%

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

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

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

Reproduce

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