Results 1 to 7 of 7

Thread: [RESOLVED] Math function on big numbers

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Math function on big numbers

    Big numbers are not an issue in .NET 4.0 because it includes the BigInteger type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Math function on big numbers

    Quote Originally Posted by Troy Lundin View Post
    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:
    1. Dim bi1 As BigInteger = 5
    2. Dim bi2 As BigInteger = 10
    3. Dim bi3 As BigInteger = bi1 + bi2
    4. Dim bi4 As BigInteger = bi1 * bi2
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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