?

Average Error: 39.1 → 11.3
Time: 10.3s
Precision: binary64
Cost: 45956

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -520000000000:\\ \;\;\;\;0.5 \cdot {\left({\left(\sqrt{2}\right)}^{0.3333333333333333} \cdot e^{0.16666666666666666 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(0.5 \cdot {im}^{2}\right)\right)}\right)}^{3}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (if (<= re -520000000000.0)
   (*
    0.5
    (pow
     (*
      (pow (sqrt 2.0) 0.3333333333333333)
      (exp
       (*
        0.16666666666666666
        (+ (log (/ -1.0 re)) (log (* 0.5 (pow im 2.0)))))))
     3.0))
   (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
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 tmp;
	if (re <= -520000000000.0) {
		tmp = 0.5 * pow((pow(sqrt(2.0), 0.3333333333333333) * exp((0.16666666666666666 * (log((-1.0 / re)) + log((0.5 * pow(im, 2.0))))))), 3.0);
	} else {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	}
	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 tmp;
	if (re <= -520000000000.0) {
		tmp = 0.5 * Math.pow((Math.pow(Math.sqrt(2.0), 0.3333333333333333) * Math.exp((0.16666666666666666 * (Math.log((-1.0 / re)) + Math.log((0.5 * Math.pow(im, 2.0))))))), 3.0);
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	}
	return tmp;
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
def code(re, im):
	tmp = 0
	if re <= -520000000000.0:
		tmp = 0.5 * math.pow((math.pow(math.sqrt(2.0), 0.3333333333333333) * math.exp((0.16666666666666666 * (math.log((-1.0 / re)) + math.log((0.5 * math.pow(im, 2.0))))))), 3.0)
	else:
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	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)
	tmp = 0.0
	if (re <= -520000000000.0)
		tmp = Float64(0.5 * (Float64((sqrt(2.0) ^ 0.3333333333333333) * exp(Float64(0.16666666666666666 * Float64(log(Float64(-1.0 / re)) + log(Float64(0.5 * (im ^ 2.0))))))) ^ 3.0));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	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)
	tmp = 0.0;
	if (re <= -520000000000.0)
		tmp = 0.5 * (((sqrt(2.0) ^ 0.3333333333333333) * exp((0.16666666666666666 * (log((-1.0 / re)) + log((0.5 * (im ^ 2.0))))))) ^ 3.0);
	else
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	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_] := If[LessEqual[re, -520000000000.0], N[(0.5 * N[Power[N[(N[Power[N[Sqrt[2.0], $MachinePrecision], 0.3333333333333333], $MachinePrecision] * N[Exp[N[(0.16666666666666666 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq -520000000000:\\
\;\;\;\;0.5 \cdot {\left({\left(\sqrt{2}\right)}^{0.3333333333333333} \cdot e^{0.16666666666666666 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(0.5 \cdot {im}^{2}\right)\right)}\right)}^{3}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original39.1
Target34.2
Herbie11.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 2 regimes
  2. if re < -5.2e11

    1. Initial program 57.3

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

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

      [Start]57.3

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

      +-commutative [=>]57.3

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

      hypot-def [=>]38.2

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

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

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

    if -5.2e11 < re

    1. Initial program 33.3

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

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

      [Start]33.3

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

      +-commutative [=>]33.3

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

      hypot-def [=>]5.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -520000000000:\\ \;\;\;\;0.5 \cdot {\left({\left(\sqrt{2}\right)}^{0.3333333333333333} \cdot e^{0.16666666666666666 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(0.5 \cdot {im}^{2}\right)\right)}\right)}^{3}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error9.7
Cost26884
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Error27.0
Cost7837
\[\begin{array}{l} t_0 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ t_1 := \sqrt{-re}\\ \mathbf{if}\;im \leq -1.05 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -1.48 \cdot 10^{-266}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{-263}:\\ \;\;\;\;0.5 \cdot \frac{im}{t_1}\\ \mathbf{elif}\;im \leq 2.85 \cdot 10^{-226}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.9 \cdot 10^{-157} \lor \neg \left(im \leq 1.85 \cdot 10^{-84}\right) \land im \leq 4.3 \cdot 10^{-69}:\\ \;\;\;\;0.5 \cdot \frac{1}{\frac{t_1}{im}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Error26.9
Cost7837
\[\begin{array}{l} t_0 := \sqrt{-re}\\ \mathbf{if}\;im \leq -1.05 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -6.8 \cdot 10^{-271}:\\ \;\;\;\;0.5 \cdot {\left(im \cdot \frac{im}{re} + 2 \cdot \left(re \cdot 2\right)\right)}^{0.5}\\ \mathbf{elif}\;im \leq 1.4 \cdot 10^{-263}:\\ \;\;\;\;0.5 \cdot \frac{im}{t_0}\\ \mathbf{elif}\;im \leq 3.8 \cdot 10^{-227}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{-156} \lor \neg \left(im \leq 5.5 \cdot 10^{-88}\right) \land im \leq 2.2 \cdot 10^{-69}:\\ \;\;\;\;0.5 \cdot \frac{1}{\frac{t_0}{im}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error27.6
Cost7773
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_1 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;im \leq -5.6 \cdot 10^{+51}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -3.7 \cdot 10^{+25}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -1.4 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6.8 \cdot 10^{-271}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{-157} \lor \neg \left(im \leq 6 \cdot 10^{-80}\right) \land im \leq 2.25 \cdot 10^{-69}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Error28.3
Cost7708
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_1 := 0.5 \cdot \frac{im}{\sqrt{-re}}\\ t_2 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;im \leq -5.6 \cdot 10^{+51}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -3.2 \cdot 10^{+25}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -1.05 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6.8 \cdot 10^{-271}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{-184}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{-97}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 2.4 \cdot 10^{-69}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 6
Error26.9
Cost7509
\[\begin{array}{l} \mathbf{if}\;im \leq -1.05 \cdot 10^{-15}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -3.5 \cdot 10^{-270}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 2.3 \cdot 10^{-157} \lor \neg \left(im \leq 5.4 \cdot 10^{-83}\right) \land im \leq 1.7 \cdot 10^{-69}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 7
Error27.6
Cost7248
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_1 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;im \leq -5.6 \cdot 10^{+51}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -3.7 \cdot 10^{+25}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -1.05 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{-65}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 8
Error36.4
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 3.6 \cdot 10^{-130}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 9
Error47.3
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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