?

Average Accuracy: 40.5% → 85.3%
Time: 9.6s
Precision: binary64
Cost: 26884

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;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} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
   (* 0.5 (/ im (sqrt (- re))))
   (* 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * (im / sqrt(-re));
	} 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 (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * (im / Math.sqrt(-re));
	} 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 math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0:
		tmp = 0.5 * (im / math.sqrt(-re))
	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 (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0)
		tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re))));
	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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0)
		tmp = 0.5 * (im / sqrt(-re));
	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[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;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}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original40.5%
Target48.1%
Herbie85.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 (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 9.9%

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

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

      [Start]9.9

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

      +-commutative [=>]9.9

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

      hypot-def [=>]9.9

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    4. Simplified51.6%

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

      [Start]51.6

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

      *-commutative [=>]51.6

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

      unpow2 [=>]51.6

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

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

      [Start]51.6

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

      *-commutative [=>]51.6

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

      associate-*r* [=>]51.6

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

      associate-*r/ [=>]51.6

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

      metadata-eval [=>]51.6

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

      neg-mul-1 [<=]51.6

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

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

      [Start]51.6

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

      frac-2neg [=>]51.6

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

      remove-double-neg [=>]51.6

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

      sqrt-div [=>]56.9

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

      sqrt-prod [=>]49.2

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

      add-sqr-sqrt [<=]53.8

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

    if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 44.7%

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

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

      [Start]44.7

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

      +-commutative [=>]44.7

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

      hypot-def [=>]89.6

      \[ 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 simplification85.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;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
Accuracy52.0%
Cost8168
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ t_2 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_3 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{if}\;re \leq -5 \cdot 10^{+146}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -6.5 \cdot 10^{+76}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -1.26 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -4.5 \cdot 10^{-12}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq -5.4 \cdot 10^{-81}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 7 \cdot 10^{-296}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 1.25 \cdot 10^{-252}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-166}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;re \leq 2.3 \cdot 10^{-114}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 6.5 \cdot 10^{-94}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 2
Accuracy51.7%
Cost8168
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ t_1 := 0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\ t_2 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_3 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{if}\;re \leq -4.7 \cdot 10^{+148}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -3.1 \cdot 10^{+77}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -7.2 \cdot 10^{+60}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.35 \cdot 10^{-7}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq -1.65 \cdot 10^{-83}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-297}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 1.5 \cdot 10^{-253}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;re \leq 3.1 \cdot 10^{-160}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 10^{-114}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;re \leq 1.4 \cdot 10^{-96}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 3
Accuracy51.9%
Cost8168
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ t_1 := 0.5 \cdot \sqrt{im \cdot -2}\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{if}\;re \leq -9 \cdot 10^{+146}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq -5.2 \cdot 10^{+76}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -7.2 \cdot 10^{+60}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\ \mathbf{elif}\;re \leq -3.45 \cdot 10^{-6}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -6.8 \cdot 10^{-81}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;re \leq 4 \cdot 10^{-297}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.6 \cdot 10^{-252}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-166}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.3 \cdot 10^{-114}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 1.15 \cdot 10^{-89}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 4
Accuracy50.6%
Cost7773
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ t_1 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;re \leq -9.5 \cdot 10^{-83}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 1.2 \cdot 10^{-296}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.55 \cdot 10^{-252}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4.9 \cdot 10^{-163}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 6.6 \cdot 10^{-108} \lor \neg \left(re \leq 1.5 \cdot 10^{-73}\right) \land re \leq 4.6 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 5
Accuracy51.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 - im\right)}\\ \mathbf{if}\;re \leq -1.35 \cdot 10^{-79}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 8 \cdot 10^{-296}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-254}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-164}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 4.8 \cdot 10^{-114}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.85 \cdot 10^{-93}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 6
Accuracy50.7%
Cost7512
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ t_1 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;re \leq -4.1 \cdot 10^{-81}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 4.6 \cdot 10^{-296}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-254}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-164}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.7 \cdot 10^{-114}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 5.7 \cdot 10^{-100}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 7
Accuracy43.0%
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 4.6 \cdot 10^{-108} \lor \neg \left(re \leq 4.4 \cdot 10^{-51}\right) \land re \leq 4.6 \cdot 10^{-12}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 8
Accuracy59.8%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -1.8 \cdot 10^{-156}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 3.1 \cdot 10^{-145}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 9
Accuracy25.8%
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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