Average Error: 31.6 → 18.6
Time: 1.3s
Precision: binary64
\[\sqrt{re \cdot re + im \cdot im}\]
\[\begin{array}{l} \mathbf{if}\;re \leq -2.2647948816248014 \cdot 10^{+66}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \leq 3.285560521583026 \cdot 10^{+62}:\\ \;\;\;\;\sqrt{re \cdot re + im \cdot im}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]
\sqrt{re \cdot re + im \cdot im}
\begin{array}{l}
\mathbf{if}\;re \leq -2.2647948816248014 \cdot 10^{+66}:\\
\;\;\;\;-re\\

\mathbf{elif}\;re \leq 3.285560521583026 \cdot 10^{+62}:\\
\;\;\;\;\sqrt{re \cdot re + im \cdot im}\\

\mathbf{else}:\\
\;\;\;\;re\\

\end{array}
(FPCore (re im) :precision binary64 (sqrt (+ (* re re) (* im im))))
(FPCore (re im)
 :precision binary64
 (if (<= re -2.2647948816248014e+66)
   (- re)
   (if (<= re 3.285560521583026e+62) (sqrt (+ (* re re) (* im im))) re)))
double code(double re, double im) {
	return ((double) sqrt(((double) (((double) (re * re)) + ((double) (im * im))))));
}
double code(double re, double im) {
	double tmp;
	if ((re <= -2.2647948816248014e+66)) {
		tmp = ((double) -(re));
	} else {
		double tmp_1;
		if ((re <= 3.285560521583026e+62)) {
			tmp_1 = ((double) sqrt(((double) (((double) (re * re)) + ((double) (im * im))))));
		} else {
			tmp_1 = re;
		}
		tmp = tmp_1;
	}
	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

Derivation

  1. Split input into 3 regimes
  2. if re < -2.26479488162480145e66

    1. Initial program 45.5

      \[\sqrt{re \cdot re + im \cdot im}\]
    2. Taylor expanded around -inf 13.0

      \[\leadsto \color{blue}{-1 \cdot re}\]
    3. Simplified13.0

      \[\leadsto \color{blue}{-re}\]

    if -2.26479488162480145e66 < re < 3.28556052158302614e62

    1. Initial program 22.4

      \[\sqrt{re \cdot re + im \cdot im}\]

    if 3.28556052158302614e62 < re

    1. Initial program 45.9

      \[\sqrt{re \cdot re + im \cdot im}\]
    2. Taylor expanded around inf 12.5

      \[\leadsto \color{blue}{re}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification18.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -2.2647948816248014 \cdot 10^{+66}:\\ \;\;\;\;-re\\ \mathbf{elif}\;re \leq 3.285560521583026 \cdot 10^{+62}:\\ \;\;\;\;\sqrt{re \cdot re + im \cdot im}\\ \mathbf{else}:\\ \;\;\;\;re\\ \end{array}\]

Reproduce

herbie shell --seed 2020219 
(FPCore (re im)
  :name "math.abs on complex"
  :precision binary64
  (sqrt (+ (* re re) (* im im))))