Average Error: 38.2 → 26.2
Time: 28.4s
Precision: binary64
\[\]
\[\]
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 <= 6.700946626241751e-301) {
		tmp = 0.5 * (sqrt((im * im) * 2.0) / sqrt(sqrt((im * im) + (re * re)) - re));
	} else if (re <= 2.858449367873232e+66) {
		tmp = 0.5 * sqrt(2.0 * (re + (cbrt(sqrt((im * im) + (re * re))) * (cbrt(sqrt((im * im) + (re * re))) * cbrt(sqrt((im * im) + (re * re)))))));
	} else {
		tmp = 0.5 * sqrt(2.0 * (re + re));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original38.2
Target33.3
Herbie26.2
\[\]

Derivation

  1. Split input into 3 regimes
  2. if re < 6.70094662624175065e-301

    1. Initial program 45.5

      \[\]
    2. Using strategy rm
    3. Applied flip-+45.4

      \[\leadsto \]
    4. Applied associate-*r/45.4

      \[\leadsto \]
    5. Applied sqrt-div45.5

      \[\leadsto \]
    6. Simplified34.8

      \[\leadsto \]

    if 6.70094662624175065e-301 < re < 2.85844936787323206e66

    1. Initial program 20.9

      \[\]
    2. Using strategy rm
    3. Applied add-cube-cbrt21.3

      \[\leadsto \]

    if 2.85844936787323206e66 < re

    1. Initial program 45.1

      \[\]
    2. Taylor expanded around inf 11.9

      \[\leadsto \]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.2

    \[\leadsto \]

Reproduce

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