?

Average Accuracy: 40.4% → 90.3%
Time: 9.7s
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:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-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)
   (* 0.5 (* im (pow re -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 = 0.5 * (im * pow(re, -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 = 0.5 * (im * Math.pow(re, -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 = 0.5 * (im * math.pow(re, -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(0.5 * Float64(im * (re ^ -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 = 0.5 * (im * (re ^ -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[(0.5 * N[(im * N[Power[re, -0.5], $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{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-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 7.0%

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

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

      [Start]7.0

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

      metadata-eval [<=]7.0

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

      metadata-eval [<=]7.0

      \[ 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* [<=]7.0

      \[ 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 [=>]7.0

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

      *-lft-identity [=>]7.0

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

      hypot-def [=>]17.8

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

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

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

      [Start]43.6

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

      associate-*r/ [=>]43.6

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

      associate-/l* [=>]43.0

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

      associate-/r/ [=>]43.6

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

      unpow2 [=>]43.6

      \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{0.5}{re} \cdot \color{blue}{\left(im \cdot im\right)}\right)} \]
    5. Taylor expanded in im around 0 90.5%

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

      \[\leadsto 0.5 \cdot \left(\color{blue}{\left(e^{\mathsf{log1p}\left({re}^{-0.5}\right)} - 1\right)} \cdot im\right) \]
      Proof

      [Start]90.5

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

      expm1-log1p-u [=>]88.0

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

      expm1-udef [=>]50.6

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

      inv-pow [=>]50.6

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

      metadata-eval [<=]50.6

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

      sqrt-pow1 [=>]50.6

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

      metadata-eval [=>]50.6

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

      metadata-eval [=>]50.6

      \[ 0.5 \cdot \left(\left(e^{\mathsf{log1p}\left({re}^{\color{blue}{-0.5}}\right)} - 1\right) \cdot im\right) \]
    7. Simplified90.5%

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

      [Start]50.6

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

      expm1-def [=>]88.0

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

      expm1-log1p [=>]90.5

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

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

    1. Initial program 46.2%

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

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

      [Start]46.2

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

      metadata-eval [<=]46.2

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

      metadata-eval [<=]46.2

      \[ 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* [<=]46.2

      \[ 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 [=>]46.2

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

      *-lft-identity [=>]46.2

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

      hypot-def [=>]90.3

      \[ 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 simplification90.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-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
Accuracy76.0%
Cost7428
\[\begin{array}{l} \mathbf{if}\;re \leq -2.25 \cdot 10^{+42}:\\ \;\;\;\;0.5 \cdot {\left(re \cdot -4 + im \cdot \frac{-1}{\frac{re}{im}}\right)}^{0.5}\\ \mathbf{elif}\;re \leq 1.8 \cdot 10^{-12}:\\ \;\;\;\;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
Accuracy76.2%
Cost7112
\[\begin{array}{l} \mathbf{if}\;re \leq -1.36 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 2.2 \cdot 10^{-15}:\\ \;\;\;\;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 3
Accuracy75.4%
Cost7048
\[\begin{array}{l} \mathbf{if}\;re \leq -2.15 \cdot 10^{+42}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 0.0146:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 4
Accuracy75.3%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -2.55 \cdot 10^{+42}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 4.9 \cdot 10^{-12}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 5
Accuracy64.6%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq -2.6 \cdot 10^{+38}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Accuracy52.3%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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