0

The following code will set x as "infinity"

#include <limits.h>
int x = INT_MAX;

when I say int x = 3;, the compiler is allocating some memory resources for variable x. Whats happening on the compiler side when I say int x = INT_MAX;`.

7
  • 2
    The following code will set x as "infinity" who said that?
    – this
    Dec 10, 2013 at 20:39
  • @self stackoverflow.com/questions/2273913/…
    – Ank
    Dec 10, 2013 at 20:39
  • 1
    Exactly the same; INT_MAX is just a fairly large number, but certainly not infinity. Dec 10, 2013 at 20:39
  • 1
    INT_MAX is not infinity, it is just a constant giving the largest number an "int" variable can hold. Basically the same thing happens with X=3 and X=INT_MAX
    – NealB
    Dec 10, 2013 at 20:41
  • 1
    @self. haha.. ya I should have done that..
    – Ank
    Dec 10, 2013 at 20:46

3 Answers 3

8

That will not set the value to infinity. With integers, there is no value that can represent infinity. Instead, it will set it to the largest value that an int can represent.

If int happens to be a 32-bit integer, then INT_MAX == 2147483647.

4

There can be no infinity value for ints, because ints have a fixed binary size. Suppose you have 9 digits to write any number you like. 9 digits is pretty big: it can go up to 999,999,999; but yet there's no way you can represent "infinity" with just that.

Integers on today's common platforms are made of 32 bits, so INT_MAX expands to the binary value 0111 1111 1111 1111 1111 1111 1111 1111, which is 231-1 (slightly above 2 billions). (the last bit is the sign bit and can't be used to make the number bigger).

To answer your actual question, no matter the value you put in an integer, the compiler always allocates the same size for it.

Floating-point numbers, on the other hand, are stranger beasts, and have a specific binary pattern to represent the infinity (or negative infinity) value. This is possible because of the way IEEE-794 numbers are represented in memory: there's a sign bit, some exponent bits, and then a mantissa. Any floating-point number, be it a float or a double, can be represented as ±(bit pattern) x 2^(exponent). However, there are also special bit patterns for values that are "not numbers" (also called NaN for Not a Number), like the result of 0.0 / 0.0, and special bit patterns for infinity values, like the result of any number other than 0 divided by 0. (This is not mathematically exact, but that's the way it works.) If I remember correctly, infinity is represented by having the exponent part to its maximum value, but I could be mistaken.

Mind you, it doesn't mean you can store any number in a floating-point number. Per the number of bits, there are "only" 2^64 possible distinct values that can be represented in a double, which is a crazy large number, but still not infinite. As you get to bigger numbers (or just more precise numbers), you start to see "holes" in the values you can represent. For instance, there is a, well, infinite step between the largest finite number you can put in a double and the infinity constant.

The point of having a value for infinity is to provide a constant that will necessarily be larger (or smaller in the case of negative infinity) than any other value. In that sense, even though it sounds more impressive than INT_MAX, it does pretty much the same thing.

Just like INT_MAX, there's a constant for it, which, unsurprisingly, is INFINITY.

2
  • Floating-point numbers, on the other hand, are stranger beasts, and have a specific binary pattern to represent the infinity (or negative infinity) value. Can you please elaborate on this..
    – Ank
    Dec 10, 2013 at 20:49
  • Yeah, I'll edit my answer with some more information on that.
    – zneak
    Dec 10, 2013 at 20:51
1

It is not setting int to infinity, it is setting it to the max value of an integer. 2,147,483,647 for a thirty two bit OS. It is storing essentially 31 1's and a sign bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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