Average Error: 38.2 → 10.3
Time: 8.7s
Precision: binary64
Cost: 26756
\[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(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{if}\;re \leq -1.072112859749039 \cdot 10^{+75}:\\ \;\;\;\;0.5 \cdot \left({\left({\left(im \cdot \left(0.5 \cdot im\right)\right)}^{0.16666666666666666} \cdot {\left(\frac{-1}{re}\right)}^{0.16666666666666666}\right)}^{3} \cdot \sqrt{2}\right)\\ \mathbf{elif}\;re \leq -18.971660196454845:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -3.4568485425480756 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;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 (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
   (if (<= re -1.072112859749039e+75)
     (*
      0.5
      (*
       (pow
        (*
         (pow (* im (* 0.5 im)) 0.16666666666666666)
         (pow (/ -1.0 re) 0.16666666666666666))
        3.0)
       (sqrt 2.0)))
     (if (<= re -18.971660196454845)
       t_0
       (if (<= re -3.4568485425480756e-15)
         (* 0.5 (/ im (sqrt (- re))))
         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 = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	double tmp;
	if (re <= -1.072112859749039e+75) {
		tmp = 0.5 * (pow((pow((im * (0.5 * im)), 0.16666666666666666) * pow((-1.0 / re), 0.16666666666666666)), 3.0) * sqrt(2.0));
	} else if (re <= -18.971660196454845) {
		tmp = t_0;
	} else if (re <= -3.4568485425480756e-15) {
		tmp = 0.5 * (im / sqrt(-re));
	} else {
		tmp = t_0;
	}
	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 * (re + Math.hypot(re, im))));
	double tmp;
	if (re <= -1.072112859749039e+75) {
		tmp = 0.5 * (Math.pow((Math.pow((im * (0.5 * im)), 0.16666666666666666) * Math.pow((-1.0 / re), 0.16666666666666666)), 3.0) * Math.sqrt(2.0));
	} else if (re <= -18.971660196454845) {
		tmp = t_0;
	} else if (re <= -3.4568485425480756e-15) {
		tmp = 0.5 * (im / Math.sqrt(-re));
	} else {
		tmp = t_0;
	}
	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 * (re + math.hypot(re, im))))
	tmp = 0
	if re <= -1.072112859749039e+75:
		tmp = 0.5 * (math.pow((math.pow((im * (0.5 * im)), 0.16666666666666666) * math.pow((-1.0 / re), 0.16666666666666666)), 3.0) * math.sqrt(2.0))
	elif re <= -18.971660196454845:
		tmp = t_0
	elif re <= -3.4568485425480756e-15:
		tmp = 0.5 * (im / math.sqrt(-re))
	else:
		tmp = t_0
	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(re + hypot(re, im)))))
	tmp = 0.0
	if (re <= -1.072112859749039e+75)
		tmp = Float64(0.5 * Float64((Float64((Float64(im * Float64(0.5 * im)) ^ 0.16666666666666666) * (Float64(-1.0 / re) ^ 0.16666666666666666)) ^ 3.0) * sqrt(2.0)));
	elseif (re <= -18.971660196454845)
		tmp = t_0;
	elseif (re <= -3.4568485425480756e-15)
		tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re))));
	else
		tmp = t_0;
	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 * (re + hypot(re, im))));
	tmp = 0.0;
	if (re <= -1.072112859749039e+75)
		tmp = 0.5 * (((((im * (0.5 * im)) ^ 0.16666666666666666) * ((-1.0 / re) ^ 0.16666666666666666)) ^ 3.0) * sqrt(2.0));
	elseif (re <= -18.971660196454845)
		tmp = t_0;
	elseif (re <= -3.4568485425480756e-15)
		tmp = 0.5 * (im / sqrt(-re));
	else
		tmp = t_0;
	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[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1.072112859749039e+75], N[(0.5 * N[(N[Power[N[(N[Power[N[(im * N[(0.5 * im), $MachinePrecision]), $MachinePrecision], 0.16666666666666666], $MachinePrecision] * N[Power[N[(-1.0 / re), $MachinePrecision], 0.16666666666666666], $MachinePrecision]), $MachinePrecision], 3.0], $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -18.971660196454845], t$95$0, If[LessEqual[re, -3.4568485425480756e-15], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$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(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\mathbf{if}\;re \leq -1.072112859749039 \cdot 10^{+75}:\\
\;\;\;\;0.5 \cdot \left({\left({\left(im \cdot \left(0.5 \cdot im\right)\right)}^{0.16666666666666666} \cdot {\left(\frac{-1}{re}\right)}^{0.16666666666666666}\right)}^{3} \cdot \sqrt{2}\right)\\

\mathbf{elif}\;re \leq -18.971660196454845:\\
\;\;\;\;t_0\\

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

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


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.2
Target33.1
Herbie10.3
\[\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 3 regimes
  2. if re < -1.07211285974903893e75

    1. Initial program 60.2

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

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

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

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

      \[\leadsto 0.5 \cdot \left({\color{blue}{\left(e^{0.16666666666666666 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(0.5 \cdot {im}^{2}\right)\right)}\right)}}^{3} \cdot \sqrt{2}\right) \]
    6. Simplified24.8

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

    if -1.07211285974903893e75 < re < -18.9716601964548452 or -3.4568485425480756e-15 < re

    1. Initial program 33.2

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

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

    if -18.9716601964548452 < re < -3.4568485425480756e-15

    1. Initial program 44.2

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

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

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    5. Simplified45.4

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot \left(-im\right)}{re}}} \]
    6. Applied egg-rr45.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.072112859749039 \cdot 10^{+75}:\\ \;\;\;\;0.5 \cdot \left({\left({\left(im \cdot \left(0.5 \cdot im\right)\right)}^{0.16666666666666666} \cdot {\left(\frac{-1}{re}\right)}^{0.16666666666666666}\right)}^{3} \cdot \sqrt{2}\right)\\ \mathbf{elif}\;re \leq -18.971660196454845:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{elif}\;re \leq -3.4568485425480756 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error10.7
Cost13708
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{if}\;re \leq -1.072112859749039 \cdot 10^{+75}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im}{re} \cdot \left(-im\right)}\\ \mathbf{elif}\;re \leq -18.971660196454845:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -3.4568485425480756 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error26.0
Cost13648
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -5.743918235544548 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.135583820333753 \cdot 10^{-215}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.062116750577376 \cdot 10^{-170}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 1.097811499829704 \cdot 10^{-87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{im}\right)\\ \end{array} \]
Alternative 3
Error25.8
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -5.743918235544548 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.135583820333753 \cdot 10^{-215}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.062116750577376 \cdot 10^{-170}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 1.097811499829704 \cdot 10^{-87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error25.6
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -5.743918235544548 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.135583820333753 \cdot 10^{-215}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.062116750577376 \cdot 10^{-170}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 1.097811499829704 \cdot 10^{-87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Error26.1
Cost7248
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -5.743918235544548 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.135583820333753 \cdot 10^{-215}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 3.062116750577376 \cdot 10^{-170}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 1.097811499829704 \cdot 10^{-87}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Error29.5
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -1.6212435483716804 \cdot 10^{-246}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 4.979672736915039 \cdot 10^{-281}:\\ \;\;\;\;\sqrt{im \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 7
Error25.9
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -5.743918235544548 \cdot 10^{-40}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.097811499829704 \cdot 10^{-87}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 8
Error44.5
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 4.979672736915039 \cdot 10^{-281}:\\ \;\;\;\;\sqrt{im \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 9
Error58.2
Cost6596
\[\begin{array}{l} \mathbf{if}\;re \leq -5.805898195120208 \cdot 10^{+47}:\\ \;\;\;\;0.5 \cdot \left(4 \cdot \left(im \cdot im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left|im\right|\\ \end{array} \]
Alternative 10
Error59.6
Cost448
\[0.5 \cdot \left(4 \cdot \left(im \cdot im\right)\right) \]
Alternative 11
Error59.7
Cost320
\[-1 + \left(im + 1\right) \]
Alternative 12
Error61.3
Cost64
\[im \]

Error

Reproduce

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