?

Average Accuracy: 39.1% → 82.9%
Time: 11.3s
Precision: binary64
Cost: 33284

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -5.8 \cdot 10^{+63}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot {\left(e^{\log \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot 0.16666666666666666 + 0.16666666666666666 \cdot \log \left(\frac{-1}{re}\right)}\right)}^{3}\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 (<= re -5.8e+63)
   (*
    0.5
    (*
     (sqrt 2.0)
     (pow
      (exp
       (+
        (* (log (* 0.5 (* im im))) 0.16666666666666666)
        (* 0.16666666666666666 (log (/ -1.0 re)))))
      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 <= -5.8e+63) {
		tmp = 0.5 * (sqrt(2.0) * pow(exp(((log((0.5 * (im * im))) * 0.16666666666666666) + (0.16666666666666666 * log((-1.0 / re))))), 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 <= -5.8e+63) {
		tmp = 0.5 * (Math.sqrt(2.0) * Math.pow(Math.exp(((Math.log((0.5 * (im * im))) * 0.16666666666666666) + (0.16666666666666666 * Math.log((-1.0 / re))))), 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 <= -5.8e+63:
		tmp = 0.5 * (math.sqrt(2.0) * math.pow(math.exp(((math.log((0.5 * (im * im))) * 0.16666666666666666) + (0.16666666666666666 * math.log((-1.0 / re))))), 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 <= -5.8e+63)
		tmp = Float64(0.5 * Float64(sqrt(2.0) * (exp(Float64(Float64(log(Float64(0.5 * Float64(im * im))) * 0.16666666666666666) + Float64(0.16666666666666666 * log(Float64(-1.0 / re))))) ^ 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 <= -5.8e+63)
		tmp = 0.5 * (sqrt(2.0) * (exp(((log((0.5 * (im * im))) * 0.16666666666666666) + (0.16666666666666666 * log((-1.0 / re))))) ^ 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, -5.8e+63], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Power[N[Exp[N[(N[(N[Log[N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.16666666666666666), $MachinePrecision] + N[(0.16666666666666666 * N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 3.0], $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 -5.8 \cdot 10^{+63}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot {\left(e^{\log \left(0.5 \cdot \left(im \cdot im\right)\right) \cdot 0.16666666666666666 + 0.16666666666666666 \cdot \log \left(\frac{-1}{re}\right)}\right)}^{3}\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.1%
Target47.2%
Herbie82.9%
\[\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.7999999999999999e63

    1. Initial program 7.7%

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

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

      [Start]7.7

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

      +-commutative [=>]7.7

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

      hypot-def [=>]38.0

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

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

      [Start]38.0

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

      add-cube-cbrt [=>]37.6

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

      pow3 [=>]37.6

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

      *-commutative [=>]37.6

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

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

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

      [Start]58.3

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

      associate-*l* [=>]58.3

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

      pow-base-1 [=>]58.3

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

      *-rgt-identity [=>]58.3

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

      exp-prod [=>]56.2

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

      *-commutative [=>]56.2

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

      unpow2 [=>]56.2

      \[ 0.5 \cdot \left(\sqrt{2} \cdot {\left({\left(e^{0.16666666666666666}\right)}^{\left(\log \left(\frac{-1}{re}\right) + \log \left(\color{blue}{\left(im \cdot im\right)} \cdot 0.5\right)\right)}\right)}^{3}\right) \]
    6. Taylor expanded in re around -inf 58.3%

      \[\leadsto 0.5 \cdot \left(\sqrt{2} \cdot {\color{blue}{\left(e^{0.16666666666666666 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(0.5 \cdot {im}^{2}\right)\right)}\right)}}^{3}\right) \]
    7. Applied egg-rr58.1%

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

      [Start]58.3

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

      distribute-lft-in [=>]58.1

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

      +-commutative [=>]58.1

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

      *-commutative [=>]58.1

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

      unpow2 [=>]58.1

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

    if -5.7999999999999999e63 < re

    1. Initial program 46.9%

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

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

      [Start]46.9

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

      +-commutative [=>]46.9

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

      hypot-def [=>]89.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 simplification82.9%

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

Alternatives

Alternative 1
Accuracy85.1%
Cost26884
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{im \cdot im + re \cdot re}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im}{re} \cdot \left(-im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Accuracy58.4%
Cost7773
\[\begin{array}{l} \mathbf{if}\;im \leq -4.8 \cdot 10^{-64}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -2.4 \cdot 10^{-98}:\\ \;\;\;\;\frac{im}{\sqrt{-re}} \cdot -0.5\\ \mathbf{elif}\;im \leq -2.55 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.12 \cdot 10^{-160}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{-126} \lor \neg \left(im \leq 2 \cdot 10^{-61}\right) \land im \leq 3.4 \cdot 10^{+29}:\\ \;\;\;\;\frac{im}{\sqrt{re \cdot -4}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Accuracy58.4%
Cost7773
\[\begin{array}{l} t_0 := \sqrt{re \cdot -4}\\ \mathbf{if}\;im \leq -4.6 \cdot 10^{-64}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -5.4 \cdot 10^{-96}:\\ \;\;\;\;\frac{im}{\sqrt{-re}} \cdot -0.5\\ \mathbf{elif}\;im \leq -1.25 \cdot 10^{-114}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.32 \cdot 10^{-158}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 4 \cdot 10^{-126}:\\ \;\;\;\;\frac{1}{\frac{t_0}{im}}\\ \mathbf{elif}\;im \leq 3.5 \cdot 10^{-61} \lor \neg \left(im \leq 3.2 \cdot 10^{+29}\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{im}{t_0}\\ \end{array} \]
Alternative 4
Accuracy58.4%
Cost7509
\[\begin{array}{l} \mathbf{if}\;im \leq -9.2 \cdot 10^{-115}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 6.2 \cdot 10^{-158}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 3.7 \cdot 10^{-126} \lor \neg \left(im \leq 1.02 \cdot 10^{-64}\right) \land im \leq 3.2 \cdot 10^{+29}:\\ \;\;\;\;\frac{im}{\sqrt{re \cdot -4}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Accuracy58.8%
Cost7509
\[\begin{array}{l} \mathbf{if}\;im \leq -1.7 \cdot 10^{-112}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 2.6 \cdot 10^{-160}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 3.4 \cdot 10^{-126} \lor \neg \left(im \leq 1.9 \cdot 10^{-61}\right) \land im \leq 3.2 \cdot 10^{+29}:\\ \;\;\;\;\frac{im}{\sqrt{re \cdot -4}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 6
Accuracy58.3%
Cost7381
\[\begin{array}{l} \mathbf{if}\;im \leq -1.25 \cdot 10^{-103}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.45 \cdot 10^{-157}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 4.3 \cdot 10^{-126} \lor \neg \left(im \leq 1.05 \cdot 10^{-61}\right) \land im \leq 20000000000000:\\ \;\;\;\;\frac{im}{\sqrt{re \cdot -4}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 7
Accuracy49.4%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -8.4 \cdot 10^{+58}:\\ \;\;\;\;\frac{im}{\sqrt{re \cdot -4}}\\ \mathbf{elif}\;re \leq 2.2 \cdot 10^{-156}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 8
Accuracy42.0%
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 2.05 \cdot 10^{-156}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 9
Accuracy25.8%
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce?

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