Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Regarding division by zero, the standards say:

C99 6.5.5p5 - The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

C++03 5.6.4 - The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined.

If we were to take the above paragraphs at face value, the answer is clearly Undefined Behavior for both languages. However, if we look further down in the C99 standard we see the following paragraph which appears to be contradictory(1):

C99 7.12p4 - The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available;

Do the standards have some sort of golden rule where Undefined Behavior cannot be superseded by a (potentially) contradictory statement? Barring that, I don't think it's unreasonable to conclude that if your implementation defines the INFINITY macro, division by zero is defined to be such. However, if your implementation does not define such a macro, the behavior is Undefined.

I'm curious what the consensus is (if any) on this matter for each of the two languages. Would the answer change if we are talking about integer division int i = 1 / 0 versus floating point division float i = 1.0 / 0.0 ?

Note (1) The C++03 standard talks about the <cmath> library which includes the INFINITY macro.

like image 481
SiegeX Avatar asked Jun 09 '10 08:06

SiegeX


People also ask

Is division by zero undefined behavior?

Regarding division by zero, the standards say: C99 6.5. 5p5 - The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

What happens if you divide by zero in C?

Because what happens is that if we can say that zero, 5, or basically any number, then that means that that "c" is not unique. So, in this scenario the first part doesn't work. So, that means that this is going to be undefined. So zero divided by zero is undefined.

How is the division by zero defined in programming?

Division by zero is the operation of taking the quotient of any number and 0, i.e., . The uniqueness of division breaks down when dividing by zero, since the product is the same for any , so. cannot be recovered by inverting the process of multiplication.

What type of error is division by zero in C?

Dividing a number by Zero is a mathematical error (not defined) and we can use exception handling to gracefully overcome such operations. If you write a code without using exception handling then the output of division by zero will be shown as infinity which cannot be further processed.


3 Answers

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.

like image 153
Péter Török Avatar answered Oct 03 '22 13:10

Péter Török


1 / 0 is not infinity, only

lim 1/x = ∞ (x -> +0)
like image 38
František Žiačik Avatar answered Oct 03 '22 12:10

František Žiačik


This was not a math purest question, but a C/C++ question.

  • According to the IEEE 754 Standard, which all modern C compilers / FPU's use, we have
    • 3.0 / 0.0 = INF
    • 0.0 / 0.0 = NaN
    • -3.0 / 0.0 = -INF

The FPU will have a status flag that you can set to generate an exception if so desired, but this is not the norm.

INF can be quite useful to avoiding branching when INF is a useful result. See discussion here

http://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF

like image 41
wcochran Avatar answered Oct 03 '22 13:10

wcochran