Why it happen?A error happen in VC6.0
-
When I use VC6.0,I find that: float value=33.4; DWORD temp=33; value=value-(float)temp; or value=value-temp; Then value will be 0.4 in theory. But in face it is 0.400002 in vc 6.0. Why is it? is it a bug of VC6.0 or CPU?
Because,in fact,the float or double is not always preciously number when it store in the memory.:laugh::laugh:
-
When I use VC6.0,I find that: float value=33.4; DWORD temp=33; value=value-(float)temp; or value=value-temp; Then value will be 0.4 in theory. But in face it is 0.400002 in vc 6.0. Why is it? is it a bug of VC6.0 or CPU?
That's life with floating point arithmetic. Just like 1/3 can't be represented in decimal without an infinite number of decimal places not all numbers can be represented exactly in base 2 with a fixed number of binary places. If you use
double
s to get more precision the answer comes to 0.39999999999999858 which is closer to the ideal. On a sererate issue, lose thefloat
cast: it's not needed and is bad form.Steve