Consulting

Results 1 to 9 of 9

Thread: Solved: Long vs Double data type

  1. #1
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location

    Solved: Long vs Double data type

    Howdy,

    Can somebody please explain why the use of double and long data type produces different results in this situation:

    [vba]Dim a As Double
    a = 63 * 0.15
    Debug.Print IIf(a = Int(a), a, Int(a) + 1)[/vba]

    and

    [vba]Dim a As Long
    a = 63 * 0.15
    Debug.Print IIf(a = Int(a), a, Int(a) + 1)[/vba]



    kp

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Hi Digita, Its my understanding the Double variables are stored as a Double precision floating point (64 bit 8 byte) value where as Long variables are stored as (32 bit 4 byte) values.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Thanks Aussiebear for quick reply.

    Just one last question - is it true that Long variables are good for speed and Doubles for greater accuracy?

    Cheers


    kp

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    I believe so
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Awesome. Thanks Aussiebear.

    Have a great weekend.

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Long variables are as accurate as Double, when the result is a Long.

  7. #7
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Hi Mikerickson,

    Which function can be used to make the result long?

    Thanks in advance.


    kp

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Basically, Long holds only integer values, Double and Single will take decimals

    [vba]
    Option Explicit
    Sub Test()
    Const C = 123.456789123458
    Dim L As Long
    Dim D As Double
    Dim S As Single
    L = C
    D = C
    S = C
    MsgBox "Long = " & L & vbCr & "Single = " & S & vbCr & "Double = " & D
    End Sub


    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Thanks Mal.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •