Average Error: 39.0 → 6.9
Time: 6.6s
Precision: binary64
Cost: 39684
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot {\left(\frac{{\left(\sqrt[3]{im}\right)}^{2}}{\sqrt[3]{-re}}\right)}^{1.5}\\ \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 (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
   (* 0.5 (pow (/ (pow (cbrt im) 2.0) (cbrt (- re))) 1.5))
   (* 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * pow((pow(cbrt(im), 2.0) / cbrt(-re)), 1.5);
	} 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 (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
		tmp = 0.5 * Math.pow((Math.pow(Math.cbrt(im), 2.0) / Math.cbrt(-re)), 1.5);
	} 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 (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0)
		tmp = Float64(0.5 * (Float64((cbrt(im) ^ 2.0) / cbrt(Float64(-re))) ^ 1.5));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	end
	return 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[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Power[N[(N[Power[N[Power[im, 1/3], $MachinePrecision], 2.0], $MachinePrecision] / N[Power[(-re), 1/3], $MachinePrecision]), $MachinePrecision], 1.5], $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot {\left(\frac{{\left(\sqrt[3]{im}\right)}^{2}}{\sqrt[3]{-re}}\right)}^{1.5}\\

\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.0
Target33.8
Herbie6.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 (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0

    1. Initial program 57.0

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
      Proof
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (+.f64 re (hypot.f64 re im))))): 0 points increase in error, 0 points decrease in error
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (+.f64 re (Rewrite<= hypot-def_binary64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im)))))))): 112 points increase in error, 1 points decrease in error
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (Rewrite<= +-commutative_binary64 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))))): 0 points increase in error, 0 points decrease in error
    3. Taylor expanded in re around -inf 28.9

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{\left(-0.5 \cdot im\right) \cdot im}{re}}} \]
      Proof
      (/.f64 (*.f64 (*.f64 -1/2 im) im) re): 0 points increase in error, 0 points decrease in error
      (/.f64 (Rewrite<= associate-*r*_binary64 (*.f64 -1/2 (*.f64 im im))) re): 0 points increase in error, 2 points decrease in error
      (/.f64 (*.f64 -1/2 (Rewrite<= unpow2_binary64 (pow.f64 im 2))) re): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*r/_binary64 (*.f64 -1/2 (/.f64 (pow.f64 im 2) re))): 3 points increase in error, 1 points decrease in error
    5. Applied egg-rr29.3

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt[3]{\frac{-1 \cdot \left(im \cdot im\right)}{re}}\right)}^{1.5}} \]
    6. Applied egg-rr1.5

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

    if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)))

    1. Initial program 36.5

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
      Proof
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (+.f64 re (hypot.f64 re im))))): 0 points increase in error, 0 points decrease in error
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (+.f64 re (Rewrite<= hypot-def_binary64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im)))))))): 112 points increase in error, 1 points decrease in error
      (*.f64 1/2 (sqrt.f64 (*.f64 2 (Rewrite<= +-commutative_binary64 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification6.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot {\left(\frac{{\left(\sqrt[3]{im}\right)}^{2}}{\sqrt[3]{-re}}\right)}^{1.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error10.3
Cost26884
\[\begin{array}{l} \mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 2
Error9.7
Cost19908
\[\begin{array}{l} \mathbf{if}\;re \leq -1.32 \cdot 10^{+65}:\\ \;\;\;\;0.5 \cdot {\left(\frac{\sqrt[3]{im \cdot im}}{\sqrt[3]{-re}}\right)}^{1.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 3
Error26.6
Cost7180
\[\begin{array}{l} \mathbf{if}\;im \leq -1.3 \cdot 10^{-115}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 4.8 \cdot 10^{-289}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{-58}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 4
Error26.3
Cost7180
\[\begin{array}{l} \mathbf{if}\;im \leq -6 \cdot 10^{-115}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 6.5 \cdot 10^{-289}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 8.5 \cdot 10^{-59}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 5
Error27.7
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -9.2 \cdot 10^{-116}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 8.4 \cdot 10^{-251}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 6
Error37.7
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 1.22 \cdot 10^{-250}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 7
Error47.1
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce

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