?

Average Accuracy: 39.7% → 90.3%
Time: 10.2s
Precision: binary64
Cost: 27401

?

\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\begin{array}{l} t_0 := \sqrt{re \cdot re + im \cdot im} - re\\ \mathbf{if}\;t_0 \leq -1 \cdot 10^{-302} \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (- (sqrt (+ (* re re) (* im im))) re)))
   (if (or (<= t_0 -1e-302) (not (<= t_0 0.0)))
     (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
     (* (pow re -0.5) (* im 0.5)))))
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 t_0 = sqrt(((re * re) + (im * im))) - re;
	double tmp;
	if ((t_0 <= -1e-302) || !(t_0 <= 0.0)) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else {
		tmp = pow(re, -0.5) * (im * 0.5);
	}
	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 t_0 = Math.sqrt(((re * re) + (im * im))) - re;
	double tmp;
	if ((t_0 <= -1e-302) || !(t_0 <= 0.0)) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else {
		tmp = Math.pow(re, -0.5) * (im * 0.5);
	}
	return tmp;
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
def code(re, im):
	t_0 = math.sqrt(((re * re) + (im * im))) - re
	tmp = 0
	if (t_0 <= -1e-302) or not (t_0 <= 0.0):
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	else:
		tmp = math.pow(re, -0.5) * (im * 0.5)
	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)
	t_0 = Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)
	tmp = 0.0
	if ((t_0 <= -1e-302) || !(t_0 <= 0.0))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	else
		tmp = Float64((re ^ -0.5) * Float64(im * 0.5));
	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)
	t_0 = sqrt(((re * re) + (im * im))) - re;
	tmp = 0.0;
	if ((t_0 <= -1e-302) || ~((t_0 <= 0.0)))
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	else
		tmp = (re ^ -0.5) * (im * 0.5);
	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_] := Block[{t$95$0 = N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -1e-302], N[Not[LessEqual[t$95$0, 0.0]], $MachinePrecision]], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im} - re\\
\mathbf{if}\;t_0 \leq -1 \cdot 10^{-302} \lor \neg \left(t_0 \leq 0\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\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) < -9.9999999999999996e-303 or 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

    1. Initial program 43.9%

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

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

      [Start]43.9

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

      metadata-eval [<=]43.9

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

      metadata-eval [<=]43.9

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

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

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

      *-lft-identity [=>]43.9

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

      hypot-def [=>]89.0

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

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

    1. Initial program 9.8%

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

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

      [Start]9.8

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

      metadata-eval [<=]9.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 [<=]9.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* [<=]9.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 [=>]9.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 [=>]9.8

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

      hypot-def [=>]10.3

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

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

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

      [Start]51.4

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

      unpow2 [=>]51.4

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

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

      [Start]51.4

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

      *-commutative [=>]51.4

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

      associate-*r* [=>]51.4

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

      metadata-eval [=>]51.4

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

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

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

      sqrt-div [=>]57.5

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

      sqrt-prod [=>]98.7

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

      add-sqr-sqrt [<=]99.2

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

      associate-*l/ [=>]99.2

      \[ \color{blue}{\frac{im \cdot 0.5}{\sqrt{re}}} \]
    6. Applied egg-rr99.2%

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

      [Start]99.2

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

      div-inv [=>]99.0

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

      *-commutative [=>]99.0

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

      pow1/2 [=>]99.0

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

      pow-flip [=>]99.2

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

      metadata-eval [=>]99.2

      \[ {re}^{\color{blue}{-0.5}} \cdot \left(im \cdot 0.5\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 -1 \cdot 10^{-302} \lor \neg \left(\sqrt{re \cdot re + im \cdot im} - re \leq 0\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy58.5%
Cost7117
\[\begin{array}{l} \mathbf{if}\;im \leq 1.75 \cdot 10^{-141} \lor \neg \left(im \leq 4 \cdot 10^{-116}\right) \land im \leq 9.5 \cdot 10^{-72}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 2
Accuracy71.3%
Cost7112
\[\begin{array}{l} \mathbf{if}\;re \leq -1.2 \cdot 10^{+184}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 3.5 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 3
Accuracy69.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -2.75 \cdot 10^{+178}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 3.5 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 4
Accuracy69.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -1.6 \cdot 10^{+177}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{elif}\;re \leq 3.5 \cdot 10^{+113}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 5
Accuracy51.7%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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