- Local time
- Today, 03:33
- Joined
- Feb 28, 2001
- Messages
- 30,880
The_Doc_Man: Stating the obvious, if not properly handled
76.07 - 67 = 9.06999999999999
This implies that 76.07 was stored in a SINGLE or DOUBLE since this won't happen in CURRENCY data types and won't happen in scaled LONG data types. The truth of the matter is that the "67" in that expression is window dressing, since that "76.07" was already stored with the seeds of its own inaccuracy already planted. The problem is that some fractions are inherently irrational (in the formal mathematical sense) when expressed in binary. Therefore, if you are worried about your representation, pick a method that doesn't run into the problem in the first place.
In decimal vs. binary for fractional numbers, we have to remember that 0.1 is irrational because it is formed by 1/(2*5) and thus 1/2*1/5. The 1/2 is a piece of cake in binary... but the 5 is not because 2 is not a factor of 5. EVERY ODD NUMBER in decimal has the potential to be irrational. Except for numbers like 0.5, 0.25, 0.125, etc., you cannot express decimal fractions evenly in binary.
The only problem I can see with a Currency field is for the 3rd or 4th digit to alter the first two digits.
If you do something in the code that would introduce a fraction, yes you would eventually accumulate something. Which is why you have to watch what you are doing with the number. When you are doing ONLY addition or subtraction operations, your concern of altering digits doesn't apply because there is no mathematical way to add digits that weren't already there. Multiplication by a fractional amount or any type of division WOULD introduce 3rd or 4th digits - but the simplest solution then becomes that you can head this off at the pass by using a function to truncate the result, such as VBA's TRUNC( number, places) after you have done something that could introduce a 3rd or 4th digit.
TRUNC function - Microsoft Support
Truncates a number to an integer by removing the fractional part of the number. TRUNC removes the fractional part of the number. INT and TRUNC are different only when using negative numbers: TRUNC(-4.3) returns -4, but INT(-4.3) returns -5 because -5 is the lower number.