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

Percentage Accurate: 41.1% → 90.1%
Time: 5.5s
Alternatives: 5
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 5 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.1% 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:\\ \;\;\;\;\frac{im \cdot 0.5}{\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)
   (/ (* im 0.5) (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 = (im * 0.5) / 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 = (im * 0.5) / 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 = (im * 0.5) / 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(Float64(im * 0.5) / 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 = (im * 0.5) / 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[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $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:\\
\;\;\;\;\frac{im \cdot 0.5}{\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 6.9%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
      2. associate-/l*56.2%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    7. Applied egg-rr7.7%

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

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

        \[\leadsto \color{blue}{0.5 \cdot \frac{im}{\sqrt{re}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      4. *-commutative99.7%

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

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

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

    1. Initial program 52.3%

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

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

      \[\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 simplification92.9%

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

Alternative 2: 76.1% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -4.4 \cdot 10^{+81}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-87}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-83}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{elif}\;re \leq 4.3 \cdot 10^{-30}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (sqrt (* 2.0 (- im re))))))
   (if (<= re -4.4e+81)
     (* 0.5 (sqrt (* re -4.0)))
     (if (<= re 5e-87)
       t_0
       (if (<= re 2.3e-83)
         (* 0.5 (* im (pow re -0.5)))
         (if (<= re 4.3e-30) t_0 (/ (* im 0.5) (sqrt re))))))))
double code(double re, double im) {
	double t_0 = 0.5 * sqrt((2.0 * (im - re)));
	double tmp;
	if (re <= -4.4e+81) {
		tmp = 0.5 * sqrt((re * -4.0));
	} else if (re <= 5e-87) {
		tmp = t_0;
	} else if (re <= 2.3e-83) {
		tmp = 0.5 * (im * pow(re, -0.5));
	} else if (re <= 4.3e-30) {
		tmp = t_0;
	} else {
		tmp = (im * 0.5) / sqrt(re);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = 0.5d0 * sqrt((2.0d0 * (im - re)))
    if (re <= (-4.4d+81)) then
        tmp = 0.5d0 * sqrt((re * (-4.0d0)))
    else if (re <= 5d-87) then
        tmp = t_0
    else if (re <= 2.3d-83) then
        tmp = 0.5d0 * (im * (re ** (-0.5d0)))
    else if (re <= 4.3d-30) then
        tmp = t_0
    else
        tmp = (im * 0.5d0) / sqrt(re)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double t_0 = 0.5 * Math.sqrt((2.0 * (im - re)));
	double tmp;
	if (re <= -4.4e+81) {
		tmp = 0.5 * Math.sqrt((re * -4.0));
	} else if (re <= 5e-87) {
		tmp = t_0;
	} else if (re <= 2.3e-83) {
		tmp = 0.5 * (im * Math.pow(re, -0.5));
	} else if (re <= 4.3e-30) {
		tmp = t_0;
	} else {
		tmp = (im * 0.5) / Math.sqrt(re);
	}
	return tmp;
}
def code(re, im):
	t_0 = 0.5 * math.sqrt((2.0 * (im - re)))
	tmp = 0
	if re <= -4.4e+81:
		tmp = 0.5 * math.sqrt((re * -4.0))
	elif re <= 5e-87:
		tmp = t_0
	elif re <= 2.3e-83:
		tmp = 0.5 * (im * math.pow(re, -0.5))
	elif re <= 4.3e-30:
		tmp = t_0
	else:
		tmp = (im * 0.5) / math.sqrt(re)
	return tmp
function code(re, im)
	t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re))))
	tmp = 0.0
	if (re <= -4.4e+81)
		tmp = Float64(0.5 * sqrt(Float64(re * -4.0)));
	elseif (re <= 5e-87)
		tmp = t_0;
	elseif (re <= 2.3e-83)
		tmp = Float64(0.5 * Float64(im * (re ^ -0.5)));
	elseif (re <= 4.3e-30)
		tmp = t_0;
	else
		tmp = Float64(Float64(im * 0.5) / sqrt(re));
	end
	return tmp
end
function tmp_2 = code(re, im)
	t_0 = 0.5 * sqrt((2.0 * (im - re)));
	tmp = 0.0;
	if (re <= -4.4e+81)
		tmp = 0.5 * sqrt((re * -4.0));
	elseif (re <= 5e-87)
		tmp = t_0;
	elseif (re <= 2.3e-83)
		tmp = 0.5 * (im * (re ^ -0.5));
	elseif (re <= 4.3e-30)
		tmp = t_0;
	else
		tmp = (im * 0.5) / sqrt(re);
	end
	tmp_2 = tmp;
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -4.4e+81], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5e-87], t$95$0, If[LessEqual[re, 2.3e-83], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.3e-30], t$95$0, N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;re \leq 5 \cdot 10^{-87}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 2.3 \cdot 10^{-83}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\

