Average Error: 38.6 → 11.0
Time: 8.0s
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 -7.749794739352438 \cdot 10^{+37}:\\ \;\;\;\;0.5 \cdot \sqrt{-\frac{im}{\frac{re}{im}}}\\ \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 -7.749794739352438e+37)
   (* 0.5 (sqrt (- (/ im (/ re im)))))
   (* 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 <= -7.749794739352438e+37) {
		tmp = 0.5 * sqrt(-(im / (re / im)));
	} 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 <= -7.749794739352438e+37) {
		tmp = 0.5 * Math.sqrt(-(im / (re / im)));
	} 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 <= -7.749794739352438e+37:
		tmp = 0.5 * math.sqrt(-(im / (re / im)))
	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 <= -7.749794739352438e+37)
		tmp = Float64(0.5 * sqrt(Float64(-Float64(im / Float64(re / im)))));
	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 <= -7.749794739352438e+37)
		tmp = 0.5 * sqrt(-(im / (re / im)));
	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, -7.749794739352438e+37], N[(0.5 * N[Sqrt[(-N[(im / N[(re / im), $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 -7.749794739352438 \cdot 10^{+37}:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{im}{\frac{re}{im}}}\\

\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.6
Target33.7
Herbie11.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 2 regimes
  2. if re < -7.7497947393524382e37

    1. Initial program 58.9

      \[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
    2. Simplified39.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)))))))): 131 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. Taylor expanded in re around -inf 34.4

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

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{im \cdot \left(-im\right)}{re}}} \]
    5. Applied egg-rr28.9

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{-\frac{im}{\frac{re}{im}}}} \]

    if -7.7497947393524382e37 < re

    1. Initial program 33.0

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

      \[\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)))))))): 131 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 simplification11.0

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

Alternatives

Alternative 1
Error30.8
Cost8036
\[\begin{array}{l} t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \mathbf{if}\;re \leq -9.364736469535825 \cdot 10^{+219}:\\ \;\;\;\;0.5 \cdot \sqrt{-\frac{im}{\frac{re}{im}}}\\ \mathbf{elif}\;re \leq -3.329100729776363 \cdot 10^{-41}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq -3.8877389718254557 \cdot 10^{-169}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq -6.631904777433044 \cdot 10^{-273}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 6.210355846722803 \cdot 10^{-273}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 1.7776449730046537 \cdot 10^{-241}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;re \leq 8.256495022680632 \cdot 10^{-202}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;re \leq 5.758803920365891 \cdot 10^{-124}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;re \leq 5.109226825123245 \cdot 10^{-61}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \end{array} \]
Alternative 2
Error25.4
Cost7244
\[\begin{array}{l} \mathbf{if}\;im \leq -1.9575841222255597 \cdot 10^{-144}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.145302310832074 \cdot 10^{-274}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{elif}\;im \leq 1.3131842994099838 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Error25.1
Cost7244
\[\begin{array}{l} \mathbf{if}\;im \leq -2.2581109715354803 \cdot 10^{-139}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 1.145302310832074 \cdot 10^{-274}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\ \mathbf{elif}\;im \leq 1.3131842994099838 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error27.7
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -7.0084986620421235 \cdot 10^{-248}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.3131842994099838 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Error28.0
Cost7048
\[\begin{array}{l} \mathbf{if}\;im \leq -7.0084986620421235 \cdot 10^{-248}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.3131842994099838 \cdot 10^{-104}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Error41.7
Cost6984
\[\begin{array}{l} \mathbf{if}\;re \leq -3.329100729776363 \cdot 10^{-41}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{elif}\;re \leq 1.2500445460658377 \cdot 10^{+54}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 2}\\ \end{array} \]
Alternative 7
Error45.0
Cost6852
\[\begin{array}{l} \mathbf{if}\;im \leq 2.3061177811333807 \cdot 10^{-263}:\\ \;\;\;\;0.5 \cdot \sqrt{re \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 8
Error59.7
Cost6720
\[0.5 \cdot \sqrt{re \cdot 2} \]

Error

Reproduce

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