?

Average Accuracy: 39.4% → 89.8%
Time: 8.2s
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 8.3%

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

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

      [Start]8.3

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

      hypot-def [=>]18.6

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

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

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

      [Start]41.3

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

      unpow2 [=>]41.3

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

      associate-/l* [=>]51.9

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

      \[\leadsto \color{blue}{0.5 \cdot \left(\sqrt{\frac{1}{re}} \cdot im\right)} \]
    6. Simplified90.9%

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

      [Start]90.9

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

      *-commutative [=>]90.9

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

      associate-*l* [=>]90.9

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

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

      [Start]90.9

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

      expm1-log1p-u [=>]88.7

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

      expm1-udef [=>]49.8

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

      log1p-udef [=>]49.8

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

      add-exp-log [<=]52.1

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

      inv-pow [=>]52.1

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

      sqrt-pow1 [=>]52.1

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

      metadata-eval [=>]52.1

      \[ \left(\left(1 + {re}^{\color{blue}{-0.5}}\right) - 1\right) \cdot \left(im \cdot 0.5\right) \]
    8. Simplified90.9%

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

      [Start]52.1

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

      +-commutative [=>]52.1

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

      associate--l+ [=>]90.9

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

      metadata-eval [=>]90.9

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

      +-rgt-identity [=>]90.9

      \[ \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 44.6%

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

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

      [Start]44.6

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

      hypot-def [=>]89.7

      \[ 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 simplification89.8%

    \[\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
Accuracy76.6%
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{if}\;re \leq -7.8 \cdot 10^{+142}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.16 \cdot 10^{+120}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -2.05 \cdot 10^{+37}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 2.7 \cdot 10^{-20}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
Alternative 2
Accuracy75.7%
Cost7312
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{if}\;re \leq -7.8 \cdot 10^{+142}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.16 \cdot 10^{+120}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.7 \cdot 10^{+35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.55 \cdot 10^{-19}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
Alternative 3
Accuracy64.2%
Cost6916
\[\begin{array}{l} \mathbf{if}\;re \leq 1.65 \cdot 10^{-19}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\ \end{array} \]
Alternative 4
Accuracy64.2%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 1.06 \cdot 10^{-19}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 5
Accuracy52.3%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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