# Thread: Very large signed integer math question

1. ## Very large signed integer math question

Here's a tough one...

How would one go about performing addition, subtraction, and multiplication on integers that are larger than the built in integer types for C? I've been thinking about this for a while, and cant seem to come up with a good answer.

Any help/comments are appreciated!

TIA

2. Easy answer: libgmp

Harder answer: Make your own data structure and create functions to add/substract/divide/etc...

3. > How would one go about performing addition, subtraction, and multiplication on integers that are larger than the built in integer types for C?
Search www.google.com for Arbitrary Precision Arithmetic, lots of good stuff.

-Prelude

4. ## one simple answer

you can store your data in floating points which provides even more data range , while this could be a temporary solution , you can create your own data structure that stores a very long integer by splitting it to 2-3 integers & you need to have your own arithmatic operator functions.

If you want further help on HowTos contact me

5. Prelude seems to be the most helpful with what you are asking. Using floats doesn't seem like it would be the best solution but if it is quick and accurate enough for you then go right ahead. There are a lot of ways of doing this. I think going for the library orbitz suggested is probably the simplest.

6. ## thanks

Thanks for the info guys, I think I'm going to try going the data structure route. Now I just have to figure out how I'm going to implement it

7. you could have a simple struct that looks like this.

Code:
```struct myint {
unsigned int sign;
signed int value;
};```
There are other ways (and better ways) but this is the most basic.

8. ## Re: one simple answer

Originally posted by krunal_dedhia
you can store your data in floating points which provides even more data range
Actually, you will get more accuracy by just using a integer of the same size as the floating point value. Being able to represent a large integer isn't very helpful if you can't represent all of the other integers in between. It would only cause problems and innacurate calculations. A 4-byte integer would be better than a 4-byte floating point and an 8-byte integer would be better than an 8-byte floating point. Not only that, but if you used floating point values, your calculation would take longer and you wouldn't be able to directly use the mod operation.

If you're using msvc++ you can use

__int64

for a 64 bit integer which holds nearly 18 and a half quintillion different integers. That's

18,446,744,073,709,551,616

different values.

In most cases you won't have to go larger than that, but if you do, then ake your own datastructure to handle it.

9. ## VERY large ;)

well I'm definately going to have to use a data structure. I am looking to store and manipulate numbers on the order of 50+ digits. Here's what i was thinking...

make a struct to hold the data (myint), make a function that initializes a new "myint" and have other functions that do the addition, subtraction, and multiplication on the "myint" type.

Sounds easy enough, but I've been really busy lately so I havent had a chance to mess around with it. You guys think that sounds reasonable? I'll post some code if I get a chance to mess around with it.

If anyone would like to provide some more detailed suggestions please feel free to message me privately.

Thanks

Popular pages Recent additions