Thread: 40 digit integer arithmetic (arrays)

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    40 digit integer arithmetic (arrays)

    what is the best way to deal with negative integers?

    what I did is to string the integer. store each digit in an int array[40]. in case of a negative integer, the only negative digit will be the one with the highest place value.

    when operating, say addition, i have to add the integer digit by digit. like doing it in paper. i got it when the integer is unsigned but im still having trouble when its signed. can you give me an idea how to tackle the negative integer arithmetic.

    its just addition, subtraction and multiplication.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you say you're only storing the sign on the highest place value, you mean the first number always, or the first nonzero digit (reading from the left)? If the former, you could be losing your signs, as -0 == 0. If the latter, that's nice for printing, but you'll have to find that sign before you try to add (since adding a negative is much like subtraction, etc.)

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    the first non zero digit is signed.

    I store the ones place on index 0.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    i need this by monday

    if you can just give me some suggestions i'd really appreciate it

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Do you know how two's complement representation works? If you're storing your numbers internally as binary that might be one solution (but I suspect you're just storing decimal digits here).

    To code subtraction the long-hand way you'll need to implement borrowing. You may be interested in this: Subtraction algorithm
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I think the subtraction algorithm wont work. The integer size is too large.

    I'll try two's compliment

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by freddyvorhees View Post
    I think the subtraction algorithm wont work. The integer size is too large.

    I'll try two's compliment
    That makes no sense. The subtraction algorithm is not size-restricted.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So, you read lets say -23213812381..312312 and 32131231..123312 and you want to do opereations on them? If that is the case I guess you read char by char. So why not put on array[0] the sign? For *, -, +, / just do it as you do o paper. Don't really see the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. from perl to C
    By prettydainty in forum C Programming
    Replies: 5
    Last Post: 01-05-2009, 11:47 AM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. 200 digit integer... how?
    By skeptik in forum C Programming
    Replies: 20
    Last Post: 07-25-2005, 07:56 AM
  5. size of integer arrays
    By steve8820 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 07:31 PM