?

Average Error: 38.3 → 10.8
Time: 10.3s
Precision: binary64
Cost: 13444

?

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

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.3
Target33.2
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 2 regimes
  2. if re < -2.2500000000000002e32

    1. Initial program 57.2

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

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

      [Start]57.2

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

      +-commutative [=>]57.2

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

      hypot-def [=>]39.1

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

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

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

      [Start]32.5

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

      *-commutative [=>]32.5

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

      unpow2 [=>]32.5

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

      associate-/l* [=>]28.5

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

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

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

      [Start]32.5

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

      mul-1-neg [=>]32.5

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

      unpow2 [=>]32.5

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

      associate-*r/ [<=]28.5

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

      distribute-rgt-neg-in [=>]28.5

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

      distribute-frac-neg [<=]28.5

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

    if -2.2500000000000002e32 < re

    1. Initial program 32.7

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

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

      [Start]32.7

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

      +-commutative [=>]32.7

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

      hypot-def [=>]5.5

      \[ 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 simplification10.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.25 \cdot 10^{+32}:\\ \;\;\;\;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} \]

Alternatives

Alternative 1
Error30.1
Cost7772
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ t_1 := 0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ t_2 := 0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{if}\;re \leq -1.82 \cdot 10^{+22}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -8.5 \cdot 10^{-95}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{elif}\;re \leq -1.85 \cdot 10^{-125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2 \cdot 10^{-295}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 1.8 \cdot 10^{-193}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.22 \cdot 10^{-148}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 9.5 \cdot 10^{-90}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 2
Error26.8
Cost7376
\[\begin{array}{l} t_0 := \frac{im}{\sqrt{-re}}\\ \mathbf{if}\;im \leq -1.65 \cdot 10^{-84}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq -5.8 \cdot 10^{-288}:\\ \;\;\;\;t_0 \cdot -0.5\\ \mathbf{elif}\;im \leq 2 \cdot 10^{-258}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 3.2 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Error26.8
Cost7244
\[\begin{array}{l} \mathbf{if}\;im \leq -4.5 \cdot 10^{-76}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.92 \cdot 10^{-258}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 3 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error27.1
Cost7180
\[\begin{array}{l} \mathbf{if}\;im \leq -4.8 \cdot 10^{-76}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.92 \cdot 10^{-258}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 3.5 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 5
Error26.6
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -3.6 \cdot 10^{-76}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{-55}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Error36.7
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 3.7 \cdot 10^{-91}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 7
Error47.8
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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