Average Error: 38.5 → 10.7
Time: 12.8s
Precision: binary64
Cost: 26756
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -5.210881243518972 \cdot 10^{-8}:\\ \;\;\;\;0.5 \cdot \left({\left({\left(\frac{-1}{re}\right)}^{0.25} \cdot {\left(im \cdot \left(0.5 \cdot im\right)\right)}^{0.25}\right)}^{2} \cdot \sqrt{2}\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.210881243518972e-8)
   (*
    0.5
    (*
     (pow (* (pow (/ -1.0 re) 0.25) (pow (* im (* 0.5 im)) 0.25)) 2.0)
     (sqrt 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 <= -5.210881243518972e-8) {
		tmp = 0.5 * (pow((pow((-1.0 / re), 0.25) * pow((im * (0.5 * im)), 0.25)), 2.0) * sqrt(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 <= -5.210881243518972e-8) {
		tmp = 0.5 * (Math.pow((Math.pow((-1.0 / re), 0.25) * Math.pow((im * (0.5 * im)), 0.25)), 2.0) * Math.sqrt(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 <= -5.210881243518972e-8:
		tmp = 0.5 * (math.pow((math.pow((-1.0 / re), 0.25) * math.pow((im * (0.5 * im)), 0.25)), 2.0) * math.sqrt(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 <= -5.210881243518972e-8)
		tmp = Float64(0.5 * Float64((Float64((Float64(-1.0 / re) ^ 0.25) * (Float64(im * Float64(0.5 * im)) ^ 0.25)) ^ 2.0) * sqrt(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 <= -5.210881243518972e-8)
		tmp = 0.5 * (((((-1.0 / re) ^ 0.25) * ((im * (0.5 * im)) ^ 0.25)) ^ 2.0) * sqrt(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, -5.210881243518972e-8], N[(0.5 * N[(N[Power[N[(N[Power[N[(-1.0 / re), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[N[(im * N[(0.5 * im), $MachinePrecision]), $MachinePrecision], 0.25], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] * N[Sqrt[2.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.210881243518972 \cdot 10^{-8}:\\
\;\;\;\;0.5 \cdot \left({\left({\left(\frac{-1}{re}\right)}^{0.25} \cdot {\left(im \cdot \left(0.5 \cdot im\right)\right)}^{0.25}\right)}^{2} \cdot \sqrt{2}\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.5
Target33.4
Herbie10.7
\[\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.21088124351897191e-8

    1. Initial program 57.1

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

      \[\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)))))))): 126 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))))): 0 points increase in error, 0 points decrease in error
    3. Applied egg-rr38.3

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

      \[\leadsto 0.5 \cdot \left(\color{blue}{{\left({\left(re + \mathsf{hypot}\left(re, im\right)\right)}^{0.25}\right)}^{2}} \cdot \sqrt{2}\right) \]
    5. Applied egg-rr38.5

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

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

      \[\leadsto 0.5 \cdot \left(\color{blue}{{\left({\left(\frac{-1}{re}\right)}^{0.25} \cdot {\left(im \cdot \left(im \cdot 0.5\right)\right)}^{0.25}\right)}^{2}} \cdot \sqrt{2}\right) \]
      Proof
      (pow.f64 (*.f64 (pow.f64 (/.f64 -1 re) 1/4) (pow.f64 (*.f64 im (*.f64 im 1/2)) 1/4)) 2): 0 points increase in error, 0 points decrease in error
      (pow.f64 (*.f64 (Rewrite<= exp-to-pow_binary64 (exp.f64 (*.f64 (log.f64 (/.f64 -1 re)) 1/4))) (pow.f64 (*.f64 im (*.f64 im 1/2)) 1/4)) 2): 27 points increase in error, 35 points decrease in error
      (pow.f64 (*.f64 (exp.f64 (*.f64 (log.f64 (/.f64 -1 re)) 1/4)) (pow.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 im im) 1/2)) 1/4)) 2): 0 points increase in error, 1 points decrease in error
      (pow.f64 (*.f64 (exp.f64 (*.f64 (log.f64 (/.f64 -1 re)) 1/4)) (pow.f64 (*.f64 (Rewrite<= unpow2_binary64 (pow.f64 im 2)) 1/2) 1/4)) 2): 0 points increase in error, 0 points decrease in error
      (pow.f64 (*.f64 (exp.f64 (*.f64 (log.f64 (/.f64 -1 re)) 1/4)) (pow.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/2 (pow.f64 im 2))) 1/4)) 2): 0 points increase in error, 0 points decrease in error
      (pow.f64 (*.f64 (exp.f64 (*.f64 (log.f64 (/.f64 -1 re)) 1/4)) (Rewrite<= exp-to-pow_binary64 (exp.f64 (*.f64 (log.f64 (*.f64 1/2 (pow.f64 im 2))) 1/4)))) 2): 38 points increase in error, 22 points decrease in error
      (pow.f64 (Rewrite<= exp-sum_binary64 (exp.f64 (+.f64 (*.f64 (log.f64 (/.f64 -1 re)) 1/4) (*.f64 (log.f64 (*.f64 1/2 (pow.f64 im 2))) 1/4)))) 2): 21 points increase in error, 20 points decrease in error
      (pow.f64 (exp.f64 (Rewrite<= distribute-rgt-in_binary64 (*.f64 1/4 (+.f64 (log.f64 (/.f64 -1 re)) (log.f64 (*.f64 1/2 (pow.f64 im 2))))))) 2): 0 points increase in error, 0 points decrease in error

    if -5.21088124351897191e-8 < re

    1. Initial program 32.0

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified4.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)))))))): 126 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))))): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification10.7

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

