What are arithmetic underflow and overflow in C?

56,086

Solution 1

Overflow

From http://en.wikipedia.org/wiki/Arithmetic_overflow:

the condition that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.

So, for instance:

uint32_t x = 1UL << 31;
x *= 2;  // Overflow!

Note that as @R mentions in a comment below, the C standard suggests:

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

Of course, this is a fairly idiosyncratic definition of "overflow". Most people would refer to modulo reduction (i.e wrap-around) as "overflow".

Underflow

From http://en.wikipedia.org/wiki/Arithmetic_underflow:

the 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.

So, for instance:

float x = 1e-30;
x /= 1e20; // Underflow!

Solution 2

Computers use only 0 and 1 to represent data so that the range of values that can be represented is limited. Many computers use 32 bits to store integers, so the largest unsigned integer that can be stored in this case is 2^32 -1 = 4294967295. But the first bit is used to represent the sign, so, in fact, the largest value is 2^31 - 1 = 2147483647.

The situation where an integer outside the allowed range requires more bits than can be stored is called an overflow.

Similarly, with real numbers, an exponent that is too small to be stored causes an underflow.

Share:
56,086

Related videos on Youtube

Registered User
Author by

Registered User

Never let the fear of falling down keep you away from playing the game

Updated on August 11, 2020

Comments

  • Registered User
    Registered User almost 4 years

    What do arithmetic underflow and overflow mean in C programming?

  • Oliver Charlesworth
    Oliver Charlesworth about 13 years
    A computer normally has no checks that an operation resulted in overflow. And where did you get 23 from?
  • Philip
    Philip about 13 years
    int is not a 32-bit data type.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE about 13 years
    Your example of overflow is wrong in the language of the C standard. Unsigned types cannot overflow.
  • Oliver Charlesworth
    Oliver Charlesworth about 13 years
    @R.: "Overflow" certainly has a well-understood meaning independent of the C standard! But you're right, so I'll include that factoid in my answer.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE about 13 years
    The distinction is mainly important when people say things like "integer overflow always results in undefined behavior".
  • Michael Carman
    Michael Carman about 11 years
    @OliCharlesworth: Under IEEE 754 a single-precision floating-point number has a 23 bit (not digit) significand. There's also an implicit bit which normally gives you 24 bits of precision but that doesn't apply during underflow, which uses subnormal numbers to provide a gradual loss of precision.
  • Michael Carman
    Michael Carman about 11 years
    Your description of underflow is completely wrong. Floating-point numbers consist of three fields: sign, significand (fraction), and exponent. The number of bits in the significand determine the precision (roughly 7 decimal characters for a 32-bit float). The number of bits in the exponent determine the range (smallest to largest) of representable values. The location of the decimal point is determined by the value of the exponent and independent of precision. Underflow occurs when the value to be stored is smaller in magnitude than the minimum value that the exponent can support.
  • Curd
    Curd over 10 years
    @Oli Charlesworth: normally every CPU has an overflow flag in its status register. (Maybe you meant that C doesn't care about this)
  • Oliver Charlesworth
    Oliver Charlesworth over 10 years
    @Curd: Yeah, I can't remember what I meant at the time! Hopefully I meant that you couldn't access the status flag from C...
  • bolov
    bolov over 9 years
    you are mixing things: arithmetic underflow with stack overflow and putting them together, making your answer confusing at best. I am not sure I agree with you definitions either.
  • GingerPlusPlus
    GingerPlusPlus over 9 years
    Your answer say only what's the difference between them, but doesn't say what they are; the question's title is "what is underflow and over flow"
  • Gamal Othman
    Gamal Othman over 5 years
    What about unsigned char x = 0; x -= 1; ? is this count as an underflow?
  • Gamal Othman
    Gamal Othman over 5 years
    I found on Wikipedia that it is called "Integer Overflow" or "Integer Wraparound". It says: Note that storing values that are too low in an integer variable (e.g. attempting to store -1 in an unsigned integer) is properly referred to as integer overflow, or more broadly "integer wraparound". The term underflow normally refers to floating point numbers only, and is a separate issue. Thanks!
  • hamid kavianathar
    hamid kavianathar about 4 years
    the definition of underflow is not completely correct.