Average Error: 39.3 → 7.8
Time: 3.1s
Precision: binary64
\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq 2.1745917313721787 \cdot 10^{-49}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(0.5 \cdot im\right)\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (if (<= re 2.1745917313721787e-49)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (* (pow re -0.5) (* 0.5 im))))
double code(double re, double im) {
	return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
}
double code(double re, double im) {
	double tmp;
	if (re <= 2.1745917313721787e-49) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else {
		tmp = pow(re, -0.5) * (0.5 * im);
	}
	return tmp;
}
public static double code(double re, double im) {
	return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)));
}
public static double code(double re, double im) {
	double tmp;
	if (re <= 2.1745917313721787e-49) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else {
		tmp = Math.pow(re, -0.5) * (0.5 * im);
	}
	return tmp;
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
def code(re, im):
	tmp = 0
	if re <= 2.1745917313721787e-49:
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	else:
		tmp = math.pow(re, -0.5) * (0.5 * im)
	return tmp
function code(re, im)
	return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))))
end
function code(re, im)
	tmp = 0.0
	if (re <= 2.1745917313721787e-49)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	else
		tmp = Float64((re ^ -0.5) * Float64(0.5 * im));
	end
	return tmp
end
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= 2.1745917313721787e-49)
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	else
		tmp = (re ^ -0.5) * (0.5 * im);
	end
	tmp_2 = tmp;
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]
code[re_, im_] := If[LessEqual[re, 2.1745917313721787e-49], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq 2.1745917313721787 \cdot 10^{-49}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

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


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 2 regimes
  2. if re < 2.17459173137217867e-49

    1. Initial program 32.7

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Simplified3.6

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

    if 2.17459173137217867e-49 < re

    1. Initial program 55.0

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Simplified36.3

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

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

      \[\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 17.6

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

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

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

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

Reproduce

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