Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I seem to remember that I was reading that

  • underflow means you have a too small magnitude that cannot be presented anymore in a type
  • overflow means you have a too large magnitude that cannot be presented anymore in a type

However, in practice I perceive that the terms are used such that

  • underflow means you have a too small value that cannot be presented anymore in a type
  • overflow means you have a too large value that cannot be presented anymore in a type

What is the correct meaning to use here? Are the terms defined differently for integer and floating point types?

share|improve this question
1  
Generally, the term "underflow" seems to be reserved for floating point arithmetic. With integers, I usually say "overflow" regardless of whether it's INT_MIN - 1 or INT_MAX + 1 –  Charles Salvia Dec 31 '10 at 14:41
add comment

3 Answers 3

up vote 8 down vote accepted

I can't really find an "authoritative" source on this matter, mostly because this is probably a matter of convention, and terminology is often very inconsistent. But, the following excerpt from Robert Seacord's "Secure Coding in C and C++" sums up my understanding of the situation:

An integer overflow occurs when an integer is increased beyond its maximum value or decreased beyond its minimum value3. Integer overflows are closely related to the underlying representation.

The footnote goes on to say:

[3] Decreasing an integer beyond its minimum value is often referred to as an integer underflow, although technically this term refers to a floating point condition.

The reason we call it an integer overflow is because there just isn't enough space available in the type to represent the value. In that sense, it's similar to a buffer overflow (except rather than actually crossing the buffer boundary, it usually exhibits wrap-around behavior.*) From this perspective, there is no conceptual difference between INT_MIN - 1 and INT_MAX + 1. In both cases there simply isn't enough space in the int data type to represent either value - so what we have is an overflow.

It also might be useful to note that in the x86 and x86_64 processor architectures, the flags register includes an overflow bit. The overflow bit is set when a signed integer arithmetic operation overflows. The expression INT_MIN - 1 will set the overflow bit. (There is no "underflow" bit.) So clearly, the engineers at AMD and Intel use the term "overflow" to describe the result of an integer arithmetic operation which has too many bits to fit in the data type, regardless of whether the value is numerically too large or too small.


*In fact, in C, signed integer overflow is actually undefined behavior, but in other languages such as Java, the two's complement arithmetic will wrap around.

share|improve this answer
add comment

It's an overflow. An underflow doesn't occur for integer values.

An overflow is when a value is too large (too far from zero) to be represented by the specific type, and an underflwo is when it's too small (too near to zero).

As the integer values closest to zero (1 and -1) can still be represented by any integer variable (assuming a signed integer with more than one bit), an underflow can't occur.

The Wikipedia article on underflow has a quite clear description:

"The term arithmetic underflow (or "floating point underflow", or just "underflow") is a condition in a computer program that can occur when the true result of a floating point operation is smaller in magnitude (that is, closer to zero) than the smallest value representable as a normal floating point number in the target datatype. Underflow can in part be regarded as negative overflow of the exponent of the floating point value."

share|improve this answer
    
It may be useful to note that underflow is often used specifically to refer to the particular condition where a number's magnitude is smaller than that of the smallest possible non-zero value, but larger than the smallest possible distance between non-zero values--in other words, cases where numbers fall into the what the Wiki article calls the "underflow gap". On IEEE-744-compliant implementations, the smallest representable number equals the smallest representable difference between numbers, so such underflows cannot occur, but outside the PC world, not all systems are IEEE-compliant. –  supercat Jul 12 '12 at 15:40
add comment

In integer math, overflow refers to both too-large and too-small values. In floating point, overflow refers to too large an exponent, and underflow refers to too small an exponent.

In fact, for integer types, CPUs have no way to tell the difference between overflow and underflow. Take the following 16-bit add:

  0x8000 (unsigned 32768, or signed -32767)
+ 0xFFFF (unsigned 65535, or signed -1)
--------
  0x7FFF (32767, the carried '1' is lost)

The overflow flag in the CPU would, of course, get set after this add. Using signed math, the result is too small (-32768). Using unsigned math, the result is too large (0x17FFF). Since 2's complement math is identical for signed and unsigned types, overflow is forced to mean both too-large and too-small values.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.