Results 1 to 7 of 7

Thread: Big Numbers

  1. #1

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Big Numbers

    Is there a decent Big Numbers library for VB.NET?

    I need to be able to multiply 3587551859 * 3587551859
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    how's this???

    VB Code:
    1. Public Function StringMultiply(ByVal str1 As String, ByVal str2 As String) As String
    2.  
    3.         Dim strResult As String = "0"
    4.  
    5.         Dim intCarry As Integer
    6.  
    7.         For I As Integer = (str1.Length - 1) To 0 Step -1
    8.  
    9.             For X As Integer = (str2.Length - 1) To 0 Step -1
    10.  
    11.                 Dim intPlaces As Integer = (str1.Length - 1 - I) + (str2.Length - 1 - X)
    12.                 'These places need to be appended before adding
    13.                 Dim strNewVal As String = CStr(Microsoft.VisualBasic.Val(str1.Chars(I)) * _
    14.                     Microsoft.VisualBasic.Val(str2.Chars(X)))
    15.  
    16.                 strNewVal = strNewVal.PadRight(strNewVal.Length + intPlaces)
    17.  
    18.                 strResult = StringAdd(strResult, strNewVal)
    19.             Next
    20.  
    21.         Next
    22.  
    23.         Return strResult
    24.  
    25.     End Function
    VB Code:
    1. Public Function StringAdd(ByVal str1 As String, ByVal str2 As String) As String
    2.         'This can add until the Length Sum of str1 and str2 is greater then 65,000 or so
    3.         Dim strResult As String
    4.  
    5.         If str1.Length > str2.Length Then
    6.             str2 = str2.PadLeft(str1.Length, "0")
    7.         Else
    8.             str1 = str1.PadLeft(str2.Length, "0")
    9.         End If
    10.  
    11.  
    12.         'now both strings are the same length
    13.  
    14.         Dim sCarry As Short
    15.  
    16.         For I As Integer = (str1.Length - 1) To 0 Step -1 ' we add Right to left
    17.             Dim intResult As Integer = Microsoft.VisualBasic.Val(str1.Chars(I)) + Microsoft.VisualBasic.Val(str2.Chars(I)) + sCarry
    18.  
    19.             If intResult > 9 Then
    20.                 sCarry = (intResult - (intResult Mod 10)) / 10
    21.                 intResult -= sCarry * 10
    22.             Else
    23.                 sCarry = 0
    24.             End If
    25.  
    26.             strResult &= intResult
    27.  
    28.         Next
    29.  
    30.         'Dont forget the sCarry
    31.  
    32.         strResult &= sCarry
    33.  
    34.         strResult = StrReverse(strResult).TrimStart("0")
    35.         If strResult.Length = 0 Then
    36.             Return "0"
    37.         Else
    38.             Return strResult
    39.         End If
    40.     End Function
    Last edited by <ABX; Mar 4th, 2004 at 04:05 AM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Wow, thanks! It's fast too!
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  4. #4

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Smile

    Do you have one for division as well?
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  5. #5
    Lively Member
    Join Date
    Feb 2005
    Posts
    85

    Talking Re: Big Numbers

    I know this is an old thread but I figured this was better than starting a new one on the same subject.

    I am glad to have found these functions here. does anyone have any for subtraction and division?.

    Thanks

  6. #6

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Big Numbers

    Sadly I can't remeber what project I needed this for and so I can't remeber if I figured the rest of it out.

    Maybe someone out there has the answer.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  7. #7
    Lively Member
    Join Date
    Feb 2005
    Posts
    85

    Re: Big Numbers

    thanks for the response Dmyze. I am trying to convert a VB6 App I have over to .Net. The VB to .Net conversion allowed the app to work but left it mainly using the old coding style.
    I mainly need the ability to do basic math functions (add, subtract, divide, and multiply and squareroot)on very large numbers(up to 1000 digits). These cover 2 of them.
    In case it helps any I am mainly after the execution speed as I have to run thousands of Calculations on these numbers.
    Again thank you for any help in this matter.

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