?

Average Error: 38.5 → 10.9
Time: 1.3min
Precision: binary64
Cost: 13836

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} t_0 := \mathsf{hypot}\left(re, im\right) + re\\ \mathbf{if}\;re \leq -1.15 \cdot 10^{+98}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \begin{array}{l} \mathbf{if}\;im \ne 0:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;im \ne 0:\\ \;\;\;\;\frac{im}{\frac{re}{\frac{im}{-2}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-0.5}{\frac{re}{{im}^{2}}}\\ \end{array}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot t_0}\\ \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 (+ (hypot re im) re)))
   (if (<= re -1.15e+98)
     (*
      0.5
      (sqrt
       (*
        2.0
        (if (!= im 0.0)
          (if (!= im 0.0)
            (/ im (/ re (/ im -2.0)))
            (/ -0.5 (/ re (pow im 2.0))))
          t_0))))
     (* 0.5 (sqrt (* 2.0 t_0))))))
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 = hypot(re, im) + re;
	double tmp_3;
	if (re <= -1.15e+98) {
		double tmp_5;
		if (im != 0.0) {
			double tmp_6;
			if (im != 0.0) {
				tmp_6 = im / (re / (im / -2.0));
			} else {
				tmp_6 = -0.5 / (re / pow(im, 2.0));
			}
			tmp_5 = tmp_6;
		} else {
			tmp_5 = t_0;
		}
		tmp_3 = 0.5 * sqrt((2.0 * tmp_5));
	} else {
		tmp_3 = 0.5 * sqrt((2.0 * t_0));
	}
	return tmp_3;
}
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 = Math.hypot(re, im) + re;
	double tmp_3;
	if (re <= -1.15e+98) {
		double tmp_5;
		if (im != 0.0) {
			double tmp_6;
			if (im != 0.0) {
				tmp_6 = im / (re / (im / -2.0));
			} else {
				tmp_6 = -0.5 / (re / Math.pow(im, 2.0));
			}
			tmp_5 = tmp_6;
		} else {
			tmp_5 = t_0;
		}
		tmp_3 = 0.5 * Math.sqrt((2.0 * tmp_5));
	} else {
		tmp_3 = 0.5 * Math.sqrt((2.0 * t_0));
	}
	return tmp_3;
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
def code(re, im):
	t_0 = math.hypot(re, im) + re
	tmp_3 = 0
	if re <= -1.15e+98:
		tmp_5 = 0
		if im != 0.0:
			tmp_6 = 0
			if im != 0.0:
				tmp_6 = im / (re / (im / -2.0))
			else:
				tmp_6 = -0.5 / (re / math.pow(im, 2.0))
			tmp_5 = tmp_6
		else:
			tmp_5 = t_0
		tmp_3 = 0.5 * math.sqrt((2.0 * tmp_5))
	else:
		tmp_3 = 0.5 * math.sqrt((2.0 * t_0))
	return tmp_3
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(hypot(re, im) + re)
	tmp_3 = 0.0
	if (re <= -1.15e+98)
		tmp_5 = 0.0
		if (im != 0.0)
			tmp_6 = 0.0
			if (im != 0.0)
				tmp_6 = Float64(im / Float64(re / Float64(im / -2.0)));
			else
				tmp_6 = Float64(-0.5 / Float64(re / (im ^ 2.0)));
			end
			tmp_5 = tmp_6;
		else
			tmp_5 = t_0;
		end
		tmp_3 = Float64(0.5 * sqrt(Float64(2.0 * tmp_5)));
	else
		tmp_3 = Float64(0.5 * sqrt(Float64(2.0 * t_0)));
	end
	return tmp_3
end
function tmp = code(re, im)
	tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
