Average Error: 38.9 → 10.1
Time: 17.3s
Precision: binary64
Cost: 26884
\[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(\sqrt{re \cdot re + im \cdot im} + re\right)} \leq 0:\\ \;\;\;\;0.5 \cdot \left(-\left(-\left(3 \cdot \left(-im\right)\right) \cdot \sqrt{\frac{-1}{re}}\right) \cdot 0.3333333333333333\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) + re\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 (+ (sqrt (+ (* re re) (* im im))) re))) 0.0)
   (* 0.5 (- (* (- (* (* 3.0 (- im)) (sqrt (/ -1.0 re)))) 0.3333333333333333)))
   (* 0.5 (sqrt (* 2.0 (+ (hypot re im) re))))))
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 * (sqrt(((re * re) + (im * im))) + re))) <= 0.0) {
		tmp = 0.5 * -(-((3.0 * -im) * sqrt((-1.0 / re))) * 0.3333333333333333);
	} else {
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) + re)));
	}
	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 * (Math.sqrt(((re * re) + (im * im))) + re))) <= 0.0) {
		tmp = 0.5 * -(-((3.0 * -im) * Math.sqrt((-1.0 / re))) * 0.3333333333333333);
	} else {
		tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) + re)));
	}
	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 math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re))) <= 0.0:
		tmp = 0.5 * -(-((3.0 * -im) * math.sqrt((-1.0 / re))) * 0.3333333333333333)
	else:
		tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) + re)))
	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(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re))) <= 0.0)
		tmp = Float64(0.5 * Float64(-Float64(Float64(-Float64(Float64(3.0 * Float64(-im)) * sqrt(Float64(-1.0 / re)))) * 0.3333333333333333)));
	else
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) + re))));
	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 (sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))) <= 0.0)
		tmp = 0.5 * -(-((3.0 * -im) * sqrt((-1.0 / re))) * 0.3333333333333333);
	else
		tmp = 0.5 * sqrt((2.0 * (hypot(re, im) + re)));
	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[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * (-N[((-N[(N[(3.0 * (-im)), $MachinePrecision] * N[Sqrt[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]) * 0.3333333333333333), $MachinePrecision])), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] + re), $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(\sqrt{re \cdot re + im \cdot im} + re\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \left(-\left(-\left(3 \cdot \left(-im\right)\right) \cdot \sqrt{\frac{-1}{re}}\right) \cdot 0.3333333333333333\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) + re\right)}\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.9
Target33.7
Herbie10.1
\[\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.5

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

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

      \[\leadsto 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}} \]
    4. Taylor expanded in im around -inf 29.1

      \[\leadsto 0.5 \cdot \color{blue}{\left(-1 \cdot \left(\sqrt{\frac{-1}{re}} \cdot im\right)\right)} \]
    5. Simplified29.1

      \[\leadsto 0.5 \cdot \color{blue}{\left(-\sqrt{\frac{-1}{re}} \cdot im\right)} \]
      Proof
    6. Applied egg-rr29.1

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

      \[\leadsto 0.5 \cdot \left(-\color{blue}{\left(-\left(3 \cdot \left(-im\right)\right) \cdot \sqrt{\frac{-1}{re}}\right) \cdot 0.3333333333333333}\right) \]
      Proof

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

    1. Initial program 36.2

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) + re\right)}} \]
      Proof
  3. Recombined 2 regimes into one program.

Alternatives

Alternative 1
Error26.4
Cost7640
\[\begin{array}{l} t_0 := \sqrt{\frac{-1}{re}} \cdot im\\ t_1 := 0.5 \cdot t_0\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{if}\;im \leq -8.2 \cdot 10^{-85}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq -4.1 \cdot 10^{-190}:\\ \;\;\;\;0.5 \cdot \left(-t_0\right)\\ \mathbf{elif}\;im \leq 2 \cdot 10^{-306}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 8.2 \cdot 10^{-210}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 7.5 \cdot 10^{-152}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 2.05 \cdot 10^{-57}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 2
Error26.2
Cost7640
\[\begin{array}{l} t_0 := \sqrt{\frac{-1}{re}} \cdot im\\ t_1 := 0.5 \cdot t_0\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{if}\;im \leq -1.32 \cdot 10^{-79}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left(-im\right)\right)}\\ \mathbf{elif}\;im \leq -1.4 \cdot 10^{-190}:\\ \;\;\;\;0.5 \cdot \left(-t_0\right)\\ \mathbf{elif}\;im \leq 2.7 \cdot 10^{-306}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 5.2 \cdot 10^{-216}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.15 \cdot 10^{-151}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{-58}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Error26.2
Cost7640
\[\begin{array}{l} t_0 := \sqrt{\frac{-1}{re}}\\ t_1 := t_0 \cdot im\\ t_2 := 0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{if}\;im \leq -2.6 \cdot 10^{-81}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left(-im\right)\right)}\\ \mathbf{elif}\;im \leq -2.5 \cdot 10^{-190}:\\ \;\;\;\;0.5 \cdot \left(-t_1\right)\\ \mathbf{elif}\;im \leq 4.1 \cdot 10^{-306}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 9.2 \cdot 10^{-215}:\\ \;\;\;\;0.5 \cdot \left(\left(\left(3 \cdot im\right) \cdot t_0\right) \cdot 0.3333333333333333\right)\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{-151}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 4.6 \cdot 10^{-58}:\\ \;\;\;\;0.5 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 4
Error26.2
Cost7640
\[\begin{array}{l} t_0 := \sqrt{\frac{-1}{re}}\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{if}\;im \leq -3.2 \cdot 10^{-82}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left(-im\right)\right)}\\ \mathbf{elif}\;im \leq -1.5 \cdot 10^{-190}:\\ \;\;\;\;0.5 \cdot \left(-\left(-\left(3 \cdot \left(-im\right)\right) \cdot t_0\right) \cdot 0.3333333333333333\right)\\ \mathbf{elif}\;im \leq 2.4 \cdot 10^{-306}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.45 \cdot 10^{-213}:\\ \;\;\;\;0.5 \cdot \left(\left(\left(3 \cdot im\right) \cdot t_0\right) \cdot 0.3333333333333333\right)\\ \mathbf{elif}\;im \leq 1.6 \cdot 10^{-152}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.6 \cdot 10^{-58}:\\ \;\;\;\;0.5 \cdot \left(t_0 \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 5
Error26.6
Cost7508
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\sqrt{\frac{-1}{re}} \cdot im\right)\\ t_1 := 0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{if}\;im \leq -2.45 \cdot 10^{-85}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 1.8 \cdot 10^{-306}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{-209}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.8 \cdot 10^{-152}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{-58}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 6
Error26.6
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -2.35 \cdot 10^{-85}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 1.6 \cdot 10^{-150}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 7
Error26.2
Cost7112
\[\begin{array}{l} \mathbf{if}\;im \leq -1.35 \cdot 10^{-85}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{elif}\;im \leq 1.05 \cdot 10^{-150}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 8
Error31.0
Cost6916
\[\begin{array}{l} \mathbf{if}\;im \leq -5 \cdot 10^{-310}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-im\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\ \end{array} \]
Alternative 9
Error47.2
Cost6720
\[0.5 \cdot \sqrt{2 \cdot im} \]

Error

Reproduce

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