?

Average Accuracy: 39.3% → 83.5%
Time: 10.3s
Precision: binary64
Cost: 32836

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -1.8 \cdot 10^{+92}:\\ \;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\\ \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 -1.8e+92)
   (* 0.5 (pow (exp (* 0.25 (+ (log (pow im 2.0)) (log (/ -1.0 re))))) 2.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 <= -1.8e+92) {
		tmp = 0.5 * pow(exp((0.25 * (log(pow(im, 2.0)) + log((-1.0 / re))))), 2.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 <= -1.8e+92) {
		tmp = 0.5 * Math.pow(Math.exp((0.25 * (Math.log(Math.pow(im, 2.0)) + Math.log((-1.0 / re))))), 2.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 <= -1.8e+92:
		tmp = 0.5 * math.pow(math.exp((0.25 * (math.log(math.pow(im, 2.0)) + math.log((-1.0 / re))))), 2.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 <= -1.8e+92)
		tmp = Float64(0.5 * (exp(Float64(0.25 * Float64(log((im ^ 2.0)) + log(Float64(-1.0 / re))))) ^ 2.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 <= -1.8e+92)
		tmp = 0.5 * (exp((0.25 * (log((im ^ 2.0)) + log((-1.0 / re))))) ^ 2.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, -1.8e+92], N[(0.5 * N[Power[N[Exp[N[(0.25 * N[(N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $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 -1.8 \cdot 10^{+92}:\\
\;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\\

\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.3%
Target47.2%
Herbie83.5%
\[\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 < -1.8e92

    1. Initial program 4.9%

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

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

      [Start]4.9

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

      +-commutative [=>]4.9

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

      hypot-def [=>]37.5

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

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

      [Start]37.5

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

      add-sqr-sqrt [=>]37.4

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

      pow2 [=>]37.4

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

      pow1/2 [=>]37.4

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

      sqrt-pow1 [=>]37.4

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

      *-commutative [=>]37.4

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

      metadata-eval [=>]37.4

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

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

    if -1.8e92 < re

    1. Initial program 46.4%

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

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

      [Start]46.4

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

      +-commutative [=>]46.4

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

      hypot-def [=>]88.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 simplification83.5%

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

Alternatives

Alternative 1
Accuracy83.2%
Cost13444
\[\begin{array}{l} \mathbf{if}\;re \leq -1.42 \cdot 10^{+92}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Accuracy57.9%
Cost7509
\[\begin{array}{l} \mathbf{if}\;im \leq -2.8 \cdot 10^{-128}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq -2.4 \cdot 10^{-229}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\ \mathbf{elif}\;im \leq 7.6 \cdot 10^{-247} \lor \neg \left(im \leq 1.9 \cdot 10^{-205}\right) \land im \leq 1.65 \cdot 10^{-102}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Accuracy59.0%
Cost7377
\[\begin{array}{l} \mathbf{if}\;im \leq -2.7 \cdot 10^{-130}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 5.3 \cdot 10^{-247} \lor \neg \left(im \leq 1.4 \cdot 10^{-205}\right) \land im \leq 1.7 \cdot 10^{-102}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Accuracy59.7%
Cost7377
\[\begin{array}{l} \mathbf{if}\;im \leq -1.25 \cdot 10^{-127}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 7.6 \cdot 10^{-247} \lor \neg \left(im \leq 1.5 \cdot 10^{-205}\right) \land im \leq 1.9 \cdot 10^{-102}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Accuracy41.7%
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 7.2 \cdot 10^{-118} \lor \neg \left(re \leq 8.5 \cdot 10^{-69}\right) \land re \leq 4 \cdot 10^{-7}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 6
Accuracy59.2%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -2 \cdot 10^{-131}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 6.8 \cdot 10^{-103}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 7
Accuracy26.6%
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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