Very large signed integer math question

This is a discussion on Very large signed integer math question within the C Programming forums, part of the General Programming Boards category; Here's a tough one... How would one go about performing addition, subtraction, and multiplication on integers that are larger than ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Question 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. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Easy answer: libgmp

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

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > 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
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    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. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    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. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    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. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    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 subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  3. Math Question, hard
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 12-08-2001, 11:58 AM
  4. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 05:20 PM
  5. Newbies question about the integer type
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21