Results 1 to 4 of 4

Thread: Adding "VERY" long integers

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    here
    Posts
    2

    Adding "VERY" long integers

    Hey,

    We have an asignment to create a calculator which can add and subtract un-signed integers, we have two variables containing validated integers up to 60 digits long.

    How can we make VB6 add these together without createing an exponential answer???

    We've only been working with Vb for a couple of moths... and god we're stuck with this 1!!!

  2. #2
    Fanatic Member Slaine's Avatar
    Join Date
    Jul 2002
    Posts
    641
    I presume you are holding these large numbers as strings. If this is the case, and you only want to do adding then you could try using the long hand method you probably learnt at school.

    ie:

    Starting at the right most digit add the two together, store any overflow in a variable and move left one digit. Add these two together plus any overflow and keep doing this untill you rech the left most digit.

    eg:

    Num1 = "123456"
    Num2 = "789123"

    Step 1: 6+3 = 9
    Step 2: 5+2 = 7
    Step 3: 4+1 = 5
    Step 4: 3+9 = 1 with 1 overflow
    Step 5: 2+8 +1 = 1 with 1 overflow
    Step 6: 1+7 + 1 = 9

    so final result = 911579.

    Subtracting can be done in a similar manner.

    Does this help?
    Martin J Wallace (Slaine)

  3. #3
    Hyperactive Member Hampster's Avatar
    Join Date
    Feb 2001
    Location
    On my hamster wheel.
    Posts
    374
    Slaine's method in code would look something like:
    VB Code:
    1. Function Add(Str1 As String, Str2 As String) As String
    2. 'Simulates working out an addition problem on paper:
    3. ' 1  1
    4. '  91028
    5. '  98191
    6. '+ ______
    7. '  189219
    8.     'Reverse the strings, so that we don't have to do more calculations
    9.     'revsing mid$()
    10.     Str1 = StrReverse(Str1)
    11.     Str2 = StrReverse(Str2)
    12.    
    13.     Dim i           As Double
    14.     Dim TmpHold(1)  As Double
    15.     Dim CarryOver   As Double
    16.     Dim Total       As String
    17.     Dim miniTotal   As Double
    18.     For i = 1 To IIf(Len(Str1) > Len(Str2), Len(Str1), Len(Str2))
    19.         If Len(Str1) >= i Then
    20.             TmpHold(0) = Mid$(Str1, i, 1)
    21.         Else
    22.             TmpHold(0) = 0
    23.         End If
    24.        
    25.         If Len(Str2) >= i Then
    26.             TmpHold(1) = Mid$(Str2, i, 1)
    27.         Else
    28.             TmpHold(1) = 0
    29.         End If
    30.  
    31.        
    32.         TmpHold(0) = TmpHold(0) + CarryOver
    33.         miniTotal = TmpHold(0) + TmpHold(1)
    34.         If miniTotal >= 10 Then
    35.             CarryOver = 1
    36.         Else
    37.             CarryOver = 0
    38.         End If
    39.         If i = IIf(Len(Str1) > Len(Str2), Len(Str1), Len(Str2)) Then
    40.             Total = Total & StrReverse(miniTotal)
    41.         Else
    42.             Total = Total & Right$(miniTotal, 1)
    43.         End If
    44.     Next
    45.     Add = StrReverse(Total) 'Function gives the answer in reverse form, revert.
    46. End Function
    Last edited by Hampster; Nov 11th, 2002 at 11:07 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    here
    Posts
    2
    thx ppl

    thats pretty much wat we ended up doin in necase

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width