?

Average Error: 38.1 → 7.7
Time: 11.1s
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 7.5 \cdot 10^{-111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4.7 \cdot 10^{-82}:\\ \;\;\;\;\frac{\frac{0.5 \cdot im}{{re}^{0.25}}}{{re}^{0.25}}\\ \mathbf{elif}\;re \leq 8.8 \cdot 10^{-16}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \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 7.5e-111)
     t_0
     (if (<= re 4.7e-82)
       (/ (/ (* 0.5 im) (pow re 0.25)) (pow re 0.25))
       (if (<= re 8.8e-16) 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 <= 7.5e-111) {
		tmp = t_0;
	} else if (re <= 4.7e-82) {
		tmp = ((0.5 * im) / pow(re, 0.25)) / pow(re, 0.25);
	} else if (re <= 8.8e-16) {
		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 <= 7.5e-111) {
		tmp = t_0;
	} else if (re <= 4.7e-82) {
		tmp = ((0.5 * im) / Math.pow(re, 0.25)) / Math.pow(re, 0.25);
	} else if (re <= 8.8e-16) {
		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 <= 7.5e-111:
		tmp = t_0
	elif re <= 4.7e-82:
		tmp = ((0.5 * im) / math.pow(re, 0.25)) / math.pow(re, 0.25)
	elif re <= 8.8e-16:
		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 <= 7.5e-111)
		tmp = t_0;
	elseif (re <= 4.7e-82)
		tmp = Float64(Float64(Float64(0.5 * im) / (re ^ 0.25)) / (re ^ 0.25));
	elseif (re <= 8.8e-16)
		tmp = t_0;
	else
		tmp = Float64(Float64(0.5 * 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 <= 7.5e-111)
		tmp = t_0;
	elseif (re <= 4.7e-82)
		tmp = ((0.5 * im) / (re ^ 0.25)) / (re ^ 0.25);
	elseif (re <= 8.8e-16)
		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, 7.5e-111], t$95$0, If[LessEqual[re, 4.7e-82], N[(N[(N[(0.5 * im), $MachinePrecision] / N[Power[re, 0.25], $MachinePrecision]), $MachinePrecision] / N[Power[re, 0.25], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8.8e-16], t$95$0, N[(N[(0.5 * im), $MachinePrecision] * N[Power[re, -0.5], $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 7.5 \cdot 10^{-111}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;re \leq 4.7 \cdot 10^{-82}:\\
\;\;\;\;\frac{\frac{0.5 \cdot im}{{re}^{0.25}}}{{re}^{0.25}}\\

\mathbf{elif}\;re \leq 8.8 \cdot 10^{-16}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\


\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 < 7.49999999999999965e-111 or 4.7000000000000001e-82 < re < 8.80000000000000001e-16

    1. Initial program 31.1

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

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

      [Start]31.1

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

      metadata-eval [<=]31.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 [<=]31.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* [<=]31.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 [=>]31.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 [=>]31.1

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

      hypot-def [=>]3.8

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

    if 7.49999999999999965e-111 < re < 4.7000000000000001e-82

    1. Initial program 39.2

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

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

      [Start]39.2

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

      metadata-eval [<=]39.2

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

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

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

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

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

      hypot-def [=>]22.2

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

      \[\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. Applied egg-rr61.0

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

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

      [Start]61.0

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

      metadata-eval [<=]61.0

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

      sub-neg [<=]61.0

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

      expm1-def [=>]38.5

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

      expm1-log1p [=>]38.5

      \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    6. Applied egg-rr38.6

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

    if 8.80000000000000001e-16 < re

    1. Initial program 56.8

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

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

      [Start]56.8

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

      metadata-eval [<=]56.8

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

      metadata-eval [<=]56.8

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

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

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

      *-lft-identity [=>]56.8

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

      hypot-def [=>]38.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 15.1

      \[\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. Applied egg-rr45.5

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

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

      [Start]45.5

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

      metadata-eval [<=]45.5

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

      sub-neg [<=]45.5

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

      expm1-def [=>]15.2

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

      expm1-log1p [=>]14.7

      \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}} \]
    6. Applied egg-rr17.9

      \[\leadsto \color{blue}{e^{\log \left(\frac{im}{\sqrt{re}} \cdot 0.5\right)}} \]
    7. Applied egg-rr14.7

      \[\leadsto \color{blue}{im \cdot \left({re}^{-0.5} \cdot 0.5\right)} \]
    8. Simplified14.7

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

      [Start]14.7

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

      *-commutative [=>]14.7

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

      associate-*l* [<=]14.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 7.5 \cdot 10^{-111}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{elif}\;re \leq 4.7 \cdot 10^{-82}:\\ \;\;\;\;\frac{\frac{0.5 \cdot im}{{re}^{0.25}}}{{re}^{0.25}}\\ \mathbf{elif}\;re \leq 8.8 \cdot 10^{-16}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \end{array} \]

Alternatives

Alternative 1
Error7.5
Cost13708
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\ \mathbf{if}\;re \leq 7.6 \cdot 10^{-111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-84}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 4.4 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \end{array} \]
Alternative 2
Error16.1
Cost7576
\[\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 -4.5 \cdot 10^{+29}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.12 \cdot 10^{-20}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.56 \cdot 10^{-79}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 7.6 \cdot 10^{-111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.26 \cdot 10^{-83}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 1.22 \cdot 10^{-14}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \end{array} \]
Alternative 3
Error15.3
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\ \mathbf{if}\;re \leq -8.5 \cdot 10^{+31}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\ \mathbf{elif}\;re \leq 7.6 \cdot 10^{-111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 6.4 \cdot 10^{-80}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 2.8 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \end{array} \]
Alternative 4
Error23.0
Cost7180
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq 6 \cdot 10^{-111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4.1 \cdot 10^{-83}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 6 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\ \end{array} \]
Alternative 5
Error23.0
Cost7180
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq 7.6 \cdot 10^{-111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 3.2 \cdot 10^{-85}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \mathbf{elif}\;re \leq 9.6 \cdot 10^{-16}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\ \end{array} \]
Alternative 6
Error22.9
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 7.6 \cdot 10^{-111} \lor \neg \left(re \leq 5.8 \cdot 10^{-86}\right) \land re \leq 3.5 \cdot 10^{-14}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\ \end{array} \]
Alternative 7
Error23.0
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 6 \cdot 10^{-111} \lor \neg \left(re \leq 1.3 \cdot 10^{-85}\right) \land re \leq 1.6 \cdot 10^{-14}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\ \end{array} \]
Alternative 8
Error30.9
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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