?

Average Accuracy: 40.7% → 90.7%
Time: 13.3s
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:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \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)
   (/ (* im 0.5) (sqrt re))
   (* 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 = (im * 0.5) / sqrt(re);
	} 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 = (im * 0.5) / Math.sqrt(re);
	} 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 = (im * 0.5) / math.sqrt(re)
	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(Float64(im * 0.5) / sqrt(re));
	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 = (im * 0.5) / sqrt(re);
	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[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $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:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\

\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.4%

      \[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.4

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

      metadata-eval [<=]8.4

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

      metadata-eval [<=]8.4

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

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

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

      *-lft-identity [=>]8.4

      \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\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 43.9%

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

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

      [Start]43.9

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

      associate-*r/ [=>]43.9

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

      associate-/l* [=>]43.3

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

      associate-/r/ [=>]43.9

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

      unpow2 [=>]43.9

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

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

      [Start]43.9

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

      add-cbrt-cube [=>]34.7

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

      pow1/3 [=>]32.9

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

      add-sqr-sqrt [<=]32.9

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

      pow1 [=>]32.9

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

      pow1/2 [=>]32.9

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

      pow-prod-up [=>]32.9

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

      associate-*l/ [=>]32.9

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

      associate-/l* [=>]32.9

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

      associate-*r/ [=>]32.9

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

      metadata-eval [=>]32.9

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

      associate-/l* [<=]32.9

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

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

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

      associate-/l* [=>]34.8

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

      metadata-eval [=>]34.8

      \[ 0.5 \cdot {\left({\left(\frac{im}{\frac{re}{im}}\right)}^{\color{blue}{1.5}}\right)}^{0.3333333333333333} \]
    6. Applied egg-rr13.9%

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

      [Start]34.8

      \[ 0.5 \cdot {\left({\left(\frac{im}{\frac{re}{im}}\right)}^{1.5}\right)}^{0.3333333333333333} \]

      expm1-log1p-u [=>]34.8

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

      expm1-udef [=>]13.9

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

      *-commutative [=>]13.9

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

      pow-pow [=>]13.9

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

      metadata-eval [=>]13.9

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

      unpow1/2 [=>]13.9

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

      sqrt-div [=>]13.9

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

      sqrt-div [=>]13.9

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

      associate-/l* [<=]13.9

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

      add-sqr-sqrt [<=]13.9

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

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

      [Start]13.9

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

      expm1-def [=>]90.8

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

      expm1-log1p [=>]91.1

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

      associate-*l/ [=>]91.1

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

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

    1. Initial program 46.6%

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

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

      [Start]46.6

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

      metadata-eval [<=]46.6

      \[ 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.6

      \[ 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.6

      \[ 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.6

      \[ 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.6

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

      hypot-def [=>]90.6

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

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

Alternatives

Alternative 1
Accuracy76.1%
Cost7248
\[\begin{array}{l} \mathbf{if}\;re \leq -3.7 \cdot 10^{+40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-139}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{-82}:\\ \;\;\;\;\frac{0.5}{\frac{\sqrt{re}}{im}}\\ \mathbf{elif}\;re \leq 126:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 2
Accuracy76.4%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -1.32 \cdot 10^{+39}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 4000:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 3
Accuracy64.7%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 980:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \end{array} \]
Alternative 4
Accuracy64.5%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 1.42 \cdot 10^{-6}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 5
Accuracy64.6%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 0.09:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 6
Accuracy52.8%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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