?

Average Accuracy: 39.0% → 83.0%
Time: 7.8s
Precision: binary64
Cost: 33100

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} t_0 := \log \left(\frac{-1}{re}\right)\\ t_1 := 0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + t_0\right)}\right)}^{2}\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{if}\;re \leq -1.65 \cdot 10^{+263}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -2.05 \cdot 10^{+257}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq -2.15 \cdot 10^{+155}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.85 \cdot 10^{+136}:\\ \;\;\;\;0.5 \cdot e^{0.5 \cdot t_0 - \log \left(\frac{-1}{im}\right)}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (log (/ -1.0 re)))
        (t_1 (* 0.5 (pow (exp (* 0.25 (+ (log (pow im 2.0)) t_0))) 2.0)))
        (t_2 (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
   (if (<= re -1.65e+263)
     t_1
     (if (<= re -2.05e+257)
       t_2
       (if (<= re -2.15e+155)
         t_1
         (if (<= re -1.85e+136)
           (* 0.5 (exp (- (* 0.5 t_0) (log (/ -1.0 im)))))
           t_2))))))
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 t_0 = log((-1.0 / re));
	double t_1 = 0.5 * pow(exp((0.25 * (log(pow(im, 2.0)) + t_0))), 2.0);
	double t_2 = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	double tmp;
	if (re <= -1.65e+263) {
		tmp = t_1;
	} else if (re <= -2.05e+257) {
		tmp = t_2;
	} else if (re <= -2.15e+155) {
		tmp = t_1;
	} else if (re <= -1.85e+136) {
		tmp = 0.5 * exp(((0.5 * t_0) - log((-1.0 / im))));
	} else {
		tmp = t_2;
	}
	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 t_0 = Math.log((-1.0 / re));
	double t_1 = 0.5 * Math.pow(Math.exp((0.25 * (Math.log(Math.pow(im, 2.0)) + t_0))), 2.0);
	double t_2 = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	double tmp;
	if (re <= -1.65e+263) {
		tmp = t_1;
	} else if (re <= -2.05e+257) {
		tmp = t_2;
	} else if (re <= -2.15e+155) {
		tmp = t_1;
	} else if (re <= -1.85e+136) {
		tmp = 0.5 * Math.exp(((0.5 * t_0) - Math.log((-1.0 / im))));
	} else {
		tmp = t_2;
	}
	return tmp;
}
def code(re, im):
	return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
