?

Average Error: 60.48% → 10.28%
Time: 12.6s
Precision: binary64
Cost: 20356

?

\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\begin{array}{l} \mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \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 (+ (* re re) (* im im))) re) 0.0)
   (* (pow re -0.5) (* im 0.5))
   (* 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(((re * re) + (im * im))) - re) <= 0.0) {
		tmp = pow(re, -0.5) * (im * 0.5);
	} 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(((re * re) + (im * im))) - re) <= 0.0) {
		tmp = Math.pow(re, -0.5) * (im * 0.5);
	} 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(((re * re) + (im * im))) - re) <= 0.0:
		tmp = math.pow(re, -0.5) * (im * 0.5)
	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 (Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re) <= 0.0)
		tmp = Float64((re ^ -0.5) * Float64(im * 0.5));
	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(((re * re) + (im * im))) - re) <= 0.0)
		tmp = (re ^ -0.5) * (im * 0.5);
	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[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision], 0.0], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $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{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\

\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 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 91.44

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

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

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

      [Start]56.33

      \[ 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)} \]

      unpow2 [=>]56.33

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

      associate-/l* [=>]46.28

      \[ 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \color{blue}{\frac{im}{\frac{re}{im}}}\right)} \]
    4. Applied egg-rr86.23

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(0.5 \cdot \frac{im}{\sqrt{re}}\right)} - 1} \]
    5. Simplified8.55

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

      [Start]86.23

      \[ e^{\mathsf{log1p}\left(0.5 \cdot \frac{im}{\sqrt{re}}\right)} - 1 \]

      expm1-def [=>]8.74

      \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(0.5 \cdot \frac{im}{\sqrt{re}}\right)\right)} \]

      expm1-log1p [=>]8.56

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

      associate-*r/ [=>]8.55

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

      *-commutative [=>]8.55

      \[ \frac{\color{blue}{im \cdot 0.5}}{\sqrt{re}} \]
    6. Applied egg-rr8.57

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

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

    1. Initial program 54.8

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

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

      [Start]54.8

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

      metadata-eval [<=]54.8

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

      metadata-eval [<=]54.8

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

      associate-*r* [<=]54.8

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

      metadata-eval [=>]54.8

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

      *-lft-identity [=>]54.8

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

      hypot-def [=>]10.59

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

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

Alternatives

Alternative 1
Error23.35%
Cost7112
\[\begin{array}{l} \mathbf{if}\;re \leq -6.5 \cdot 10^{-28}:\\ \;\;\;\;0.5 \cdot {\left(re \cdot -4\right)}^{0.5}\\ \mathbf{elif}\;re \leq 1.55 \cdot 10^{-20}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
Alternative 2
Error23.66%
Cost7048
\[\begin{array}{l} \mathbf{if}\;re \leq -5.5 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot {\left(re \cdot -4\right)}^{0.5}\\ \mathbf{elif}\;re \leq 5.3 \cdot 10^{-18}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
Alternative 3
Error23.82%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -7.5 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot {\left(re \cdot -4\right)}^{0.5}\\ \mathbf{elif}\;re \leq 1.3 \cdot 10^{-16}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 4
Error36.26%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 1.3 \cdot 10^{-16}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\ \end{array} \]
Alternative 5
Error35.95%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 2 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 6
Error48.87%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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