a Few Things About Division by Zero in C

A few things about division by zero in C

Floating point division by zero behaves differently than integer division by zero.

The IEEE floating point standard differentiates between +inf and -inf, while integers cannot store infinity. Integer division by zero results in undefined behaviour. Floating point division by zero is defined by the floating point standard and results in +inf or -inf.

Edit:

As pointed out by Luchian, C++ implementations are not required to follow the IEEE Floating point standard. If the implementation you use doesn't follow the IEEE Floating point standard the result of floating point division by zero is undefined.

Division by zero: Undefined Behavior or Implementation Defined in C and/or C++?

I don't see any contradiction. Division by zero is undefined, period. There is no mention of "... unless INFINITY is defined" anywhere in the quoted text.

Note that nowhere in mathematics it is defined that 1 / 0 = infinity. One might interpret it that way, but it is a personal, "shortcut" style interpretation, rather than a sound fact.

Integer division by zero c++

As ooga mentions in his comment, 1/2 (and 0/2) will equal zero due to integer math.

To avoid division by 0 here, change cin >> num; to:

do
{
cin >> num;
} while (num <= 1);

This will continue prompting until a valid number is input.

EDIT: Cornstalk's Answer properly points out that you must guard the inner-most while loop with i > 0 && num % i == 0.

division by zero program crash in c++

The best "reason" is that, per the language, division by zero is undefined behavior, so anything could happen. A crash is the most useful "anything" to the programmer since it helps you catch the bug.

As for why the C++ language left division by zero undefined, that probably comes from C which does the same, and in turn from the facts that (1) there is no mathematical definition, and (2) different hardware might do different things when dividing by zero, and since there's no reason a valid program should be making the division by zero, it's not worth adding an emulation/patch-up layer around every division to force a common behavior on all platforms.

Why doesn't 'd /= d' throw a division by zero exception when d == 0?

C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:

  1. The compiler assumes Undefined Behavior doesn't happen
  2. Division by Zero in C++ is undefined behavior
  3. Therefore, code which can cause a Division by Zero is presumed to not do so.

    • And, code which must cause a Division by Zero is presumed to never happen
  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen
  5. Therefore, d / d must always equal 1.

However...

We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.

volatile int d = 0;
d /= d; //What happens?

So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.

Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.

There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.



Related Topics



Leave a reply



Submit