Results 1 to 22 of 22

Thread: Working with BIG numbers or LOTS of decimal places

Threaded View

  1. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: Working with BIG numbers or LOTS of decimal places

    Code:
    Public Function subtract(tnum1 As String, tnum2 As String) As String
    Dim i As Long
    Dim length As Long
    Dim temp As Integer
    Dim carry As Byte
    Dim num1 As String
    Dim num2 As String
    num1 = tnum1
    num2 = tnum2
    checkdec:
    'format like always
    If InStr(num1, ".") > 0 And InStr(num2, ".") > 0 Then
        If (Len(num1) - InStr(num1, ".")) < (Len(num2) - InStr(num2, ".")) Then
            Do
                num1 = num1 & "0"
            Loop Until (Len(num1) - InStr(num1, ".")) = (Len(num2) - InStr(num2, "."))
        ElseIf (Len(num1) - InStr(num1, ".")) > (Len(num2) - InStr(num2, ".")) Then
            Do
                num2 = num2 & "0"
            Loop Until (Len(num1) - InStr(num1, ".")) = (Len(num2) - InStr(num2, "."))
        End If
    ElseIf InStr(num1, ".") > 0 And InStr(num2, ".") = 0 Then
        num2 = num2 & "."
        GoTo checkdec
    ElseIf InStr(num1, ".") = 0 And InStr(num2, ".") > 0 Then
        num1 = num1 & "."
        GoTo checkdec
    End If
    
    If Len(num1) > Len(num2) Then
        length = Len(num1)
        num2 = String$(length - Len(num2), "0") & num2
    ElseIf Len(num2) > Len(num1) Then
        length = Len(num2)
        num1 = String$(length - Len(num1), "0") & num1
    Else
        length = Len(num1)
    End If
    
    'subtract the numbers in a similar way to the add code
    carry = 0
    For i = 1 To length
        If Mid(num1, (length - i + 1), 1) = "." Then
            subtract = "." & subtract
        Else
            temp = Val(Mid(num1, (length - i + 1), 1)) - Val(Mid(num2, (length - i + 1), 1)) - carry
            If temp >= 0 Then
                subtract = temp & subtract
                carry = 0
            ElseIf temp < 0 Then
                carry = 1
                temp = temp + 10
                If temp < 0 Then
                    temp = temp + 10
                    carry = 2
                End If
                subtract = temp & subtract
            End If
        End If
    
    Next i
    
    Do While Left(subtract, 1) = "0"
     subtract = Right(subtract, Len(subtract) - 1)
    Loop
    
    If Left(subtract, 1) = "." Then subtract = "0" & subtract
    
    
    
    End Function
    
    
    
    
    
    
    Public Function multiply(tnum1 As String, tnum2 As String) As String
    Dim num1 As String
    Dim num2 As String
    
    
    Dim tempnum3 As String
    Dim mcounter As String
    Dim tempnum2 As String
    Dim templeft As String
    Dim tempright As String
    Dim tempnum1 As String
    
    If morethan(tnum1, tnum2) Then
        num2 = tnum1
        num1 = tnum2
    Else
    num1 = tnum1
    num2 = tnum2
    End If
    
    If InStr(num2, ".") > 0 Then
        If Left(num2, 2) = "0." Then
            'extract 38 and 0.45 from 38.45 for example
            tempnum2 = num2
            num2 = 0
        Else
        tempnum2 = "0." & Right(num2, (Len(num2) - InStr(num2, ".")))
        num2 = Left(num2, (InStr(num2, ".")) - 1)
    End If
    tempnum1 = num1
    
    'once they have been extracted, the decimal places need to be moved
    'turn 6.42 * 0.34 into 34 * 0.0642 for example
    'this will give the same result, but the multiplication code needs at least one of the numbers to be whole for it to work
    'so that is why this needs to be done
    If InStr(tempnum1, ".") = 0 Then tempnum1 = tempnum1 & "."
        Do
            templeft = Left(tempnum1, (InStr(tempnum1, ".") - 1))
            tempright = Right(tempnum1, (Len(tempnum1) - (InStr(tempnum1, "."))))
            If Len(templeft) = 1 Then templeft = 0 & templeft
            tempright = Right(templeft, 1) & tempright
            templeft = Left(templeft, Len(templeft) - 1)
            tempnum1 = templeft & "." & tempright
            templeft = Left(tempnum2, (InStr(tempnum2, ".") - 1))
            tempright = Right(tempnum2, (Len(tempnum2) - (InStr(tempnum2, "."))))
            templeft = templeft & Left(tempright, 1)
            tempright = Right(tempright, Len(tempright) - 1)
            tempnum2 = templeft & "." & tempright
        Loop Until Right(tempnum2, 1) = "."
        tempnum2 = Left(tempnum2, Len(tempnum2) - 1)
        Do While Left(tempnum2, 1) = "0"
            tempnum2 = Right(tempnum2, Len(tempnum2) - 1)
        Loop
    
    'multiply just the 0.?? for now
        Do Until mcounter = tempnum2
            multiply = add(multiply, tempnum1)
            mcounter = add(mcounter, 1)
    DoEvents
        Loop
    End If
    
    'swap the numbers if one is bigger than the other so that the multiplication code doesn't need to loop around so much
    If morethan(num2, num1) And InStr(num1, ".") = 0 Then
        tempnum3 = num1
        num1 = num2
        num2 = tempnum3
    End If
    'multiply the whole numbers
    mcounter = 0
    Do Until mcounter = num2
        multiply = add(multiply, num1)
        mcounter = add(mcounter, 1)
    Loop
    
    'remove any unnecessary 0's at the right if there is a decimal place
    If InStr(multiply, ".") > 0 Then
        Do While Right(multiply, 1) = "0"
             multiply = Left(multiply, Len(multiply) - 1)
        Loop
    End If
    
    'remove any unnecessary 0's at the left
    
    Do While Left(multiply, 1) = "0"
        multiply = Right(multiply, Len(multiply) - 1)
    Loop
    
    If Left(multiply, 1) = "." Then multiply = "0" & multiply
    
    End Function
    
    
    Public Function power(tnum1 As String, tnum2 As String) As String
    Dim pcounter As Long
    Dim num1 As String
    Dim num2 As String
    num1 = tnum1
    num2 = tnum2
    power = 1
    
    Do
        'multiply the number by itself the necessary amount of times
        power = multiply(power, num1)
        pcounter = pcounter + 1
    
    Loop Until pcounter = num2
    
    End Function
    Attached Files Attached Files
    Last edited by killo; Feb 27th, 2008 at 11:31 AM.

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