Results 1 to 21 of 21

Thread: [RESOLVED] Convert a floating-point no. to its true/complete numeric value

Threaded View

  1. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Convert a floating-point no. to its true/complete numeric value

    There are many ways to multiply BIG integers (fast or slow).
    We had discussed about this a year ago on these forums (one of the threads are between me and dbasnett).
    Search "BigInteger" and/or "LargeInteger" to find more.

    This is a way to multiply 2 "digit string", it is not very fast because it use a "primary school method" to multiply.
    It will be quicker if chop "number strings" into smaller parts of 4 digits (use with Long) or 7 digits (use with Currency) or 14 digits (use with Decimal Variant).
    In this function, it uses 1-digit parts with Byte data type.
    Code:
    Sub BigTest()
        Dim sNum1 As String
        Dim sNum2 As String
        Dim sNum3 As String
        
        sNum1 = "123456789123456789123456789123456789123456789123456789123456" & _
                "789123456789123456789123456789123456789123456789"
        sNum2 = "987654321987654321987654321987654321987654321987654321987654" & _
                "321987654321987654321987654321987654321987654321"
    
        sNum3 = BigMultiply(sNum1, sNum2)
        
        Debug.Print sNum3
    
    '-- sNum3 = "121932631356500531591068431825636332060204232294772132529340" & _
                "032763907932998475833233043733467611633702179533692882171458" & _
                "314271223746370989178470754610570520042670285474770050906869" & _
                "816338969581771069347203169112635269"
    End Sub
    Code:
    Function BigMultiply(ByVal sNum1 As String, sNum2 As String) As String
        Dim d1 As Byte, d2 As Byte, r As Byte, d As Byte
        Dim n1 As Long, n2 As Long, n3 As Long
        Dim i As Long, j As Long, k As Long, n As Long
        Dim bn1() As Byte, bn2() As Byte
        Dim Matrix() As Byte
        Dim sResult As String
        
        bn1 = StrConv(sNum1, vbFromUnicode)
        bn2 = StrConv(sNum2, vbFromUnicode)
        For i = 0 To UBound(bn1): bn1(i) = bn1(i) - 48: Next
        For i = 0 To UBound(bn2): bn2(i) = bn2(i) - 48: Next
        n1 = UBound(bn1) + 1
        n2 = UBound(bn2) + 1
        n3 = n1 + n2
        
        ReDim Matrix(1 To n2, 1 To n3)
        
        sResult = String$(n3, "0")
        
        For i = n2 To 1 Step -1
            r = 0
            For j = n1 To 1 Step -1
                n = bn2(i - 1) * bn1(j - 1) + r '<<-- updated
                d = n Mod 10
                r = n \ 10
                k = i + j
                Matrix(i, k) = d
            Next
            If r > 0 Then Matrix(i, k - 1) = r
        Next
        
        n = 0
        For k = n3 To 1 Step -1
            For i = n2 To 1 Step -1
                n = n + Matrix(i, k)
            Next
            Mid$(sResult, k, 1) = n Mod 10
            n = n \ 10
        Next
        
        If n > 0 Then             '-- there was a mistake in these 2 lines:  
            sResult = n & sResult '-- n must be used instead of r
        Else
            Do While Left$(sResult, 1) = "0"
                sResult = Mid$(sResult, 2)
            Loop
        End If
        
        BigMultiply = sResult
    
    End Function
    Last edited by anhn; Apr 9th, 2009 at 11:54 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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