From bug-octave-request at bevo dot che dot wisc dot edu Mon Oct 16 23:46:55 1995 Subject: bug in Complex operator / (double, const Complex&) From: John Eaton To: bug-lib-g++ at prep dot ai dot mit dot edu cc: bug-octave at bevo dot che dot wisc dot edu Date: Mon, 16 Oct 1995 23:46:50 -0500 Using libg++ 2.7.0 and g++ 2.7.0 on a DEC Alpha running OSF 3.2, the following program incorrectly reports a division by zero: $ cat foo.cc #include #include int main (void) { Complex x (1.0, 0.0); Complex y (1.0e-200, 1.0e-200); cout << x / y << "\n"; cout << 1.0 / y << "\n"; return 0; } $ g++ -o foo foo.cc -lm $ ./foo (5e+199, -5e+199) Fatal Complex arithmetic error. Attempted division by zero. Here's a fix. It simply uses the same trick in the definition of operator / (double, const Complex&) as is used in the definition of operator / (const Complex&, const Complex&). With it, the program above prints: (5e+199, -5e+199) (5e+199, -5e+199) *** libg++-2.7.0/libg++/src/Complex.cc Fri Jun 16 16:43:09 1995 --- Complex.cc Mon Oct 16 23:35:47 1995 *************** *** 74,82 **** Complex /* const */ operator / (double x, const Complex& y) { ! double den = norm(y); if (den == 0.0) y.error ("Attempted division by zero."); ! return Complex((x * y.real()) / den, -(x * y.imag()) / den); } Complex /* const */ operator / (const Complex& x, double y) --- 74,86 ---- Complex /* const */ operator / (double x, const Complex& y) { ! double den = fabs(y.real()) + fabs(y.imag()); if (den == 0.0) y.error ("Attempted division by zero."); ! double xrden = x / den; ! double yrden = y.real() / den; ! double yiden = y.imag() / den; ! double nrm = yrden * yrden + yiden * yiden; ! return Complex(xrden * yrden / nrm, -xrden * yiden / nrm); } Complex /* const */ operator / (const Complex& x, double y) Thanks, jwe