\mathbf{elif}\;re \leq 4.3 \cdot 10^{-30}:\\
\;\;\;\;t_0\\

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


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

    1. Initial program 28.2%

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

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

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

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

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

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

    if -4.39999999999999974e81 < re < 5.00000000000000042e-87 or 2.2999999999999999e-83 < re < 4.29999999999999966e-30

    1. Initial program 65.8%

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

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

    if 5.00000000000000042e-87 < re < 2.2999999999999999e-83

    1. Initial program 4.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
      2. associate-/l*61.4%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    7. Step-by-step derivation
      1. associate-/r/61.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{re} \cdot im}} \]
      2. *-commutative61.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{im \cdot \frac{im}{re}}} \]
      3. *-un-lft-identity61.7%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right)} \cdot \frac{im}{re}} \]
      8. *-un-lft-identity61.4%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \frac{\color{blue}{1 \cdot im}}{re}} \]
      9. add-sqr-sqrt61.1%

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

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \color{blue}{\left(\frac{1}{\sqrt{re}} \cdot \frac{im}{\sqrt{re}}\right)}} \]
      11. metadata-eval61.1%

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

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\color{blue}{\sqrt{\frac{1}{re}}} \cdot \frac{im}{\sqrt{re}}\right)} \]
      13. *-un-lft-identity61.4%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \frac{\color{blue}{1 \cdot im}}{\sqrt{re}}\right)} \]
      14. metadata-eval61.4%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \frac{\color{blue}{\sqrt{1}} \cdot im}{\sqrt{re}}\right)} \]
      15. metadata-eval61.4%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \frac{\sqrt{\color{blue}{2 \cdot 0.5}} \cdot im}{\sqrt{re}}\right)} \]
      16. sqrt-unprod60.9%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \frac{\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right)} \cdot im}{\sqrt{re}}\right)} \]
      17. associate-*r*61.1%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \frac{\color{blue}{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)}}{\sqrt{re}}\right)} \]
      18. un-div-inv61.4%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \color{blue}{\left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \frac{1}{\sqrt{re}}\right)}\right)} \]
      19. metadata-eval61.4%

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

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

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \color{blue}{\left(\sqrt{2} \cdot \left(\left(\sqrt{0.5} \cdot im\right) \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)} \]
      22. *-commutative60.9%

        \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \left(\sqrt{\frac{1}{re}} \cdot \left(\sqrt{2} \cdot \left(\color{blue}{\left(im \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)} \]
      23. associate-*r*60.7%

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

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

    if 4.29999999999999966e-30 < re

    1. Initial program 12.2%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
      2. associate-/l*50.5%

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

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

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

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

        \[\leadsto \color{blue}{0.5 \cdot \frac{im}{\sqrt{re}}} \]
      3. associate-*r/82.2%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      4. *-commutative82.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.4 \cdot 10^{+81}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-87}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-83}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \mathbf{elif}\;re \leq 4.3 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]

Alternative 3: 74.8% accurate, 2.0× speedup?

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

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

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

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


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

    1. Initial program 48.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified75.1%

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

    if -4.5999999999999998e-58 < re < 5.00000000000000042e-87

    1. Initial program 63.4%

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

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

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

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

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

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

    if 5.00000000000000042e-87 < re

    1. Initial program 17.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\frac{\color{blue}{im \cdot im}}{re}} \]
      2. associate-/l*45.7%

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im}{\frac{re}{im}}}} \]
    7. Applied egg-rr14.3%

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

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

        \[\leadsto \color{blue}{0.5 \cdot \frac{im}{\sqrt{re}}} \]
      3. associate-*r/75.6%

        \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{re}}} \]
      4. *-commutative75.6%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -4.6 \cdot 10^{-58}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-87}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]

Alternative 4: 63.4% accurate, 2.0× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -2.2499999999999999e-54

    1. Initial program 48.4%

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

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

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

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

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{re \cdot -4}} \]
    6. Simplified75.1%

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

    if -2.2499999999999999e-54 < re

    1. Initial program 45.6%

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

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

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

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

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

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

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

Alternative 5: 52.1% 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 46.5%

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

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

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

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

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

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

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

Reproduce

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