?

Average Error: 38.6 → 8.4
Time: 9.0s
Precision: binary64
Cost: 13708

?

\[im > 0\]
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \]
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{if}\;re \leq 1.28 \cdot 10^{-114}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.95 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{im}}{\sqrt{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq 4.2 \cdot 10^{+25}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-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 (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))))
   (if (<= re 1.28e-114)
     t_0
     (if (<= re 1.95e-33)
       (* 0.5 (/ (sqrt im) (sqrt (/ re im))))
       (if (<= re 4.2e+25) t_0 (* 0.5 (* im (pow re -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 = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	double tmp;
	if (re <= 1.28e-114) {
		tmp = t_0;
	} else if (re <= 1.95e-33) {
		tmp = 0.5 * (sqrt(im) / sqrt((re / im)));
	} else if (re <= 4.2e+25) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im * pow(re, -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 = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
	double tmp;
	if (re <= 1.28e-114) {
		tmp = t_0;
	} else if (re <= 1.95e-33) {
		tmp = 0.5 * (Math.sqrt(im) / Math.sqrt((re / im)));
	} else if (re <= 4.2e+25) {
		tmp = t_0;
	} else {
		tmp = 0.5 * (im * Math.pow(re, -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 = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re)))
	tmp = 0
	if re <= 1.28e-114:
		tmp = t_0
	elif re <= 1.95e-33:
		tmp = 0.5 * (math.sqrt(im) / math.sqrt((re / im)))
	elif re <= 4.2e+25:
		tmp = t_0
	else:
		tmp = 0.5 * (im * math.pow(re, -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(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re))))
	tmp = 0.0
	if (re <= 1.28e-114)
		tmp = t_0;
	elseif (re <= 1.95e-33)
		tmp = Float64(0.5 * Float64(sqrt(im) / sqrt(Float64(re / im))));
	elseif (re <= 4.2e+25)
		tmp = t_0;
	else
		tmp = Float64(0.5 * Float64(im * (re ^ -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 = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
	tmp = 0.0;
	if (re <= 1.28e-114)
		tmp = t_0;
	elseif (re <= 1.95e-33)
		tmp = 0.5 * (sqrt(im) / sqrt((re / im)));
	elseif (re <= 4.2e+25)
		tmp = t_0;
	else
		tmp = 0.5 * (im * (re ^ -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[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, 1.28e-114], t$95$0, If[LessEqual[re, 1.95e-33], N[(0.5 * N[(N[Sqrt[im], $MachinePrecision] / N[Sqrt[N[(re / im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.2e+25], t$95$0, N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{if}\;re \leq 1.28 \cdot 10^{-114}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 1.95 \cdot 10^{-33}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{im}}{\sqrt{\frac{re}{im}}}\\

\mathbf{elif}\;re \leq 4.2 \cdot 10^{+25}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-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 3 regimes
  2. if re < 1.28e-114 or 1.94999999999999987e-33 < re < 4.1999999999999998e25

    1. Initial program 32.4

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

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

      [Start]32.4

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

      metadata-eval [<=]32.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 [<=]32.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* [<=]32.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 [=>]32.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 [=>]32.4

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

      hypot-def [=>]4.0

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

    if 1.28e-114 < re < 1.94999999999999987e-33

    1. Initial program 37.3

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

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

      [Start]37.3

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

      metadata-eval [<=]37.3

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

      metadata-eval [<=]37.3

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

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

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

      *-lft-identity [=>]37.3

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

      hypot-def [=>]22.7

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

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

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

      [Start]53.4

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

      unpow2 [=>]53.4

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

      associate-/l* [=>]49.6

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

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

    if 4.1999999999999998e25 < re

    1. Initial program 58.7

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

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

      [Start]58.7

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

      metadata-eval [<=]58.7

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

      metadata-eval [<=]58.7

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

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

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

      *-lft-identity [=>]58.7

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

      hypot-def [=>]40.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 34.1

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

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

      [Start]34.1

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

      unpow2 [=>]34.1

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

      associate-/l* [=>]29.6

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

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

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

      [Start]44.0

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

      expm1-def [=>]13.7

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

      expm1-log1p [=>]13.2

      \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    7. Applied egg-rr13.2

      \[\leadsto 0.5 \cdot \color{blue}{\left({re}^{-0.5} \cdot im\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification8.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 1.28 \cdot 10^{-114}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{elif}\;re \leq 1.95 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{im}}{\sqrt{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq 4.2 \cdot 10^{+25}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error15.5
Cost13644
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -4.9 \cdot 10^{+47}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 3.5 \cdot 10^{-113}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.3 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{im}}{\sqrt{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq 4.5 \cdot 10^{+26}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 2
Error15.6
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -5.8 \cdot 10^{+47}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.45 \cdot 10^{-113}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4.1 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 1.65 \cdot 10^{+25}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 3
Error16.5
Cost7312
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -1.95 \cdot 10^{-103}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 2.2 \cdot 10^{-101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 6.1 \cdot 10^{+25}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 4
Error24.1
Cost7180
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq 2.2 \cdot 10^{-101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.55 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 3.6 \cdot 10^{+26}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 5
Error24.1
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 1.9 \cdot 10^{-101} \lor \neg \left(re \leq 1.6 \cdot 10^{-33}\right) \land re \leq 1.4 \cdot 10^{+24}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 6
Error24.1
Cost7116
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq 9 \cdot 10^{-102}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-33}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{+23}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\ \end{array} \]
Alternative 7
Error31.1
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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