Results 1 to 5 of 5

Thread: LONG NUMBERS --> No Exponents

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    LONG NUMBERS --> No Exponents

    Hi...I'm trying to make a program to find patterns in numbers. The only problem is that the numbers i'm using can have more than 15 digits long, which is bigger than the maximum number for a DOUBLE type variable. I tried using strings but it still failed and gave me something like 1.11111111111111E16 and i dont want that because it rounds the numbers up and defeats the purpose behind patterns.

    The thing is, i'm adding numbers together using something like

    A = Val(Num1) + Val(Num2)

    and that always gives me exponents. so how do i fix this?

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    val returns numeric string of appropriate type. As you say you understand, double only goes to 15 digits, so if you feed val something that has to be returned as a double, then it will give you a value that has less than 15 digits and the way it does that is to use exponential notation.

    It sounds as though you want to use the VB data types and yet not be bound by their limitations. Can't happen.

  3. #3
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    U should be able to do it......the old fashioned way....Give me a sec..

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  4. #4
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Ok a very barbones version. U could add error handling code
    VB Code:
    1. Private Function Add_The_Way_We_Learnt_As_Kids(uValOne As String, uValTwo As String) As String
    2. Dim bytCarry As Byte, intResult As String, lngPos As Long
    3. Dim StrSum As String
    4. If Len(uValOne) > Len(uValTwo) Then
    5.     uValTwo = String(Len(uValOne) - Len(uValTwo), "0") & uValTwo
    6. ElseIf Len(uValOne) < Len(uValTwo) Then
    7.     uValOne = String(Len(uValTwo) - Len(uValOne), "0") & uValOne
    8. End If
    9.  
    10.  
    11. For lngPos = Len(uValOne) To 1 Step -1
    12.     intResult = (CInt(Mid(uValOne, lngPos, 1)) + CInt(Mid(uValTwo, lngPos, 1)))
    13.     If bytCarry = 1 Then
    14.         intResult = intResult + 1
    15.         bytCarry = 0
    16.     End If
    17.    
    18.     If Len(CStr(intResult)) = 2 Then
    19.          bytCarry = 1
    20.          intResult = CInt(Right$(CStr(intResult), 1))
    21.     End If
    22.        
    23.     StrSum = CStr(intResult) & StrSum
    24.     'Debug.Print StrSum
    25. Next lngPos
    26.     Add_The_Way_We_Learnt_As_Kids = StrSum
    27.            
    28.  
    29. End Function
    30. Private Sub Command1_Click()
    31. Dim testVal1 As String, testVal2 As String
    32. testVal1 = "123458792314147451225187487840010005484445610005"
    33. testVal2 = "4568144484844848489421213232955629595944"
    34. Debug.Print Add_The_Way_We_Learnt_As_Kids(testVal1, testVal2)
    35. Debug.Print CCur(testVal1) + CCur(testVal2)
    36. End Sub
    Same as we learnt in junior school. Beging from the right and carry over if greater than 9.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    the code's good..but it's buggy and I can't find the bug for some reason. You're all familiar with the fibinacci sequence, right? So i tested your code with that simple program...but for some reason, it can't do it and I couldn't find the problem. :S I fixed it for the first 2 digits but that's it. So I put it back to your code, and only 1 digit is added :S!

    VB Code:
    1. Option Explicit
    2.  
    3. Sub Main()
    4.     Fibinacci 216, "D:\OutPut.txt"
    5. End Sub
    6.  
    7. Private Function Adding(uValOne As String, uValTwo As String) As String
    8.     Dim bytCarry As Byte
    9.     Dim intResult As String
    10.     Dim lngPos As Long
    11.     Dim StrSum As String
    12.    
    13.     If Len(uValOne) > Len(uValTwo) Then
    14.         uValTwo = String(Len(uValOne) - Len(uValTwo), "0") & uValTwo
    15.     ElseIf Len(uValOne) < Len(uValTwo) Then
    16.         uValOne = String(Len(uValTwo) - Len(uValOne), "0") & uValOne
    17.     End If
    18.  
    19.     For lngPos = Len(uValOne) To 1 Step -1
    20.         intResult = (CInt(Mid(uValOne, lngPos, 1)) + CInt(Mid(uValTwo, lngPos, 1)))
    21.         If bytCarry = 1 Then
    22.             intResult = intResult + 1
    23.             bytCarry = 0
    24.         End If
    25.        
    26.         If Len(CStr(intResult)) = 2 Then
    27.              bytCarry = 1
    28.              intResult = CInt(Right$(CStr(intResult), 1))
    29.         End If
    30.            
    31.         StrSum = CStr(intResult) & StrSum
    32.        
    33.     Next lngPos
    34.     Adding = StrSum
    35.  
    36. End Function
    37.  
    38. Function Fibinacci(Times As Integer, FilePath As String) As Boolean
    39.     On Error Resume Next
    40.    
    41.     Dim I As Long
    42.    
    43.     Dim A As String
    44.     Dim B As String
    45.     Dim C As String
    46.    
    47.     Kill FilePath
    48.    
    49.     A = "1"
    50.     B = "1"
    51.     C = "0"
    52.    
    53.     Open FilePath For Binary As #1
    54.        
    55.         Put #1, , "1." & vbTab & A & vbCrLf
    56.         Put #1, , "2." & vbTab & B & vbCrLf
    57.        
    58.         For I = 2 To Times
    59.             A = Adding(A, B)
    60.  
    61.             DoEvents
    62.            
    63.             Put #1, , I & "." & vbTab & A & vbCrLf
    64.            
    65.             C = A
    66.             A = B
    67.             B = C
    68.         Next I
    69.        
    70.     Close #1
    71. End Function

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