|
-
Mar 3rd, 2004, 08:46 PM
#1
Thread Starter
Addicted Member
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
-
Mar 3rd, 2004, 11:12 PM
#2
how's this???
VB Code:
Public Function StringMultiply(ByVal str1 As String, ByVal str2 As String) As String
Dim strResult As String = "0"
Dim intCarry As Integer
For I As Integer = (str1.Length - 1) To 0 Step -1
For X As Integer = (str2.Length - 1) To 0 Step -1
Dim intPlaces As Integer = (str1.Length - 1 - I) + (str2.Length - 1 - X)
'These places need to be appended before adding
Dim strNewVal As String = CStr(Microsoft.VisualBasic.Val(str1.Chars(I)) * _
Microsoft.VisualBasic.Val(str2.Chars(X)))
strNewVal = strNewVal.PadRight(strNewVal.Length + intPlaces)
strResult = StringAdd(strResult, strNewVal)
Next
Next
Return strResult
End Function
VB Code:
Public Function StringAdd(ByVal str1 As String, ByVal str2 As String) As String
'This can add until the Length Sum of str1 and str2 is greater then 65,000 or so
Dim strResult As String
If str1.Length > str2.Length Then
str2 = str2.PadLeft(str1.Length, "0")
Else
str1 = str1.PadLeft(str2.Length, "0")
End If
'now both strings are the same length
Dim sCarry As Short
For I As Integer = (str1.Length - 1) To 0 Step -1 ' we add Right to left
Dim intResult As Integer = Microsoft.VisualBasic.Val(str1.Chars(I)) + Microsoft.VisualBasic.Val(str2.Chars(I)) + sCarry
If intResult > 9 Then
sCarry = (intResult - (intResult Mod 10)) / 10
intResult -= sCarry * 10
Else
sCarry = 0
End If
strResult &= intResult
Next
'Dont forget the sCarry
strResult &= sCarry
strResult = StrReverse(strResult).TrimStart("0")
If strResult.Length = 0 Then
Return "0"
Else
Return strResult
End If
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
-
Mar 4th, 2004, 02:49 PM
#3
Thread Starter
Addicted Member
Wow, thanks! It's fast too!
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Mar 9th, 2004, 08:39 PM
#4
Thread Starter
Addicted Member
Do you have one for division as well?
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Nov 19th, 2007, 09:42 AM
#5
Lively Member
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
-
Nov 19th, 2007, 11:47 AM
#6
Thread Starter
Addicted Member
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
-
Nov 19th, 2007, 11:56 PM
#7
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|