Average Error: 38.9 → 6.8
Time: 9.8s
Precision: binary64
Cost: 20356
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\ \;\;\;\;0.5 \cdot \left|im \cdot \sqrt{\frac{-1}{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 (<= (+ re (sqrt (+ (* re re) (* im im)))) 0.0)
   (* 0.5 (fabs (* im (sqrt (/ -1.0 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 + sqrt(((re * re) + (im * im)))) <= 0.0) {
		tmp = 0.5 * fabs((im * sqrt((-1.0 / 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 + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
		tmp = 0.5 * Math.abs((im * Math.sqrt((-1.0 / 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 + math.sqrt(((re * re) + (im * im)))) <= 0.0:
		tmp = 0.5 * math.fabs((im * math.sqrt((-1.0 / 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 (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0)
		tmp = Float64(0.5 * abs(Float64(im * sqrt(Float64(-1.0 / 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 + sqrt(((re * re) + (im * im)))) <= 0.0)
		tmp = 0.5 * abs((im * sqrt((-1.0 / 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[LessEqual[N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Abs[N[(im * N[Sqrt[N[(-1.0 / re), $MachinePrecision]], $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 + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot \left|im \cdot \sqrt{\frac{-1}{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

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

    1. Initial program 59.1

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

      \[\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)))))))): 2 points increase in error, 0 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))))): 2 points increase in error, 0 points decrease in error
    3. Taylor expanded in re around -inf 37.1

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

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

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

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

    1. Initial program 35.4

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

      \[\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)))))))): 2 points increase in error, 0 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))))): 2 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification6.8

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

Alternatives

Alternative 1
Error25.6
Cost13644
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\ t_1 := 0.5 \cdot \left|im \cdot \sqrt{\frac{-1}{re}}\right|\\ \mathbf{if}\;re \leq -5.5 \cdot 10^{-117}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -4.6 \cdot 10^{-124}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -9 \cdot 10^{-187}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq -1.1 \cdot 10^{-299}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.8 \cdot 10^{-258}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{re}{\frac{im}{re \cdot -0.5}} + \left(re - im\right)\right)}\\ \mathbf{elif}\;re \leq 1.85 \cdot 10^{-112}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{elif}\;re \leq 4.9 \cdot 10^{-30}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(im - re\right) \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 4}\\ \end{array} \]
Alternative 2
Error27.6
Cost7513
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -9 \cdot 10^{-54}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq -9.5 \cdot 10^{-228}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.02 \cdot 10^{-243}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 10^{-187} \lor \neg \left(im \leq 1.12 \cdot 10^{-55}\right) \land im \leq 1.5 \cdot 10^{-30}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 3
Error27.0
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -6.5 \cdot 10^{-54}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq -9.5 \cdot 10^{-228}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.05 \cdot 10^{-243}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 2.15 \cdot 10^{-188}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error26.4
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -1.35 \cdot 10^{-109}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(im - re\right) \cdot -2}\\ \mathbf{elif}\;im \leq -2.2 \cdot 10^{-227}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.05 \cdot 10^{-243}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{-189}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Error26.4
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -9.5 \cdot 10^{-111}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(im - re\right) \cdot -2}\\ \mathbf{elif}\;im \leq -9.5 \cdot 10^{-228}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.05 \cdot 10^{-243}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\ \mathbf{elif}\;im \leq 9.2 \cdot 10^{-188}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 6
Error26.3
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{if}\;im \leq -4 \cdot 10^{-110}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(im - re\right) \cdot -2}\\ \mathbf{elif}\;im \leq -9.5 \cdot 10^{-228}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -6.8 \cdot 10^{-244}:\\ \;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\ \mathbf{elif}\;im \leq 10^{-187}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 7
Error27.4
Cost7249
\[\begin{array}{l} \mathbf{if}\;im \leq -5.5 \cdot 10^{-54}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 9.8 \cdot 10^{-188} \lor \neg \left(im \leq 1.8 \cdot 10^{-60}\right) \land im \leq 7.5 \cdot 10^{-31}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 8
Error44.9
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 1.45 \cdot 10^{-214}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 9
Error30.7
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq -5 \cdot 10^{-310}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 10
Error46.9
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce

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