?

Average Accuracy: 39.7% → 84.1%
Time: 11.0s
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 \sqrt{\frac{-im}{\frac{re}{im}}}\\ \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 (sqrt (/ (- im) (/ re im))))
   (* 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 * sqrt((-im / (re / im)));
	} 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 * Math.sqrt((-im / (re / im)));
	} 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 * math.sqrt((-im / (re / im)))
	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 * sqrt(Float64(Float64(-im) / Float64(re / im))));
	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 * sqrt((-im / (re / im)));
	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[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $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 \sqrt{\frac{-im}{\frac{re}{im}}}\\

\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.7%
Target47.4%
Herbie84.1%
\[\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 11.0%

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

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

      [Start]11.0

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

      +-commutative [=>]11.0

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

      hypot-def [=>]11.0

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

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

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

      [Start]51.5

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

      associate-*r/ [=>]51.5

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

      unpow2 [=>]51.5

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

      associate-*r* [=>]51.5

      \[ 0.5 \cdot \sqrt{2 \cdot \frac{\color{blue}{\left(-0.5 \cdot im\right) \cdot im}}{re}} \]
    5. Taylor expanded in im around 0 51.5%

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

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

      [Start]51.5

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

      mul-1-neg [=>]51.5

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

      unpow2 [=>]51.5

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

      distribute-frac-neg [<=]51.5

      \[ 0.5 \cdot \sqrt{\color{blue}{\frac{-im \cdot im}{re}}} \]
    7. Taylor expanded in im around 0 51.5%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-1 \cdot \frac{{im}^{2}}{re}}} \]
    8. Simplified57.8%

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

      [Start]51.5

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

      mul-1-neg [=>]51.5

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

      unpow2 [=>]51.5

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

      distribute-neg-frac [=>]51.5

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

      distribute-lft-neg-out [<=]51.5

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

      associate-/l* [=>]57.8

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

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

    1. Initial program 43.7%

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

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

      [Start]43.7

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

      +-commutative [=>]43.7

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

      hypot-def [=>]87.8

      \[ 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 simplification84.1%

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

Alternatives

Alternative 1
Accuracy49.2%
Cost7773
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -2.4 \cdot 10^{-39}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -2.1 \cdot 10^{-137}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.1 \cdot 10^{-259}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-248}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-196}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.15 \cdot 10^{-178} \lor \neg \left(re \leq 7.5 \cdot 10^{-32}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Accuracy49.7%
Cost7773
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{if}\;re \leq -1.1 \cdot 10^{-37}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -2.9 \cdot 10^{-136}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -4 \cdot 10^{-260}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 7.5 \cdot 10^{-240}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-196}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.3 \cdot 10^{-172} \lor \neg \left(re \leq 1.15 \cdot 10^{-30}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Accuracy49.8%
Cost7773
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot im}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{if}\;re \leq -7.5 \cdot 10^{-38}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{-1}{re}}\right)\\ \mathbf{elif}\;re \leq -3.1 \cdot 10^{-134}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -1.25 \cdot 10^{-260}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 4 \cdot 10^{-238}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-196}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 1.05 \cdot 10^{-178} \lor \neg \left(re \leq 3.5 \cdot 10^{-26}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Accuracy50.9%
Cost7641
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{if}\;re \leq -280000000000:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{elif}\;re \leq -3.6 \cdot 10^{-261}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.35 \cdot 10^{-242}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-196}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.12 \cdot 10^{-175} \lor \neg \left(re \leq 9 \cdot 10^{-31}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Accuracy50.9%
Cost7641
\[\begin{array}{l} \mathbf{if}\;re \leq -2700000000000:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{elif}\;re \leq -2.2 \cdot 10^{-260}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\left(re - im\right) + \frac{re}{\frac{im}{re}} \cdot -0.5\right)}\\ \mathbf{elif}\;re \leq 2 \cdot 10^{-251}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{elif}\;re \leq 3.3 \cdot 10^{-196}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;re \leq 1.05 \cdot 10^{-178} \lor \neg \left(re \leq 5.5 \cdot 10^{-27}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 6
Accuracy55.8%
Cost7048
\[\begin{array}{l} \mathbf{if}\;im \leq -3.1 \cdot 10^{-273}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 9.4 \cdot 10^{-178}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 7
Accuracy52.0%
Cost6916
\[\begin{array}{l} \mathbf{if}\;im \leq -5 \cdot 10^{-310}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 8
Accuracy26.7%
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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