Average Error: 38.6 → 6.4
Time: 7.4s
Precision: binary64
Cost: 26884
\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\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} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(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) {
	return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * 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) {
	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 (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):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
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)
	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 (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 = 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 (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_] := 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[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]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\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}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

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 57.8

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      Proof
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (-.f64 (hypot.f64 re im) re)))): 0 points increase in error, 0 points decrease in error
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (-.f64 (Rewrite<= hypot-def_binary64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im)))) re)))): 126 points increase in error, 0 points decrease in error
    3. Taylor expanded in re around inf 30.4

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5}{\frac{re}{im \cdot im}}}} \]
      Proof
      (/.f64 1/2 (/.f64 re (*.f64 im im))): 0 points increase in error, 0 points decrease in error
      (/.f64 1/2 (/.f64 re (Rewrite<= unpow2_binary64 (pow.f64 im 2)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 1/2 (pow.f64 im 2)) re)): 11 points increase in error, 10 points decrease in error
      (Rewrite<= associate-*r/_binary64 (*.f64 1/2 (/.f64 (pow.f64 im 2) re))): 0 points increase in error, 0 points decrease in error
    5. Applied egg-rr0.9

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

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
      Proof
      (/.f64 im (sqrt.f64 re)): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite<= *-lft-identity_binary64 (*.f64 1 im)) (sqrt.f64 re)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 1 (sqrt.f64 re)) im)): 14 points increase in error, 12 points decrease in error
      (Rewrite<= associate-/r/_binary64 (/.f64 1 (/.f64 (sqrt.f64 re) im))): 14 points increase in error, 20 points decrease in error
      (Rewrite<= +-lft-identity_binary64 (+.f64 0 (/.f64 1 (/.f64 (sqrt.f64 re) im)))): 0 points increase in error, 0 points decrease in error

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

    1. Initial program 36.0

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}} \]
      Proof
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (-.f64 (hypot.f64 re im) re)))): 0 points increase in error, 0 points decrease in error
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (-.f64 (Rewrite<= hypot-def_binary64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im)))) re)))): 126 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification6.4

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

Alternatives

Alternative 1
Error15.4
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{if}\;re \leq -9.2 \cdot 10^{+91}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -3.8 \cdot 10^{+41}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq -0.00155:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2 \cdot 10^{+20}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 2
Error15.7
Cost7312
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{if}\;re \leq -9.2 \cdot 10^{+91}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.3 \cdot 10^{+41}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -7.6 \cdot 10^{-10}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.3 \cdot 10^{+21}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 3
Error23.0
Cost6916
\[\begin{array}{l} \mathbf{if}\;re \leq 1.15 \cdot 10^{+21}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 4
Error23.0
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 2.8 \cdot 10^{+21}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 5
Error30.9
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce

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