Alternatives

Alternative 1
Error12.1
Cost19976
\[\begin{array}{l} t_0 := re + \mathsf{hypot}\left(re, im\right)\\ \mathbf{if}\;re \leq -3.629750173437626 \cdot 10^{+216}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{elif}\;re \leq -5.414473462324678 \cdot 10^{+169}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{t_0}\right)\\ \mathbf{elif}\;re \leq -5.210881243518972 \cdot 10^{-8}:\\ \;\;\;\;0.5 \cdot {\left(\frac{im \cdot \left(-im\right)}{re}\right)}^{0.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot t_0}\\ \end{array} \]
Alternative 2
Error25.3
Cost13512
\[\begin{array}{l} \mathbf{if}\;im \leq -3.6083740763147437 \cdot 10^{-134}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.3892458150341566 \cdot 10^{-90}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{re + im}\right)\\ \end{array} \]
Alternative 3
Error11.4
Cost13444
\[\begin{array}{l} \mathbf{if}\;re \leq -5.210881243518972 \cdot 10^{-8}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \end{array} \]
Alternative 4
Error25.6
Cost13384
\[\begin{array}{l} \mathbf{if}\;im \leq -3.6083740763147437 \cdot 10^{-134}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.3892458150341566 \cdot 10^{-90}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{im}\right)\\ \end{array} \]
Alternative 5
Error36.6
Cost7116
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\ t_1 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;re \leq 2.7100445237759135 \cdot 10^{-163}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.5466864866357182 \cdot 10^{-86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 6.26211975117037 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error25.6
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -3.6083740763147437 \cdot 10^{-134}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.3892458150341566 \cdot 10^{-90}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 7
Error25.3
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -3.6083740763147437 \cdot 10^{-134}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.3892458150341566 \cdot 10^{-90}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 8
Error25.9
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -3.6083740763147437 \cdot 10^{-134}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.3892458150341566 \cdot 10^{-90}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 9
Error58.9
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 9.734505188686609 \cdot 10^{-245}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot im\right) \cdot 4\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt[3]{im + im}\\ \end{array} \]
Alternative 10
Error43.8
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 1.071097947955499 \cdot 10^{-296}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot im\right) \cdot 4\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 11
Error59.8
Cost6720
\[0.5 \cdot \left|im + im\right| \]
Alternative 12
Error59.7
Cost448
\[0.5 \cdot \left(\left(im \cdot im\right) \cdot 4\right) \]
Alternative 13
Error59.6
Cost196
\[\begin{array}{l} \mathbf{if}\;im \leq 9.734505188686609 \cdot 10^{-245}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;im\\ \end{array} \]
Alternative 14
Error60.2
Cost64
\[0 \]

Error

Reproduce

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