PDA

View Full Version : Solved: Long vs Double data type



Digita
05-28-2009, 05:40 PM
Howdy,

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

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

and

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



kp

Aussiebear
05-28-2009, 06:58 PM
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.

Digita
05-28-2009, 07:26 PM
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

Aussiebear
05-28-2009, 07:35 PM
I believe so

Digita
05-28-2009, 07:47 PM
Awesome. Thanks Aussiebear.

Have a great weekend.

mikerickson
05-28-2009, 08:20 PM
Long variables are as accurate as Double, when the result is a Long.

Digita
05-28-2009, 11:38 PM
Hi Mikerickson,

Which function can be used to make the result long?

Thanks in advance.


kp

mdmackillop
05-29-2009, 12:42 AM
Basically, Long holds only integer values, Double and Single will take decimals


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

Digita
05-30-2009, 03:56 PM
Thanks Mal.