0

This is a two part question.

  1. I have a small code that adds numbers; however the numbers often grow faster than long long can hold.

    So I thought I'd make a class to move it over.

    My thought process is given 3 long long a, b, c;,

    Add everything to c and when c is at capacity, add one to b and set c to zeros; once b is "full" add one to a and set b and c to zero and so on.

    So, I have to check to see if c is at capacity before I reset it and add one to b and so on!

    Is there a way to check for that?

  2. Can someone point me to the right direction to make my own data type. Visually, I put 3 long longs together like the above and the data type would do what I talk about above.

Finally, I would like to treat my new data type just like i do to ints:

int a = 0;

I would like to be able to do

mydatatype a = 0;

English is not my first language.

Cripto
  • 3,581
  • 7
  • 41
  • 65
  • also have a look at BigInteger in libraries such as [GMP](http://stackoverflow.com/questions/124332/c-handling-very-large-integers) – StuartLC Nov 11 '12 at 05:34
  • Your best bet would be to use types that are not the maximum possible size. In other words, if you have two arrays of bytes representing large integers, take them 16 bits at a time and perform your math with 32 bit types. It's easy to check if the 32 bit result wouldn't fit into a 16 bit integer, and thus overflow handling is easy. – Wug Nov 11 '12 at 05:35
  • Let's see: if you create numbers at 1 GHz or 1e9 per second and you run your program for 100 years, then you'll generate about 3e18 numbers, give or take. The range on a 64-bit 'signed long long' is 9e18. So, to be running out of the range, you either have even faster counters/generators, or you have multiple number generators running in parallel, or you have jumps in the numbers. – Jonathan Leffler Nov 11 '12 at 05:42

3 Answers3

2

Header <limits.h> defines, amongst other things:

  • LLONG_MAX — the maximum value that can be stored in a (signed) long long. The minimum acceptable value for it is +9223372036854775807 (263-1).
  • ULLONG_MAX — the maximum value that can be stored in an unsigned long long. The minimum acceptable value for it is 18446744073709551615 (264-1).

You can test whether your numbers have reached these limits by comparing them with the appropriate one of these values.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

Testing if an integer is at capacity means that your code will be very inefficient. For example, to add 123 you'd need to do 123 increments and 123 comparisons.

A better way would be to determine if the operation will overflow before performing the operation. For example (for unsigned integers only):

if(sum <= ULLONG_MAX - a) {
    sum += a;
} else {
    /* It would have overflowed */
}

This works because ULLONG_MAX - a can not overflow. When you start looking at signed integers it becomes a larger problem because LLONG_MAX - a can overflow (if a is negative), and LLONG_MIN - a can also overflow (if a is positive). You need to test both ways:

if( ( a > 0) && (sum <= LLONG_MAX - a) {
    sum += a;
} else if( ( a < 0) && (sum >= LLONG_MIN - a) {
    sum += a;
} else if( a != 0) {
    /* It would have overflowed */
}

Once you've determined if it would have overflowed you need to handle that case. For example; if you're using multiple integers to represent one larger integer; then (for unsigned integers):

if(sum_low <= ULLONG_MAX - a) {
    sum_low += a;
} else {
    sum_low -= (ULLONG_MAX - a) + 1;
    sum_high++;
}

Please note that you have to be very careful to avoid accidental overflows in temporary calculations involved in handling the original (avoided) overflow.

If you use multiple signed integers to represent one larger signed integer, then the logic behind overflow handling becomes complex and error-prone. It is theoretically possible; however it's far better to separate the signs of the numbers and then do operation (or its inverse - e.g. subtracting a positive number instead of adding a negative number) on unsigned integers alone; especially if you need to use 3 or more integers to represent one huge integer, or if you need more complex operations like multiplication and division.

Of course once you start down this path you're effectively implementing your own "big number" code, and should probably find/use a suitable library instead.

Finally, if you'd like to treat your new data type like primitive data types (e.g. and be able to do things like mydatatype a = 0;) then you're out of luck - C doesn't work this way. Essentially, C is beautiful because it doesn't allow complex things to be hidden from people trying to read/understand the code; and you'd have to use a less beautiful language like C++ if you want to hide important information from unsuspecting victims. ;-)

Brendan
  • 35,656
  • 2
  • 39
  • 66
1

If you're talking about signed numbers, you could tell if a number is at "capacity" by doing this:

if (c > 0 && c + x < 0)

For unsigned numbers it would be similar:

if (c + x < c)

In both cases you'd just check for overflow. After doing that, if there is overflow, then you'd add 1 to b and put the remainder (MAX_LONG_LONG - c) back into c. So on for the transition from b to a.

I can't think of an easy way to do this without just creating your own struct and writing add and subtract as functions around those structs.

Chris Hayes
  • 11,471
  • 4
  • 32
  • 47