?

Average Error: 38.2 → 10.8
Time: 11.0s
Precision: binary64
Cost: 20364

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} t_0 := \sqrt{-re}\\ t_1 := im \cdot \frac{0.5}{t_0}\\ t_2 := 0.5 \cdot \frac{\sqrt{im \cdot \left(0.5 \cdot im\right)}}{\frac{t_0}{\sqrt{2}}}\\ \mathbf{if}\;re \leq -8 \cdot 10^{+90}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq -8.2 \cdot 10^{+24}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1900000000000:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq -1.8 \cdot 10^{-68} \lor \neg \left(re \leq -1.3 \cdot 10^{-73}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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 (sqrt (- re)))
        (t_1 (* im (/ 0.5 t_0)))
        (t_2 (* 0.5 (/ (sqrt (* im (* 0.5 im))) (/ t_0 (sqrt 2.0))))))
   (if (<= re -8e+90)
     t_2
     (if (<= re -8.2e+24)
       t_1
       (if (<= re -1900000000000.0)
         t_2
         (if (or (<= re -1.8e-68) (not (<= re -1.3e-73)))
           (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))
           t_1))))))
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 = sqrt(-re);
	double t_1 = im * (0.5 / t_0);
	double t_2 = 0.5 * (sqrt((im * (0.5 * im))) / (t_0 / sqrt(2.0)));
	double tmp;
	if (re <= -8e+90) {
		tmp = t_2;
	} else if (re <= -8.2e+24) {
		tmp = t_1;
	} else if (re <= -1900000000000.0) {
		tmp = t_2;
	} else if ((re <= -1.8e-68) || !(re <= -1.3e-73)) {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	} else {
		tmp = t_1;
	}
	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 = Math.sqrt(-re);
	double t_1 = im * (0.5 / t_0);
	double t_2 = 0.5 * (Math.sqrt((im * (0.5 * im))) / (t_0 / Math.sqrt(2.0)));
	double tmp;
	if (re <= -8e+90) {
		tmp = t_2;
	} else if (re <= -8.2e+24) {
		tmp = t_1;
	} else if (re <= -1900000000000.0) {
		tmp = t_2;
	} else if ((re <= -1.8e-68) || !(re <= -1.3e-73)) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	} else {
		tmp = t_1;
	}
	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 = math.sqrt(-re)
	t_1 = im * (0.5 / t_0)
	t_2 = 0.5 * (math.sqrt((im * (0.5 * im))) / (t_0 / math.sqrt(2.0)))
	tmp = 0
	if re <= -8e+90:
		tmp = t_2
	elif re <= -8.2e+24:
		tmp = t_1
	elif re <= -1900000000000.0:
		tmp = t_2
	elif (re <= -1.8e-68) or not (re <= -1.3e-73):
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	else:
		tmp = t_1
	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 = sqrt(Float64(-re))
	t_1 = Float64(im * Float64(0.5 / t_0))
	t_2 = Float64(0.5 * Float64(sqrt(Float64(im * Float64(0.5 * im))) / Float64(t_0 / sqrt(2.0))))
	tmp = 0.0
	if (re <= -8e+90)
		tmp = t_2;
	elseif (re <= -8.2e+24)
		tmp = t_1;
	elseif (re <= -1900000000000.0)
		tmp = t_2;
	elseif ((re <= -1.8e-68) || !(re <= -1.3e-73))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	else
		tmp = t_1;
	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 = sqrt(-re);
	t_1 = im * (0.5 / t_0);
	t_2 = 0.5 * (sqrt((im * (0.5 * im))) / (t_0 / sqrt(2.0)));
	tmp = 0.0;
	if (re <= -8e+90)
		tmp = t_2;
	elseif (re <= -8.2e+24)
		tmp = t_1;
	elseif (re <= -1900000000000.0)
		tmp = t_2;
	elseif ((re <= -1.8e-68) || ~((re <= -1.3e-73)))
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	else
		tmp = t_1;
	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[Sqrt[(-re)], $MachinePrecision]}, Block[{t$95$1 = N[(im * N[(0.5 / t$95$0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(0.5 * N[(N[Sqrt[N[(im * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(t$95$0 / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -8e+90], t$95$2, If[LessEqual[re, -8.2e+24], t$95$1, If[LessEqual[re, -1900000000000.0], t$95$2, If[Or[LessEqual[re, -1.8e-68], N[Not[LessEqual[re, -1.3e-73]], $MachinePrecision]], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := \sqrt{-re}\\
t_1 := im \cdot \frac{0.5}{t_0}\\
t_2 := 0.5 \cdot \frac{\sqrt{im \cdot \left(0.5 \cdot im\right)}}{\frac{t_0}{\sqrt{2}}}\\
\mathbf{if}\;re \leq -8 \cdot 10^{+90}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;re \leq -8.2 \cdot 10^{+24}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -1900000000000:\\
\;\;\;\;t_2\\

\mathbf{elif}\;re \leq -1.8 \cdot 10^{-68} \lor \neg \left(re \leq -1.3 \cdot 10^{-73}\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.2
Target33.0
Herbie10.8
\[\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 < -7.99999999999999973e90 or -8.2000000000000002e24 < re < -1.9e12

    1. Initial program 60.1

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Taylor expanded in re around -inf 31.9

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

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

      [Start]31.9

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

      associate-*r/ [=>]31.9

      \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{-0.5 \cdot {im}^{2}}{re}}} \]

      unpow2 [=>]31.9

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

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

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

      [Start]23.5

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

      *-commutative [=>]23.5

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

      associate-/l* [=>]23.5

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

      associate-*l* [=>]23.5

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

    if -7.99999999999999973e90 < re < -8.2000000000000002e24 or -1.80000000000000004e-68 < re < -1.3e-73

    1. Initial program 49.7

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Taylor expanded in re around -inf 40.8

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

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

      [Start]40.8

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

      associate-*r/ [=>]40.8

      \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{-0.5 \cdot {im}^{2}}{re}}} \]

      unpow2 [=>]40.8

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

      \[\leadsto 0.5 \cdot \color{blue}{{\left({\left(\frac{-1 \cdot \left(im \cdot im\right)}{re}\right)}^{0.25}\right)}^{2}} \]
    5. Applied egg-rr42.1

      \[\leadsto \color{blue}{\frac{0.5 \cdot im}{\sqrt{-re}}} \]
    6. Simplified42.1

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

      [Start]42.1

      \[ \frac{0.5 \cdot im}{\sqrt{-re}} \]

      associate-/l* [=>]42.3

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

      associate-/r/ [=>]42.1

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

    if -1.9e12 < re < -1.80000000000000004e-68 or -1.3e-73 < re

    1. Initial program 31.9

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

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

      [Start]31.9

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

      +-commutative [=>]31.9

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

      hypot-def [=>]5.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -8 \cdot 10^{+90}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{im \cdot \left(0.5 \cdot im\right)}}{\frac{\sqrt{-re}}{\sqrt{2}}}\\ \mathbf{elif}\;re \leq -8.2 \cdot 10^{+24}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -1900000000000:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{im \cdot \left(0.5 \cdot im\right)}}{\frac{\sqrt{-re}}{\sqrt{2}}}\\ \mathbf{elif}\;re \leq -1.8 \cdot 10^{-68} \lor \neg \left(re \leq -1.3 \cdot 10^{-73}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{-re}}\\ \end{array} \]

Alternatives

Alternative 1
Error10.0
Cost26884
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;\frac{0.5 \cdot im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Error25.6
Cost7509
\[\begin{array}{l} t_0 := \sqrt{-re}\\ \mathbf{if}\;im \leq -1.56 \cdot 10^{-93}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{-283}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 8.5 \cdot 10^{-140}:\\ \;\;\;\;\frac{0.5 \cdot im}{t_0}\\ \mathbf{elif}\;im \leq 9.2 \cdot 10^{-115} \lor \neg \left(im \leq 9.6 \cdot 10^{-87}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{t_0}\\ \end{array} \]
Alternative 3
Error25.3
Cost7509
\[\begin{array}{l} t_0 := \sqrt{-re}\\ \mathbf{if}\;im \leq -3.45 \cdot 10^{-87}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 6.5 \cdot 10^{-283}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 4.1 \cdot 10^{-140}:\\ \;\;\;\;\frac{0.5 \cdot im}{t_0}\\ \mathbf{elif}\;im \leq 10^{-114} \lor \neg \left(im \leq 9.5 \cdot 10^{-89}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{t_0}\\ \end{array} \]
Alternative 4
Error25.9
Cost7445
\[\begin{array}{l} \mathbf{if}\;im \leq -6 \cdot 10^{-92}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 6.5 \cdot 10^{-283}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 7.2 \cdot 10^{-141} \lor \neg \left(im \leq 1.1 \cdot 10^{-114}\right) \land im \leq 7.6 \cdot 10^{-89}:\\ \;\;\;\;im \cdot \frac{0.5}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 5
Error25.9
Cost7445
\[\begin{array}{l} t_0 := \sqrt{-re}\\ \mathbf{if}\;im \leq -1.65 \cdot 10^{-90}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 5.6 \cdot 10^{-283}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 2.1 \cdot 10^{-140}:\\ \;\;\;\;\frac{0.5 \cdot im}{t_0}\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{-114} \lor \neg \left(im \leq 8 \cdot 10^{-89}\right):\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;im \cdot \frac{0.5}{t_0}\\ \end{array} \]
Alternative 6
Error25.8
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -7 \cdot 10^{-89}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{-153}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 7
Error36.7
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 2.1 \cdot 10^{-45}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 8
Error47.3
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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