Average Error: 38.3 → 7.3
Time: 13.9s
Precision: binary64
Cost: 13444
\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} \mathbf{if}\;re \leq -1.046441282610084 \cdot 10^{+39}:\\ \;\;\;\;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 -1.046441282610084e+39)
   (* 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 <= -1.046441282610084e+39) {
		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 <= -1.046441282610084e+39) {
		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 <= -1.046441282610084e+39:
		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 (re <= -1.046441282610084e+39)
		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 <= -1.046441282610084e+39)
		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[re, -1.046441282610084e+39], 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 \leq -1.046441282610084 \cdot 10^{+39}:\\
\;\;\;\;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.3
Target33.5
Herbie7.3
\[\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.046441282610084e39

    1. Initial program 58.7

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

      \[\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)))))))): 139 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-rr39.6

      \[\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 26.5

      \[\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}} \]
    5. Simplified48.3

      \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{\mathsf{fma}\left(2, \log im, \log \left(\frac{-1}{re}\right)\right)}\right)}^{0.5}} \]
      Proof
      (pow.f64 (exp.f64 (fma.f64 2 (log.f64 im) (log.f64 (/.f64 -1 re)))) 1/2): 0 points increase in error, 0 points decrease in error
      (pow.f64 (exp.f64 (Rewrite<= fma-def_binary64 (+.f64 (*.f64 2 (log.f64 im)) (log.f64 (/.f64 -1 re))))) 1/2): 0 points increase in error, 0 points decrease in error
      (pow.f64 (exp.f64 (+.f64 (Rewrite<= log-pow_binary64 (log.f64 (pow.f64 im 2))) (log.f64 (/.f64 -1 re)))) 1/2): 7 points increase in error, 40 points decrease in error
      (pow.f64 (exp.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re)))) (Rewrite<= metadata-eval (*.f64 2 1/4))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= pow-sqr_binary64 (*.f64 (pow.f64 (exp.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re)))) 1/4) (pow.f64 (exp.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re)))) 1/4))): 7 points increase in error, 12 points decrease in error
      (*.f64 (Rewrite<= exp-prod_binary64 (exp.f64 (*.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re))) 1/4))) (pow.f64 (exp.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re)))) 1/4)): 1 points increase in error, 2 points decrease in error
      (*.f64 (exp.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/4 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re)))))) (pow.f64 (exp.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re)))) 1/4)): 0 points increase in error, 0 points decrease in error
      (*.f64 (exp.f64 (*.f64 1/4 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re))))) (Rewrite<= exp-prod_binary64 (exp.f64 (*.f64 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re))) 1/4)))): 1 points increase in error, 14 points decrease in error
      (*.f64 (exp.f64 (*.f64 1/4 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re))))) (exp.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/4 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re))))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= unpow2_binary64 (pow.f64 (exp.f64 (*.f64 1/4 (+.f64 (log.f64 (pow.f64 im 2)) (log.f64 (/.f64 -1 re))))) 2)): 0 points increase in error, 0 points decrease in error
    6. Applied egg-rr40.2

      \[\leadsto 0.5 \cdot {\color{blue}{\left({\left({\left(\frac{-1}{re} \cdot \left(im \cdot im\right)\right)}^{1.5}\right)}^{0.6666666666666666}\right)}}^{0.5} \]
    7. Applied egg-rr12.9

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

    if -1.046441282610084e39 < re

    1. Initial program 32.8

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified5.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)))))))): 139 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 simplification7.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -1.046441282610084 \cdot 10^{+39}:\\ \;\;\;\;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
Error26.6
Cost14172
\[\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|\\ t_2 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;im \leq -5.580850603196456 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -799693.4838634767:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -6.655822479294364 \cdot 10^{-103}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.1289079449687082 \cdot 10^{-300}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 4.0628426208765444 \cdot 10^{-132}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 2.011744924990974 \cdot 10^{-62}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.7563346312568003 \cdot 10^{-60}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\sqrt{re + im} \cdot \sqrt{2}\right)\\ \end{array} \]
Alternative 2
Error26.6
Cost14040
\[\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|\\ t_2 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{if}\;im \leq -5.580850603196456 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -799693.4838634767:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -6.655822479294364 \cdot 10^{-103}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -1.1289079449687082 \cdot 10^{-300}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 4.0628426208765444 \cdot 10^{-132}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 2.011744924990974 \cdot 10^{-62}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.7563346312568003 \cdot 10^{-60}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Error26.5
Cost7640
\[\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}\;im \leq -5.580850603196456 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -799693.4838634767:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -6.259835303791085 \cdot 10^{-191}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 4.0628426208765444 \cdot 10^{-132}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.011744924990974 \cdot 10^{-62}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{-1}{re}}\right)\\ \mathbf{elif}\;im \leq 2.7563346312568003 \cdot 10^{-60}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error26.8
Cost7512
\[\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}\;im \leq -5.580850603196456 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -799693.4838634767:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -6.259835303791085 \cdot 10^{-191}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 4.0628426208765444 \cdot 10^{-132}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.011744924990974 \cdot 10^{-62}:\\ \;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{-1}{re}}\right)\\ \mathbf{elif}\;im \leq 2.7563346312568003 \cdot 10^{-60}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 5
Error26.9
Cost7248
\[\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}\;im \leq -5.580850603196456 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -799693.4838634767:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -6.259835303791085 \cdot 10^{-191}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.7563346312568003 \cdot 10^{-60}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Error35.7
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -1.046441282610084 \cdot 10^{+39}:\\ \;\;\;\;0.5 \cdot \left(4 \cdot \left(im \cdot im\right)\right)\\ \mathbf{elif}\;re \leq 9.010547225204561 \cdot 10^{-83}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 7
Error43.9
Cost6852
\[\begin{array}{l} \mathbf{if}\;re \leq 6.395369297180332 \cdot 10^{-307}:\\ \;\;\;\;0.5 \cdot \left(4 \cdot \left(im \cdot im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 8
Error58.3
Cost6724
\[\begin{array}{l} \mathbf{if}\;im \cdot im \leq 4 \cdot 10^{-305}:\\ \;\;\;\;0.5 \cdot \left(4 \cdot \left(im \cdot im\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left|im\right|\\ \end{array} \]
Alternative 9
Error59.7
Cost576
\[0.5 \cdot \left(-1 + \left(1 + \left(im + im\right)\right)\right) \]
Alternative 10
Error59.6
Cost448
\[0.5 \cdot \left(4 \cdot \left(im \cdot im\right)\right) \]
Alternative 11
Error59.4
Cost196
\[\begin{array}{l} \mathbf{if}\;im \leq 1.011229484841618 \cdot 10^{-259}:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;im\\ \end{array} \]
Alternative 12
Error60.1
Cost64
\[0 \]

Error

Reproduce

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