?

Average Accuracy: 39.5% → 87.1%
Time: 10.2s
Precision: binary64
Cost: 13709

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -2.2 \cdot 10^{+132} \lor \neg \left(re \leq -1.02 \cdot 10^{+105}\right) \land re \leq -2.15 \cdot 10^{-45}:\\ \;\;\;\;\left|im \cdot \sqrt{\frac{-0.25}{re}}\right|\\ \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 (or (<= re -2.2e+132) (and (not (<= re -1.02e+105)) (<= re -2.15e-45)))
   (fabs (* im (sqrt (/ -0.25 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.2e+132) || (!(re <= -1.02e+105) && (re <= -2.15e-45))) {
		tmp = fabs((im * sqrt((-0.25 / 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.2e+132) || (!(re <= -1.02e+105) && (re <= -2.15e-45))) {
		tmp = Math.abs((im * Math.sqrt((-0.25 / 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.2e+132) or (not (re <= -1.02e+105) and (re <= -2.15e-45)):
		tmp = math.fabs((im * math.sqrt((-0.25 / 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.2e+132) || (!(re <= -1.02e+105) && (re <= -2.15e-45)))
		tmp = abs(Float64(im * sqrt(Float64(-0.25 / 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.2e+132) || (~((re <= -1.02e+105)) && (re <= -2.15e-45)))
		tmp = abs((im * sqrt((-0.25 / 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[Or[LessEqual[re, -2.2e+132], And[N[Not[LessEqual[re, -1.02e+105]], $MachinePrecision], LessEqual[re, -2.15e-45]]], N[Abs[N[(im * N[Sqrt[N[(-0.25 / 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.2 \cdot 10^{+132} \lor \neg \left(re \leq -1.02 \cdot 10^{+105}\right) \land re \leq -2.15 \cdot 10^{-45}:\\
\;\;\;\;\left|im \cdot \sqrt{\frac{-0.25}{re}}\right|\\

\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.5%
Target47.6%
Herbie87.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 re < -2.19999999999999989e132 or -1.02e105 < re < -2.1499999999999999e-45

    1. Initial program 13.2%

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

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

      [Start]13.2

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

      +-commutative [=>]13.2

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

      hypot-def [=>]41.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 43.0%

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

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

      [Start]43.0

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

      associate-*r/ [=>]43.0

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

      unpow2 [=>]43.0

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

      associate-*r* [=>]43.0

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

      \[\leadsto \color{blue}{{\left(\frac{-1 \cdot \left(im \cdot im\right)}{re} \cdot 0.25\right)}^{0.5}} \]
      Proof

      [Start]43.0

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

      add-sqr-sqrt [=>]42.9

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

      pow1/2 [=>]42.9

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

      pow1/2 [=>]42.9

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

      pow-prod-down [=>]43.0

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

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

      [Start]43.0

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

      unpow1/2 [=>]43.0

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

      associate-/l* [=>]42.3

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

      associate-*l/ [=>]42.3

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

      metadata-eval [=>]42.3

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

      metadata-eval [<=]42.3

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

      associate-/r/ [=>]43.0

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

      metadata-eval [=>]43.0

      \[ \sqrt{\frac{\color{blue}{-0.25}}{re} \cdot \left(im \cdot im\right)} \]
    7. Applied egg-rr73.3%

      \[\leadsto \color{blue}{\left|im \cdot \sqrt{\frac{-0.25}{re}}\right|} \]
      Proof

      [Start]43.0

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

      add-sqr-sqrt [=>]43.0

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

      rem-sqrt-square [=>]43.0

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

      *-commutative [=>]43.0

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

      sqrt-prod [=>]52.8

      \[ \left|\color{blue}{\sqrt{im \cdot im} \cdot \sqrt{\frac{-0.25}{re}}}\right| \]

      sqrt-prod [=>]35.5

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

      add-sqr-sqrt [<=]73.3

      \[ \left|\color{blue}{im} \cdot \sqrt{\frac{-0.25}{re}}\right| \]

    if -2.19999999999999989e132 < re < -1.02e105 or -2.1499999999999999e-45 < re

    1. Initial program 49.1%

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

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

      [Start]49.1

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

      +-commutative [=>]49.1

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

      hypot-def [=>]92.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.2 \cdot 10^{+132} \lor \neg \left(re \leq -1.02 \cdot 10^{+105}\right) \land re \leq -2.15 \cdot 10^{-45}:\\ \;\;\;\;\left|im \cdot \sqrt{\frac{-0.25}{re}}\right|\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy58.9%
Cost13252
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{if}\;re \leq -5 \cdot 10^{-45}:\\ \;\;\;\;\left|im \cdot \sqrt{\frac{-0.25}{re}}\right|\\ \mathbf{elif}\;re \leq -2.2 \cdot 10^{-211}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;re \leq 7.8 \cdot 10^{-281}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 2.55 \cdot 10^{-201}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;re \leq 9 \cdot 10^{-78} \lor \neg \left(re \leq 3 \cdot 10^{-37}\right) \land re \leq 6.5 \cdot 10^{+40}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \end{array} \]
Alternative 2
Accuracy53.7%
Cost8170
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{if}\;im \leq -2.6 \cdot 10^{-56}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.8 \cdot 10^{-148}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -6.7 \cdot 10^{-227}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6.5 \cdot 10^{-245}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 7.6 \cdot 10^{-284}:\\ \;\;\;\;\sqrt{im \cdot \left(im \cdot \frac{-0.25}{re}\right)}\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{-145} \lor \neg \left(im \leq 1.4 \cdot 10^{-85}\right) \land \left(im \leq 1.26 \cdot 10^{-37} \lor \neg \left(im \leq 8.5 \cdot 10^{+14}\right) \land im \leq 9.5 \cdot 10^{+81}\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 3
Accuracy53.8%
Cost8168
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ t_1 := 0.5 \cdot \sqrt{im \cdot 2}\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{if}\;im \leq -3.3 \cdot 10^{-56}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -9 \cdot 10^{-147}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -2.05 \cdot 10^{-227}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6.2 \cdot 10^{-245}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{-283}:\\ \;\;\;\;\sqrt{im \cdot \left(im \cdot \frac{-0.25}{re}\right)}\\ \mathbf{elif}\;im \leq 1.8 \cdot 10^{-141}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 3.9 \cdot 10^{-87}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.32 \cdot 10^{-37}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 9.2 \cdot 10^{+14}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;im \leq 9.5 \cdot 10^{+81}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy51.6%
Cost7773
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{if}\;re \leq -2.75 \cdot 10^{+43}:\\ \;\;\;\;\sqrt{\frac{im}{\frac{re}{im \cdot -0.25}}}\\ \mathbf{elif}\;re \leq -6.5 \cdot 10^{-208}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-280}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 5 \cdot 10^{-200}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;re \leq 4.7 \cdot 10^{-76} \lor \neg \left(re \leq 1.85 \cdot 10^{-29}\right) \land re \leq 8 \cdot 10^{+35}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \end{array} \]
Alternative 5
Accuracy35.0%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -1.45 \cdot 10^{-10}:\\ \;\;\;\;im \cdot \sqrt{\frac{-0.25}{re}}\\ \mathbf{elif}\;re \leq 1.85 \cdot 10^{+166}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 2}\\ \end{array} \]
Alternative 6
Accuracy56.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -1.7 \cdot 10^{-243}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{-116}:\\ \;\;\;\;im \cdot \sqrt{\frac{-0.25}{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 7
Accuracy29.0%
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 8.5 \cdot 10^{-277}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 8
Accuracy25.7%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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