?

Average Accuracy: 38.9% → 99.5%
Time: 8.6s
Precision: binary64
Cost: 19972

?

\[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 4.7 \cdot 10^{-262}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\frac{im}{\sqrt{re + \mathsf{hypot}\left(im, re\right)}} \cdot \sqrt{2}\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 4.7e-262)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (* 0.5 (* (/ im (sqrt (+ re (hypot im re)))) (sqrt 2.0)))))
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 <= 4.7e-262) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else {
		tmp = 0.5 * ((im / sqrt((re + hypot(im, re)))) * sqrt(2.0));
	}
	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 <= 4.7e-262) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else {
		tmp = 0.5 * ((im / Math.sqrt((re + Math.hypot(im, re)))) * Math.sqrt(2.0));
	}
	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 <= 4.7e-262:
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	else:
		tmp = 0.5 * ((im / math.sqrt((re + math.hypot(im, re)))) * math.sqrt(2.0))
	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 <= 4.7e-262)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	else
		tmp = Float64(0.5 * Float64(Float64(im / sqrt(Float64(re + hypot(im, re)))) * sqrt(2.0)));
	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 <= 4.7e-262)
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	else
		tmp = 0.5 * ((im / sqrt((re + hypot(im, re)))) * sqrt(2.0));
	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, 4.7e-262], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[(im / N[Sqrt[N[(re + N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $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 4.7 \cdot 10^{-262}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\frac{im}{\sqrt{re + \mathsf{hypot}\left(im, re\right)}} \cdot \sqrt{2}\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 < 4.6999999999999998e-262

    1. Initial program 45.5%

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
    2. Simplified100.0%

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

    if 4.6999999999999998e-262 < re

    1. Initial program 30.7%

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

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

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

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

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

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

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

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

Alternatives

Alternative 1
Accuracy90.1%
Cost26884
\[\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
Accuracy76.9%
Cost7112
\[\begin{array}{l} \mathbf{if}\;re \leq -1.4 \cdot 10^{+19}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 6.2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 3
Accuracy76.2%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -1.25 \cdot 10^{+19}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 10^{-14}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 4
Accuracy63.2%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq -8.6 \cdot 10^{+18}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 5
Accuracy51.8%
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

herbie shell --seed 2023157 -o generate:proofs
(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)))))