Take the 2-minute tour ×

Regarding division by zero, the standards say:


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.

share|improve this question
Here is another well-known joke: If x * 0 = y then how to find x ? –  psihodelia Jun 9 '10 at 8:31
@paxdiablo: how have you canceled y/(xy) ? You must get 1/x, but not x –  psihodelia Jun 9 '10 at 9:27
@psihodelia - i would say that x * 0 = y, find x would be the mathematical equivalent of a rhetorical question. x is anything or nothing, and therefore it has no answer while not requiring an answer just the same. –  pxl Jun 9 '10 at 9:49
@paxdiablo: If you divide by x you have to assume x != 0 as well. –  u0b34a0f6ae Jun 9 '10 at 12:21

6 Answers 6

up vote 14 down vote accepted

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.

share|improve this answer
Agreed. 1 / 0; and similar expressions in no way involve the macro INFINITY (unless by chance the implementation voluntarily chooses to use that to resolve the undefined behavior). –  Matthew Flaschen Jun 9 '10 at 8:29
@Matthew: regarding your parenthesized comment: my findings show that this is indeed the way that many (most?) implementations choose to resolve this. Not to say that makes it implementation defined behavior, but it does add to the confusion. –  SiegeX Jun 9 '10 at 8:43
Not with GCC and friends. I ran #include <math.h> int div_0_i = 1 / 0; float div_0_f = 1.0f / 0.0f; float my_inf = INFINITY; through GNU cpp, and got int div_0_i = 1 / 0; float div_0_f = 1.0f / 0.0f; float my_inf = (__builtin_inff()); So I don't see a connection. –  Matthew Flaschen Jun 9 '10 at 8:49
@Matthew: Apparently gcc is not smart enough to replace div zero at pre-processor time but compile and run this code and you'll see what I mean. --> #include <math.h> float f = 1.0f / 0.0f; if(f == INFINITY) puts("f == INFINITY"); –  SiegeX Jun 9 '10 at 16:58
@SiegeX, note that - as @Kenny pointed out in his answer - this may well simply be used as a signal of "overflow", aka "invalid value", not a computation result per se. –  Péter Török Jun 9 '10 at 20:19

1 / 0 is not infinity, only

lim 1/x = ∞ (x -> 0)

share|improve this answer
this answer is not programming related. –  Pete Kirkham Jun 9 '10 at 8:25
... it's a perfectly valid answer given the question. –  Peter Alexander Jun 9 '10 at 8:29
This is incorrect, it's only true if x is limited to positive values. –  interjay Jun 9 '10 at 8:31
@interjay: you're right, i should have put there |lim 1/x| but the point is still there isnt it :) –  František Žiačik Jun 9 '10 at 8:41
@Pete Alexander the question is "Division by zero: Undefined Behavior or Implementation Defined in C and/or C++ ?", not whether the behaviour in the standards agrees with the mathematical concept. The standard uses HUGE_VAL for such limits, which is the same bit pattern as infinity in IEE745 implementations. –  Pete Kirkham Jun 9 '10 at 9:03

Why would it?

That doesn't make sense mathematically, it's not as if 1/x is defined as ∞ in mathematics in general. Also, you would at least need two more cases: -1/x and 0/x can't also equal ∞.

See division by zero in general, and the section about computer arithmetic in particular.

share|improve this answer
I would contend that y = 1/x does equal ∞ as x --> 0 –  SiegeX Jun 9 '10 at 8:25
This is an incredibly common mistake (I'm guessing that people who never actually took a formal calculus course are more likely to make it.) SiegeX, C/C++ doesn't have the concept of "-->" –  shoosh Jun 9 '10 at 8:26
@shoosh: are you saying that the lim [x --> +0] f(y) != ∞ where f(y) = 1/x ? –  SiegeX Jun 9 '10 at 8:30
@shoosh: sure it does, haven't you seen this SO question stackoverflow.com/questions/1642028/…? =P –  SiegeX Jun 9 '10 at 8:32
@SiegeX: I would contend that division by zero is undefined in maths.y = 1/x tends to infinity if x is positive and tends to 0. However, it tends to -infinity if x is negative and tends to 0. Which one do you choose? The answer is neither since division is defined mathematically in terms of multiplication i.e. d = p/q is defined as the number which multiplied by q gives p and no number times 0 can give anything but 0. –  JeremyP Jun 9 '10 at 8:48

I've only got the C99 draft. In §7.12/4 it says:

The macro


expands to a constant expression of type float representing positive or unsigned infinity, if available; else to a positive constant of type float that overflows at translation time.

Note that INFINITY can be defined in terms of floating-point overflow, not necessarily divide-by-zero.

share|improve this answer

For the INFINITY macro: there is a explicit coding to represent +/- infinity in the IEEE754 standard, which is if all exponent bits are set and all fraction bits are cleared (if a fraction bit is set, it represents NaN)

With my compiler, (int) INFINITY == -2147483648, so an expression that evaluates to int i = 1/0 would definitely produce wrong results if INFINITIY was returned

share|improve this answer

Bottom line, C99 (as per your quotes) does not say anything about INFINITY in the context of "implementation-defined". Secondly, what you quoted does not show inconsistent meaning of "undefined behavior".

[Quoting Wikipedia's Undefined Behavior page] "In C and C++, implementation-defined behavior is also used, where the language standard does not specify the behavior, but the implementation must choose a behavior and needs to document and observe the rules it chose."

More precisely, the standard means "implementation-defined" (I think only) when it uses those words with respect to the statement made since "implementation-defined" is a specific attribute of the standard. The quote of C99 7.12p4 didn't mention "implementation-defined".

[From C99 std (late draft)] "undefined behavior: behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements"

Note there is "no requirement" imposed for undefined behavior!

[C99 ..] "implementation-defined behavior: unspecified behavior where each implementation documents how the choice is made"

[C99 ..] "unspecified behavior: use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance"

Documentation is a requirement for implementation-defined behavior.

share|improve this answer

Your Answer