def code(re, im):
	t_0 = math.log((-1.0 / re))
	t_1 = 0.5 * math.pow(math.exp((0.25 * (math.log(math.pow(im, 2.0)) + t_0))), 2.0)
	t_2 = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	tmp = 0
	if re <= -1.65e+263:
		tmp = t_1
	elif re <= -2.05e+257:
		tmp = t_2
	elif re <= -2.15e+155:
		tmp = t_1
	elif re <= -1.85e+136:
		tmp = 0.5 * math.exp(((0.5 * t_0) - math.log((-1.0 / im))))
	else:
		tmp = t_2
	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)
	t_0 = log(Float64(-1.0 / re))
	t_1 = Float64(0.5 * (exp(Float64(0.25 * Float64(log((im ^ 2.0)) + t_0))) ^ 2.0))
	t_2 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))))
	tmp = 0.0
	if (re <= -1.65e+263)
		tmp = t_1;
	elseif (re <= -2.05e+257)
		tmp = t_2;
	elseif (re <= -2.15e+155)
		tmp = t_1;
	elseif (re <= -1.85e+136)
		tmp = Float64(0.5 * exp(Float64(Float64(0.5 * t_0) - log(Float64(-1.0 / im)))));
	else
		tmp = t_2;
	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)
	t_0 = log((-1.0 / re));
	t_1 = 0.5 * (exp((0.25 * (log((im ^ 2.0)) + t_0))) ^ 2.0);
	t_2 = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	tmp = 0.0;
	if (re <= -1.65e+263)
		tmp = t_1;
	elseif (re <= -2.05e+257)
		tmp = t_2;
	elseif (re <= -2.15e+155)
		tmp = t_1;
	elseif (re <= -1.85e+136)
		tmp = 0.5 * exp(((0.5 * t_0) - log((-1.0 / im))));
	else
		tmp = t_2;
	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_] := Block[{t$95$0 = N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Power[N[Exp[N[(0.25 * N[(N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1.65e+263], t$95$1, If[LessEqual[re, -2.05e+257], t$95$2, If[LessEqual[re, -2.15e+155], t$95$1, If[LessEqual[re, -1.85e+136], N[(0.5 * N[Exp[N[(N[(0.5 * t$95$0), $MachinePrecision] - N[Log[N[(-1.0 / im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$2]]]]]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := \log \left(\frac{-1}{re}\right)\\
t_1 := 0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + t_0\right)}\right)}^{2}\\
t_2 := 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\mathbf{if}\;re \leq -1.65 \cdot 10^{+263}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -2.05 \cdot 10^{+257}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;re \leq -2.15 \cdot 10^{+155}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;re \leq -1.85 \cdot 10^{+136}:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot t_0 - \log \left(\frac{-1}{im}\right)}\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original39.0%
Target46.9%
Herbie83.0%
\[\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 3 regimes
  2. if re < -1.65e263 or -2.0500000000000001e257 < re < -2.1500000000000001e155

    1. Initial program 0.0%

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

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

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

      \[\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.65e263 < re < -2.0500000000000001e257 or -1.85000000000000005e136 < re

    1. Initial program 47.1%

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

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

    if -2.1500000000000001e155 < re < -1.85000000000000005e136

    1. Initial program 18.4%

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

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

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

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

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

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

      \[\leadsto 0.5 \cdot e^{\color{blue}{\log \left(\frac{-1}{re}\right) \cdot 0.5 - \log \left(\frac{-1}{im}\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification83.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.65 \cdot 10^{+263}:\\ \;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\\ \mathbf{elif}\;re \leq -2.05 \cdot 10^{+257}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{elif}\;re \leq -2.15 \cdot 10^{+155}:\\ \;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\\ \mathbf{elif}\;re \leq -1.85 \cdot 10^{+136}:\\ \;\;\;\;0.5 \cdot e^{0.5 \cdot \log \left(\frac{-1}{re}\right) - \log \left(\frac{-1}{im}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy85.3%
Cost26884
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;\sqrt{im \cdot \left(\frac{im}{re} \cdot -0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Accuracy58.3%
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -3.25 \cdot 10^{-192}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 6.5 \cdot 10^{-42}:\\ \;\;\;\;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
Accuracy31.5%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -1.6 \cdot 10^{-151}:\\ \;\;\;\;3812798742493.5\\ \mathbf{elif}\;im \leq 2.9 \cdot 10^{-230}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 4
Accuracy43.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -5.7 \cdot 10^{+85}:\\ \;\;\;\;0\\ \mathbf{elif}\;re \leq 3.6 \cdot 10^{-209}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 5
Accuracy57.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -3.2 \cdot 10^{-193}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 6.5 \cdot 10^{-42}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Accuracy8.8%
Cost328
\[\begin{array}{l} \mathbf{if}\;im \leq -1.55 \cdot 10^{-151}:\\ \;\;\;\;193710244.5\\ \mathbf{elif}\;im \leq 4.1 \cdot 10^{-224}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.006944444444444444\\ \end{array} \]
Alternative 7
Accuracy8.8%
Cost328
\[\begin{array}{l} \mathbf{if}\;im \leq -1.8 \cdot 10^{-151}:\\ \;\;\;\;3812798742493.5\\ \mathbf{elif}\;im \leq 5 \cdot 10^{-223}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.006944444444444444\\ \end{array} \]
Alternative 8
Accuracy8.8%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -6.5 \cdot 10^{+45}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;4.923200210024256 \cdot 10^{-15}\\ \end{array} \]
Alternative 9
Accuracy8.8%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -5.5 \cdot 10^{+46}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;4.96145150637606 \cdot 10^{-8}\\ \end{array} \]
Alternative 10
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -1.9 \cdot 10^{+47}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;1.071673525377229 \cdot 10^{-5}\\ \end{array} \]
Alternative 11
Accuracy8.9%
Cost196
\[\begin{array}{l} \mathbf{if}\;re \leq -8.2 \cdot 10^{+45}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;0.006944444444444444\\ \end{array} \]
Alternative 12
Accuracy6.1%
Cost64
\[0 \]

Error

Reproduce?

herbie shell --seed 2023157 -o generate:proofs
(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)))))