|
-
May 9th, 2010, 11:32 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Math function on big numbers
I am making a calculator that accepts large number (represented as strings) and performs basic math functions on them. So far I have Add and Multiply figured out.
Here is addition
Code:
Private Function Add(ByVal Number1 As String, ByVal Number2 As String) As String
Dim num1, num2, r, total As New Integer
Dim sum As String = ""
' Make the numbers the same length, padding zeroes were necessary.
If Number1.Length > Number2.Length Then
Number2 = Number2.PadLeft(Number1.Length, Chr(48))
Else
Number1 = Number1.PadLeft(Number2.Length, Chr(48))
End If
' This loop adds each column of numbers together, along with the remainder from the previous column.
For i As Integer = Number1.Length - 1 To 0 Step -1
num1 = Val(Number1(i))
num2 = Val(Number2(i))
total = num1 + num2 + r
r = 0 ' Reset the remainder
Do While total > 9
total -= 10
r += 1
Loop
sum = total & sum ' Adds the new column answer in front of the previous one.
Next
If r <> 0 Then ' If there is a remainder, put it on the front.
Return r & sum
Else
Return sum
End If
End Function
And Multiply
Code:
Private Function Multiply(ByVal Number1 As String, ByVal Number2 As String) As String
Dim num1(Number1.Length - 1), num2(Number2.Length - 1), r, temp As Integer
Dim total(num1.Count - 1) As String
' Fill the arrays with the digits of each number
For a As Integer = Number1.Length - 1 To 0 Step -1
num1(a) = Val(Number1(a))
num2(a) = Val(Number2(a))
Next
' Multiply each lower digit by each upper digit
For b As Integer = num2.Count - 1 To 0 Step -1
For c As Integer = num1.Count - 1 To 0 Step -1
temp = num1(c) * num2(b) + r ' Multiply the two number together along with any remainder.
r = 0 ' Reset the remainder to 0
Do While temp > 9 And Not c = 0 ' If the number is greater than 9 then we need to get the remainders.
temp -= 10
r += 1
Loop
total(b) = temp & total(b) ' Store the answer
Next
Next
' Pad following zeroes
For d As Integer = 2 To total.Count
For f As Integer = 2 To d
total(total.Count - d) = total(total.Count - d) & "0"
Next
Next
' Add the totals together
Dim sum As String = ""
For e As Integer = total.Count - 1 To 0 Step -1
sum = Add(sum, total(e))
Next
' Trim leading zeroes and commas
TrimZero(sum)
' Add the remainder to the front of the sum, if it exists.
' Return the number
If r <> 0 Then
Return r & sum
Else
Return sum
End If
End Function
How would I do subtract and divide?
I saw something like this in the code bank but the examples there were from VB6(?) and were hugely inefficient (compared his Add and Multiply examples to mine, his having way more code and loops.)
Prefix has no suffix, but suffix has a prefix.
-
May 9th, 2010, 11:46 PM
#2
Re: Math function on big numbers
Big numbers are not an issue in .NET 4.0 because it includes the BigInteger type.
-
May 10th, 2010, 02:28 AM
#3
Thread Starter
Hyperactive Member
Re: Math function on big numbers
Wow, I did not know that. On top of not knowing about it, it took me 20 minutes to figure out I have to add it as a reference manually.
Haha, late night. Thanks.
Prefix has no suffix, but suffix has a prefix.
-
May 10th, 2010, 08:22 AM
#4
Re: [RESOLVED] Math function on big numbers
I so want to stop fooling around with SQL and put BigInteger into my code timer to see how it performs.
-
May 10th, 2010, 03:07 PM
#5
Re: [RESOLVED] Math function on big numbers
Along the lines of actually coding a calculator I suggest you take a different approach. Check out the calculator in my sig.
I approached the problem by splitting the input into 3 parts, part 1 is the first number, part 2 is the function, and part 3 is the second number. Then just use a select case for part 2 (each function) and perform the function with part 1 and 3, the numbers.
If you do it that way there will be a lot less code, it will be easier to read, and a lot easier to add new functions do unlike your method now where your addition/subtraction and multiplication functions are completely different bodies of code.
-
May 11th, 2010, 02:34 AM
#6
Thread Starter
Hyperactive Member
Re: [RESOLVED] Math function on big numbers
Well, with the addition of BigInteger I can just use
Code:
BigInteger.Multiply(num1, num2)
It has the functions built in. I messed with it for a while using massive numbers (over 200,000 digits each). Fun stuff.
Prefix has no suffix, but suffix has a prefix.
-
May 11th, 2010, 05:23 AM
#7
Re: [RESOLVED] Math function on big numbers
 Originally Posted by Troy Lundin
Well, with the addition of BigInteger I can just use
Code:
BigInteger.Multiply(num1, num2)
It has the functions built in. I messed with it for a while using massive numbers (over 200,000 digits each). Fun stuff.
You don't have to use methods. The BigInteger type supports standard mathematical operators:
vb.net Code:
Dim bi1 As BigInteger = 5
Dim bi2 As BigInteger = 10
Dim bi3 As BigInteger = bi1 + bi2
Dim bi4 As BigInteger = bi1 * bi2
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
|