Click to See Complete Forum and Search --> : Division by zero


July 5th, 1999, 09:31 AM
Is that true that VC++ 6.0 compilator doesn't mind if I divide something by zero, or I've missed something? The following code works:



double a;
double b;
a = 10;
b = 0;
try
{
a = a / b;
}
catch (...)
{
// some code.
}






Division by zero wasn't even catched.

spotteddog
July 5th, 1999, 10:46 AM
In is not the job of the compiler to detect such things.

Dave Lorde
July 5th, 1999, 11:13 AM
According to the C++ Standard (5.5), if the result is not mathematically defined, or not in the range of representable values, the result is undefined (so the compiler can do anything). In the case of divide by zero and floating point exceptions, treatment of the result varies among machines, and may be adjustable via a library routine.

With floating-point arithmetic with VC++ on Intel PCs, division by zero gives a result value in 'a' of infinity, which is represented as 1.#INF. Integer types (e.g. int) will cause an exception to be thrown.

Dave

Dave Lorde
July 5th, 1999, 11:18 AM
You're right, of course, but I think he really meant the VC++ run-time code...

Whatever :-)

Dave