?

Average Error: 38.5 → 9.7
Time: 8.2s
Precision: binary64
Cost: 27401

?

\[0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \]
\[\begin{array}{l} t_0 := re + \sqrt{re \cdot re + im \cdot im}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-301} \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (+ re (sqrt (+ (* re re) (* im im))))))
   (if (or (<= t_0 -2e-301) (not (<= t_0 0.0)))
     (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))
     (* 0.5 (/ im (sqrt (- 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 t_0 = re + sqrt(((re * re) + (im * im)));
	double tmp;
	if ((t_0 <= -2e-301) || !(t_0 <= 0.0)) {
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	} else {
		tmp = 0.5 * (im / sqrt(-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 t_0 = re + Math.sqrt(((re * re) + (im * im)));
	double tmp;
	if ((t_0 <= -2e-301) || !(t_0 <= 0.0)) {
		tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
	} else {
		tmp = 0.5 * (im / Math.sqrt(-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):
	t_0 = re + math.sqrt(((re * re) + (im * im)))
	tmp = 0
	if (t_0 <= -2e-301) or not (t_0 <= 0.0):
		tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im))))
	else:
		tmp = 0.5 * (im / math.sqrt(-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)
	t_0 = Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im))))
	tmp = 0.0
	if ((t_0 <= -2e-301) || !(t_0 <= 0.0))
		tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im)))));
	else
		tmp = Float64(0.5 * Float64(im / sqrt(Float64(-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)
	t_0 = re + sqrt(((re * re) + (im * im)));
	tmp = 0.0;
	if ((t_0 <= -2e-301) || ~((t_0 <= 0.0)))
		tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
	else
		tmp = 0.5 * (im / sqrt(-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_] := Block[{t$95$0 = N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -2e-301], N[Not[LessEqual[t$95$0, 0.0]], $MachinePrecision]], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := re + \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-301} \lor \neg \left(t_0 \leq 0\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.5
Target33.9
Herbie9.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 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < -2.00000000000000013e-301 or 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re)

    1. Initial program 35.9

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
      Proof

      [Start]35.9

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

      +-commutative [=>]35.9

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

      hypot-def [=>]7.1

      \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)} \]

    if -2.00000000000000013e-301 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0

    1. Initial program 57.0

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \]
      Proof

      [Start]57.0

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

      +-commutative [=>]57.0

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

      hypot-def [=>]56.5

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

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

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

      [Start]32.0

      \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)} \]

      *-commutative [=>]32.0

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

      unpow2 [=>]32.0

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{\frac{-im \cdot im}{re}}\right)} - 1\right)} \]
    6. Simplified32.0

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

      [Start]53.4

      \[ 0.5 \cdot \left(e^{\mathsf{log1p}\left(\sqrt{\frac{-im \cdot im}{re}}\right)} - 1\right) \]

      expm1-def [=>]32.1

      \[ 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{\frac{-im \cdot im}{re}}\right)\right)} \]

      expm1-log1p [=>]32.0

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

      distribute-rgt-neg-in [=>]32.0

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

      \[\leadsto 0.5 \cdot \color{blue}{\frac{im}{\sqrt{-re}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification9.7

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

Alternatives

Alternative 1
Error25.9
Cost7509
\[\begin{array}{l} \mathbf{if}\;im \leq -4 \cdot 10^{-145}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.5 \cdot 10^{-226}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 2 \cdot 10^{-188} \lor \neg \left(im \leq 5 \cdot 10^{-136}\right) \land im \leq 3.15 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 2
Error25.6
Cost7509
\[\begin{array}{l} \mathbf{if}\;im \leq -3.1 \cdot 10^{-144}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\ \mathbf{elif}\;im \leq 3.4 \cdot 10^{-224}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 1.42 \cdot 10^{-188} \lor \neg \left(im \leq 2.45 \cdot 10^{-136}\right) \land im \leq 4.8 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\ \end{array} \]
Alternative 3
Error26.0
Cost7180
\[\begin{array}{l} \mathbf{if}\;im \leq -1.35 \cdot 10^{-134}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 2.4 \cdot 10^{-220}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{elif}\;im \leq 9.2 \cdot 10^{-96}:\\ \;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 4
Error37.3
Cost7117
\[\begin{array}{l} \mathbf{if}\;re \leq 1.75 \cdot 10^{-206} \lor \neg \left(re \leq 6.2 \cdot 10^{-78}\right) \land re \leq 1.18 \cdot 10^{+14}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \end{array} \]
Alternative 5
Error26.1
Cost6984
\[\begin{array}{l} \mathbf{if}\;im \leq -4.2 \cdot 10^{-140}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\ \mathbf{elif}\;im \leq 1.22 \cdot 10^{-163}:\\ \;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\ \end{array} \]
Alternative 6
Error47.2
Cost6720
\[0.5 \cdot \sqrt{im \cdot 2} \]

Error

Reproduce?

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