?

Average Accuracy: 39.7% → 87.0%
Time: 12.4s
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 3.8 \cdot 10^{-119}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{elif}\;re \leq 1.02 \cdot 10^{-54} \lor \neg \left(re \leq 2.5 \cdot 10^{-9}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \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 3.8e-119)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (if (or (<= re 1.02e-54) (not (<= re 2.5e-9)))
     (* 0.5 (/ im (sqrt re)))
     (* 0.5 (sqrt (* 2.0 im))))))
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 <= 3.8e-119) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else if ((re <= 1.02e-54) || !(re <= 2.5e-9)) {
		tmp = 0.5 * (im / sqrt(re));
	} else {
		tmp = 0.5 * sqrt((2.0 * im));
	}
	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 <= 3.8e-119) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else if ((re <= 1.02e-54) || !(re <= 2.5e-9)) {
		tmp = 0.5 * (im / Math.sqrt(re));
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * im));
	}
	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 <= 3.8e-119:
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	elif (re <= 1.02e-54) or not (re <= 2.5e-9):
		tmp = 0.5 * (im / math.sqrt(re))
	else:
		tmp = 0.5 * math.sqrt((2.0 * im))
	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 <= 3.8e-119)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	elseif ((re <= 1.02e-54) || !(re <= 2.5e-9))
		tmp = Float64(0.5 * Float64(im / sqrt(re)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * im)));
	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 <= 3.8e-119)
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	elseif ((re <= 1.02e-54) || ~((re <= 2.5e-9)))
		tmp = 0.5 * (im / sqrt(re));
	else
		tmp = 0.5 * sqrt((2.0 * im));
	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, 3.8e-119], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.02e-54], N[Not[LessEqual[re, 2.5e-9]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $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 3.8 \cdot 10^{-119}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

\mathbf{elif}\;re \leq 1.02 \cdot 10^{-54} \lor \neg \left(re \leq 2.5 \cdot 10^{-9}\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 3 regimes
  2. if re < 3.79999999999999975e-119

    1. Initial program 51.2%

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

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

      [Start]51.2

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

      hypot-def [=>]96.2

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

    if 3.79999999999999975e-119 < re < 1.01999999999999999e-54 or 2.5000000000000001e-9 < re

    1. Initial program 16.3%

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

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

      [Start]16.3

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

      hypot-def [=>]44.7

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

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

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

      [Start]70.4

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

      associate-*l* [=>]70.4

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

      *-commutative [=>]70.4

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

      associate-*l* [=>]70.5

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

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

      [Start]70.5

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

      add-log-exp [=>]14.9

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

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

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

      log-prod [=>]14.9

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

      metadata-eval [=>]14.9

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

      add-log-exp [<=]70.5

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

      *-commutative [=>]70.5

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

      associate-*r* [=>]70.4

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

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

      [Start]70.9

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

      +-lft-identity [=>]70.9

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

      associate-*r/ [=>]70.9

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

      metadata-eval [=>]70.9

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

      unpow-1 [<=]70.9

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

      exp-to-pow [<=]66.9

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

      exp-prod [<=]66.9

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

      *-commutative [=>]66.9

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

      neg-mul-1 [<=]66.9

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

      distribute-lft-neg-out [=>]66.9

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

      exp-neg [=>]66.9

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

      associate-*l/ [=>]66.9

      \[ 0.5 \cdot \color{blue}{\frac{1 \cdot im}{e^{\log re \cdot 0.5}}} \]

      *-lft-identity [=>]66.9

      \[ 0.5 \cdot \frac{\color{blue}{im}}{e^{\log re \cdot 0.5}} \]

      exp-to-pow [=>]71.0

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

      unpow1/2 [=>]71.0

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

    if 1.01999999999999999e-54 < re < 2.5000000000000001e-9

    1. Initial program 34.8%

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

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

      [Start]34.8

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

      hypot-def [=>]58.6

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 3.8 \cdot 10^{-119}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{elif}\;re \leq 1.02 \cdot 10^{-54} \lor \neg \left(re \leq 2.5 \cdot 10^{-9}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy74.0%
Cost7513
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{if}\;re \leq -300000000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.25 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -6.5 \cdot 10^{-71}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 3.8 \cdot 10^{-119} \lor \neg \left(re \leq 7.5 \cdot 10^{-55}\right) \land re \leq 1.9 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 2
Accuracy73.7%
Cost7513
\[\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 -2.7 \cdot 10^{+124}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.6 \cdot 10^{-16}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -6.8 \cdot 10^{-71}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.95 \cdot 10^{-119}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.2 \cdot 10^{-57} \lor \neg \left(re \leq 4.3 \cdot 10^{-10}\right):\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 3
Accuracy62.9%
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 2.6 \cdot 10^{-119} \lor \neg \left(re \leq 3.8 \cdot 10^{-57}\right) \land re \leq 1.55 \cdot 10^{-9}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 4
Accuracy51.4%
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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