?

Average Error: 38.0 → 4.5
Time: 10.0s
Precision: binary64
Cost: 13832

?

\[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 \cdot 10^{-260}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{+160}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{2 \cdot im}{\frac{re + \mathsf{hypot}\left(re, im\right)}{im}}}\\ \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 -1e-260)
   (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
   (if (<= re 1.2e+160)
     (* 0.5 (sqrt (/ (* 2.0 im) (/ (+ re (hypot re im)) im))))
     (* 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 <= -1e-260) {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	} else if (re <= 1.2e+160) {
		tmp = 0.5 * sqrt(((2.0 * im) / ((re + hypot(re, im)) / im)));
	} 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 <= -1e-260) {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	} else if (re <= 1.2e+160) {
		tmp = 0.5 * Math.sqrt(((2.0 * im) / ((re + Math.hypot(re, im)) / im)));
	} 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 <= -1e-260:
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	elif re <= 1.2e+160:
		tmp = 0.5 * math.sqrt(((2.0 * im) / ((re + math.hypot(re, im)) / im)))
	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 <= -1e-260)
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))));
	elseif (re <= 1.2e+160)
		tmp = Float64(0.5 * sqrt(Float64(Float64(2.0 * im) / Float64(Float64(re + hypot(re, im)) / im))));
	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 <= -1e-260)
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	elseif (re <= 1.2e+160)
		tmp = 0.5 * sqrt(((2.0 * im) / ((re + hypot(re, im)) / im)));
	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, -1e-260], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.2e+160], N[(0.5 * N[Sqrt[N[(N[(2.0 * im), $MachinePrecision] / N[(N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision] / im), $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 \cdot 10^{-260}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\

\mathbf{elif}\;re \leq 1.2 \cdot 10^{+160}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{2 \cdot im}{\frac{re + \mathsf{hypot}\left(re, im\right)}{im}}}\\

\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 3 regimes
  2. if re < -9.99999999999999961e-261

    1. Initial program 30.4

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

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

      [Start]30.4

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

      metadata-eval [<=]30.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 [<=]30.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* [<=]30.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 [=>]30.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 [=>]30.4

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

      hypot-def [=>]0.0

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

    if -9.99999999999999961e-261 < re < 1.2000000000000001e160

    1. Initial program 39.1

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

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

      [Start]39.1

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

      metadata-eval [<=]39.1

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

      metadata-eval [<=]39.1

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

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

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

      *-lft-identity [=>]39.1

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

      hypot-def [=>]19.2

      \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)} \]
    3. Applied egg-rr39.0

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{{\left(\mathsf{hypot}\left(re, im\right)\right)}^{2} - re \cdot re}{re + \mathsf{hypot}\left(re, im\right)}}} \]
    4. Taylor expanded in re around 0 30.7

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{{im}^{2}}}{re + \mathsf{hypot}\left(re, im\right)}} \]
    5. Simplified30.7

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

      [Start]30.7

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

      unpow2 [=>]30.7

      \[ 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{im \cdot im}}{re + \mathsf{hypot}\left(re, im\right)}} \]
    6. Applied egg-rr8.7

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

    if 1.2000000000000001e160 < re

    1. Initial program 64.0

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

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

      [Start]64.0

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

      metadata-eval [<=]64.0

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

      metadata-eval [<=]64.0

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

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

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

      *-lft-identity [=>]64.0

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

      hypot-def [=>]44.0

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

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

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

      [Start]30.5

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

      unpow2 [=>]30.5

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

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

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

      [Start]38.2

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

      expm1-def [=>]8.2

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

      expm1-log1p [=>]7.4

      \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification4.5

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

Alternatives

Alternative 1
Error7.1
Cost13444
\[\begin{array}{l} \mathbf{if}\;re \leq 7.2 \cdot 10^{-20}:\\ \;\;\;\;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} \]
Alternative 2
Error15.1
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -3.3 \cdot 10^{+132}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.55 \cdot 10^{+85}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -3 \cdot 10^{-20}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 5.1 \cdot 10^{-18}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 3
Error15.6
Cost7248
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -3.3 \cdot 10^{+126}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -2.1 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.55 \cdot 10^{-50}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 8.3 \cdot 10^{-20}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 4
Error23.5
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq -3.3 \cdot 10^{+126} \lor \neg \left(re \leq -2.1 \cdot 10^{+86}\right) \land re \leq -7 \cdot 10^{-52}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 5
Error30.5
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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