Search code examples
programming-languagesintlong-integermaximize

How to represent a number greater than the maximum value


How to represent values that exceed the max values of any particular data type (int, long ) ?

I am thinking of having another storage space acting like a counter. Once the max value is crossed, the counter updates to say , the variable has exceeded the limit for "x" number of times. Is there some other efficient way of doing it ?

How do we display the exact value ?

P.S : Just a hypothetical question.


Solution

  • One way is to actually carve out one of the values for this purpose.

    For example, if you have a 16-bit integral type that can represent the values from 0 through 65535 inclusive, reduce the range to 0 through 65534 and use 65535 to represent the value "too darned big".

    You would have to be careful to control the operations so that they wouldn't produce that value in the normal course of events but that's reasonably easy if your language provides class capabilities.

    Alternatively, you can use the next biggest data type such as long for int or long long for long and use the extra range to store information.

    And, if you need more than that, you can code up a bignum library (or use one that already exists) so that there are no artificial limits placed on your numbers.