Is there a decent Big Numbers library for VB.NET?
I need to be able to multiply 3587551859 * 3587551859
Printable View
Is there a decent Big Numbers library for VB.NET?
I need to be able to multiply 3587551859 * 3587551859
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 FunctionVB 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
Wow, thanks! It's fast too!
Do you have one for division as well?
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
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.
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.