c

Storing value infinity to a variable


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;`.


Solution

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