Results 1 to 21 of 21

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

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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

    How can I convert a number like 1.21932631356501E+215 to its complete form?
    Last edited by dee-u; Apr 9th, 2009 at 01:41 PM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

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

    Like this?
    Code:
        Dim fl As Double        
        fl = 1.21932631356501E+21    
        MsgBox Format(fl, "0") ' = 1219326313565010000000

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

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

    dee-u

    What do you mean by "complete form"?

    Do you want to see 202 zeros?
    Do you also want to see commas?
    Is your screen wide enough??

    EDIT: Actually, looks like jcis is on to something (albeit sans commas)

    Spoo

  4. #4

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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

    I actually would want to multiply 1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891 23456789 to 9876543219876543219876543219876543219876543219876543219876543219876543219876543219876543219876543219 87654321 aand it should also return a compete number instead of a floating point. The number I quoted above was the result in Double.

    I am searching some ways on how I could determine the exact product of those two values so I could compare with the result from jcis' recommendation.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

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

    dee-u

    Did you mean you wanted to multiply a 120 digit number by another 120
    digit number and produce the exact result?

    EDIT: If so, how do you propose to even show a number with 120 significant figures?
    One way might be to utilize an array. Double-precision sure won't do the job.

    Spoo

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

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

    Quote Originally Posted by dee-u View Post
    How can I convert a number like 1.21932631356501E+215 to its complete form?
    That's not really a floating point number; that's a very large integer. My solutions are very different for both problems.

    For storing the exact value of a decimal, I would store the numerator and denominator. So instead of a floating point .333..., I'd store the integers 1 and 3. This of course won't work for irrational numbers, but those can't be stored with exact precision pretty much by definition. (Right?)

    To handle very large integers, however, I'd set up my own math processor. This has come up in the past, and Logophobic has pointed out that using strings for this is hugely inefficient. But it works well for pointing you in the right direction.

    Store those two numbers as strings. Then set up loops to go through and do your addition / subtraction / whatever exactly the same way you were taught in elementary school. For addition, add the two right-most digits together, carry the 1, move left one and add the next set of digits, carry the 1, etc...

    EDIT: Come to think of it, Logo may have actually posted a functional solution. I'm too lazy to search for it now, though.

  7. #7

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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

    How could I utilize an array in this case? I would want it to make a function that will accept such large number (a string or other appropriate variable type) and return a string that has the result.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

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

    Strings are just arrays of characters. They are conceptually the same thing.

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

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

    Im not sure if I understand, you mean just this?
    Code:
    Private Function GetMiltiply(pNumber1 As Double, pNumber2 As Double) As String
        GetMiltiply = Format(pNumber1 * pNumber2, 0)
    End Function
    Code:
    MsgBox GetMiltiply(1.23456789123457E+107, 9.87654321987654E+107)

  10. #10
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

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

    Quote Originally Posted by dee-u View Post
    How could I utilize an array in this case?
    LOL.. that was just an idea, not a complete solution. But, it could be
    like a byte array, each element represents, 1,10,100,1000, etc. I went
    this route as opposed to a string (I see that Ellis Dee also went there)
    because you do want to actually do some math, right?

    Addition or subtraction would be a snap, either way. But multiplication,
    which was your original goal, seemed to me to beg for numeric values,
    not characters, which would need to be converted.

    Spoo

  11. #11
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

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

    Quote Originally Posted by jcis View Post
    Im not sure if I understand, you mean just this?
    Code:
    Private Function GetMiltiply(pNumber1 As Double, pNumber2 As Double) As String
        GetMiltiply = Format(pNumber1 * pNumber2, 0)
    End Function
    Code:
    MsgBox GetMiltiply(1.23456789123457E+107, 9.87654321987654E+107)
    No, he wants the actual values. Doubles are only approximations. For example:
    Code:
    Public Sub Test()
        Dim dblNumber As Double
        
        dblNumber = 1.23456789123457E+107
        If dblNumber + 1 = dblNumber Then MsgBox "Epic Fail"
    End Sub
    It's never a good thing when your math ends up showing that a + 1 = a.

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

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

    Quote Originally Posted by Ellis Dee View Post
    No, he wants the actual values. Doubles are only approximations. For example:
    Code:
    Public Sub Test()
        Dim dblNumber As Double
        
        dblNumber = 1.23456789123457E+107
        If dblNumber + 1 = dblNumber Then MsgBox "Epic Fail"
    End Sub
    It's never a good thing when your math ends up showing that a + 1 = a.
    Awwww, good example. I didn't know that.

  13. #13

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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

    Quote Originally Posted by Ellis Dee View Post
    EDIT: Come to think of it, Logo may have actually posted a functional solution. I'm too lazy to search for it now, though.
    I tried searching but as of now I cannot find it, any keyword clues?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  14. #14
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

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

    How about binary arrays. Do the digit by digit math with long variables for the answer and the carries. Use a for next loop that multiplies and adds the carry. You could use redim preserve to shift up for the higher place values but multiply with the same loop.

    Where the dickens are you gonna display the answer on a scrolling sign in time square?
    Last edited by technorobbo; Apr 9th, 2009 at 05:39 AM. Reason: Post edited because I crossed radices
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  15. #15
    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

  16. #16
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

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

    Quote Originally Posted by dee-u View Post
    I tried searching but as of now I cannot find it, any keyword clues?
    Found it by searching for "universe".

    http://www.vbforums.com/showthread.php?t=483207

    Though it is a thread about converting very large hex numbers to decimal, I bring up the idea of using strings to do arithmetic on them. Logo then posts code snippets of how to do it with byte arrays as a (much) faster alternative.

    There are also links that go to functional very large decimal calculators.

  17. #17
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

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

    heh, I like the (lack of) precision demonstration function I posted to that thread much better:
    Code:
    Public Sub Imprecision()
        Dim dblNumber  As Double
        Dim dblCompare As Double
        
        dblNumber = HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
        dblCompare = dblNumber - 10000000000000#
        If dblNumber = dblCompare Then
            MsgBox "x minus 10,000,000,000,000 is equal to x"
        End If
    End Sub
    Subtract 10 trillion from a number and the number still doesn't change. Ouch.

  18. #18
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

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

    dee-u

    I see that this is now [resolved] -- can you give us a clue as to your
    resolution?

    Spoo

  19. #19
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

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


  20. #20
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

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

    Quote Originally Posted by anhn View Post
    There are many ways to multiply BIG integers (fast or slow).
    Unless both parameters passed to your BigMultiply are of equal length I get a subscript out of range error.

    Debug.Print BigMultiply("100", "10") ' error

    Just thought I mention it, I suck at math and have no clue with stuff like this!

  21. #21
    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

    Quote Originally Posted by Edgemeal View Post
    Unless both parameters passed to your BigMultiply are of equal length I get a subscript out of range error.

    Debug.Print BigMultiply("100", "10") ' error

    Just thought I mention it, I suck at math and have no clue with stuff like this!
    There was a bug there with i and j mixed up

    Replace
    n = bn2(j - 1) * bn1(i - 1) + r
    with
    n = bn2(i - 1) * bn1(j - 1) + r
    • 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