?

Average Accuracy: 39.5% → 87.8%
Time: 9.8s
Precision: binary64
Cost: 13444

?

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

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\


\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 < 1.5e-6

    1. Initial program 49.3%

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

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

      [Start]49.3

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

      hypot-def [=>]92.1

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

    if 1.5e-6 < re

    1. Initial program 10.9%

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

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

      [Start]10.9

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

      hypot-def [=>]41.1

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

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

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

      [Start]45.5

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

      associate-*r/ [=>]45.5

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

      unpow2 [=>]45.5

      \[ 0.5 \cdot \sqrt{2 \cdot \frac{0.5 \cdot \color{blue}{\left(im \cdot im\right)}}{re}} \]
    5. Applied egg-rr75.3%

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

      [Start]45.5

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

      add-log-exp [=>]16.0

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

      *-un-lft-identity [=>]16.0

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

      log-prod [=>]16.0

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

      metadata-eval [=>]16.0

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

      add-log-exp [<=]45.5

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

      associate-*r/ [=>]45.5

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

      sqrt-div [=>]56.5

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

      associate-*r* [=>]56.5

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

      metadata-eval [=>]56.5

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

      *-un-lft-identity [<=]56.5

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

      sqrt-prod [=>]75.0

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

      add-sqr-sqrt [<=]75.3

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

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

      [Start]75.3

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

      +-lft-identity [=>]75.3

      \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.8%

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

Alternatives

Alternative 1
Accuracy76.1%
Cost13512
\[\begin{array}{l} \mathbf{if}\;re \leq -5.4 \cdot 10^{+19}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.45 \cdot 10^{-7}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{im - re} \cdot \sqrt{2}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 2
Accuracy76.3%
Cost7112
\[\begin{array}{l} \mathbf{if}\;re \leq -5.5 \cdot 10^{+19}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 1.22 \cdot 10^{-6}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 3
Accuracy75.7%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -4.7 \cdot 10^{+19}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 3.8 \cdot 10^{-6}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 4
Accuracy64.2%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 5.3 \cdot 10^{-8}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 5
Accuracy52.5%
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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