Reputation: 75
All, I'm intermediate for VB6. I want to get remainder from divided two doubles. I used ,
Dim a As Double, b As Double, result As Double
b = 8333.33
a = 58333.31
result = a - (b * Fix(a / b))
result should be 0. But it is not. a/b =7 and no remain. So Fix(a / b) should be 7. But Fix(a / b)=6, Why?
Upvotes: 3
Views: 574
Reputation: 1486
BTW, another trick to get it working - depending on the floating point numbers you need to work with - is to use Currency
instead of Double
Dim a As Currency, b As Currency
b = 8333.33
a = 58333.31
Debug.Print Fix(a / b) ' Returns 7
Debug.Print a - (b * Fix(a / b)) ' Returns 0
Upvotes: 3
Reputation: 13048
If you use this as a workaround I think you will get the result you expect:
Dim a As Double, b As Double, intermediate As Double, fixed1 As Double, fixed2 As Double
b = 8333.33
a = 58333.31
intermediate = a / b
fixed1 = Fix(intermediate)
fixed2 = Fix(a / b)
Debug.Print intermediate ' 7
Debug.Print fixed1 ' 7
Debug.Print fixed2 ' 6
Int, Fix functions
Returns the integer portion of a number.
... Remarks Both Int and Fix remove the fractional part of number and return the resulting integer value.
(that is from VBA docs but should be the same for VB6).
Apparently when VB evaluates a / b
within the context of the function call it results in a floating point value very slightly < 7.
Upvotes: 2