11-20-2009 02:50 AM
Read this piece of code....
float f = 32760.0;
for (;;) {
f += 0.001;
if (f > 32770.0) break;
}
I expect that after some iterations the loop terminates.
The problem is that after the variable f reaches 32768.0, it won't increment anymore.
if the variable f is incremented by 0.002, the loop exits correctly.....
This was reported using in CVI81 and 711...
any ideas ?????
11-20-2009 03:27 AM
Use a double, not a float. You are hitting the granularity of the floating point representation. There are a limited number of bits in which to store your information, and your example exceeds this limit.
JR
11-20-2009 03:41 AM
Nice point,
I understand there is a granularity limit for each type, but it is surprising to see that it changes with actual value.
Maybe it is obvious once you work on the binary floating point representation.
11-20-2009 11:28 AM
11-20-2009 03:01 PM
Floating point precision is in terms of total decimal digits. A surprise to many is that while you may be able to exactly represent, say 0.001, you may not be able to exactly represent 1000000000.001
You might wind up with 1000000000.00099999 for example instead.
Also realize that a floating point number is a binary approximation of a decimal number. Floating point representation allows the exact representation of a finite number of decimal values, and these numbers are not evenly distributed due to the way the binary approximation works.
A double allows the exact representation of a far larger number of decimal values than float, and over a larger range.
As an exercise it's very informative to look at the binary representation for different decinmal numbers and see for yourself how it works. You have to be careful, though, since you can obscure the issue when print the decimal values out as the formatting may round the values to a smaller precision than is in the value itself.
Menchar
11-20-2009 03:24 PM
11-20-2009 04:49 PM - edited 11-20-2009 04:52 PM
That's a very cool site.
If you go up to the parent page for that link you see a link to a page where you can enter a decimal number and see the resulting IEEE 754 representations.
I remember 35 years ago figuring this all out for myself on a 16 bit HP minicomputer.
Menchar