end
function tmp_8 = code(re, im)
	t_0 = hypot(re, im) + re;
	tmp_4 = 0.0;
	if (re <= -1.15e+98)
		tmp_6 = 0.0;
		if (im ~= 0.0)
			tmp_7 = 0.0;
			if (im ~= 0.0)
				tmp_7 = im / (re / (im / -2.0));
			else
				tmp_7 = -0.5 / (re / (im ^ 2.0));
			end
			tmp_6 = tmp_7;
		else
			tmp_6 = t_0;
		end
		tmp_4 = 0.5 * sqrt((2.0 * tmp_6));
	else
		tmp_4 = 0.5 * sqrt((2.0 * t_0));
	end
	tmp_8 = tmp_4;
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[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] + re), $MachinePrecision]}, If[LessEqual[re, -1.15e+98], N[(0.5 * N[Sqrt[N[(2.0 * If[Unequal[im, 0.0], If[Unequal[im, 0.0], N[(im / N[(re / N[(im / -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-0.5 / N[(re / N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], t$95$0]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := \mathsf{hypot}\left(re, im\right) + re\\
\mathbf{if}\;re \leq -1.15 \cdot 10^{+98}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \begin{array}{l}
\mathbf{if}\;im \ne 0:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;im \ne 0:\\
\;\;\;\;\frac{im}{\frac{re}{\frac{im}{-2}}}\\

\mathbf{else}:\\
\;\;\;\;\frac{-0.5}{\frac{re}{{im}^{2}}}\\


\end{array}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}}\\

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


\end{array}

Error?

Target

Original38.5
Target33.8
Herbie10.9
\[\begin{array}{l} \mathbf{if}\;re < 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if re < -1.15000000000000007e98

    1. Initial program 61.3

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;\mathsf{hypot}\left(re, im\right) + re \ne 0:\\ \;\;\;\;\frac{1}{\frac{1}{\mathsf{hypot}\left(re, im\right) + re}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(re, im\right) + re\\ } \end{array}}} \]
    4. Taylor expanded in im around inf 39.1

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \begin{array}{l} \mathbf{if}\;\color{blue}{im} \ne 0:\\ \;\;\;\;\frac{1}{\frac{1}{\mathsf{hypot}\left(re, im\right) + re}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(re, im\right) + re\\ \end{array}} \]
    5. Taylor expanded in re around -inf 34.4

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \begin{array}{l} \mathbf{if}\;im \ne 0:\\ \;\;\;\;\frac{1}{\color{blue}{-2 \cdot \frac{re}{{im}^{2}}}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(re, im\right) + re\\ \end{array}} \]
    6. Simplified34.4

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \begin{array}{l} \mathbf{if}\;im \ne 0:\\ \;\;\;\;\frac{1}{\color{blue}{\frac{-2 \cdot re}{{im}^{2}}}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(re, im\right) + re\\ \end{array}} \]
      Proof
    7. Applied egg-rr27.6

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \begin{array}{l} \mathbf{if}\;im \ne 0:\\ \;\;\;\;\color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;im \ne 0:\\ \;\;\;\;\frac{1}{\frac{-2 \cdot re}{im}} \cdot im\\ \mathbf{else}:\\ \;\;\;\;\frac{-0.5}{\frac{re}{{im}^{2}}}\\ } \end{array}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(re, im\right) + re\\ \end{array}} \]
    8. Simplified27.6

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

    if -1.15000000000000007e98 < re

    1. Initial program 33.9

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) + re\right)}} \]
      Proof
  3. Recombined 2 regimes into one program.

Alternatives

Alternative 1
Error11.1
Cost13576
\[\begin{array}{l} t_0 := \mathsf{hypot}\left(re, im\right) + re\\ \mathbf{if}\;re \leq -1.85 \cdot 10^{+98}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \begin{array}{l} \mathbf{if}\;im \ne 0:\\ \;\;\;\;\frac{1}{\frac{\frac{-2}{im} \cdot re}{im}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot t_0}\\ \end{array} \]
Alternative 2
Error11.9
Cost13444
\[\begin{array}{l} \mathbf{if}\;re \leq -5.4 \cdot 10^{+101}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{-0.5 \cdot \left(im \cdot im\right)}{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) + re\right)}\\ \end{array} \]
Alternative 3
Error26.3
Cost7640
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{if}\;im \leq -7.2 \cdot 10^{+102}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -5.2 \cdot 10^{+69}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{-0.5 \cdot \left(im \cdot im\right)}{re}}\\ \mathbf{elif}\;im \leq -2000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -2.9 \cdot 10^{-15}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -2.26 \cdot 10^{-101}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 3.5 \cdot 10^{-190}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error26.6
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{if}\;im \leq -1.9 \cdot 10^{+19}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.55 \cdot 10^{-15}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -3 \cdot 10^{-101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 1.08 \cdot 10^{-194}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 5
Error26.0
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{if}\;im \leq -1.9 \cdot 10^{+19}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -2.8 \cdot 10^{-15}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -5.6 \cdot 10^{-100}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.05 \cdot 10^{-189}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 6
Error25.6
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{if}\;im \leq -2000000000:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -1.5 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -3.15 \cdot 10^{-101}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{-190}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 7
Error44.9
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 3.45 \cdot 10^{-273}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot re}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 8
Error30.6
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq -1 \cdot 10^{-309}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 9
Error47.1
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

herbie shell --seed 2023033 
(FPCore (re im)
  :name "math.sqrt on complex, real part"
  :precision binary64

  :herbie-target
  (if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))