We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
My test routine is as follows.
#pragma OPTIMIZE(0) #pragma FLOATFUZZY(3) #include <intrins.h> void main() { float f1; float f2; unsigned long l1; unsigned long l2; f1 = 0.9; f2 = f1 - 0.1; // f2 = 0.8 f1 = 0.8; // f1 = 0.8 // Show the binary representations of the floats. l1 = *(unsigned long *)&f1; // 0x3F4CCCCD l2 = *(unsigned long *)&f2; // 0x3F4CCCCC if(f1 == f2) { // not equal _nop_(); } while(1); }
"One possible bug might be in exactly what Keil means when they say the low "three" bits are "rounded". Depending on your interpretation of that phrase, that might mean a number of things. Does the library code add 4 and truncate? Add 3 and truncate? Add 7 and truncate?" Indeed, I've always wondered what they do. Whatever it is, though, it is still a really bad idea to test floats for (in)equality. You're likely to wind up with code that works until some unfortunate combination of values causes it to fail. You're probably lucky that you've started off with a failure condition. Stefan