This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I still have trouble with FLOATFUZZY

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);
}
I suppose that f1 and f2 would be equal after the three least significant bits of the mantissa are rounded. But the simulation turns out that they are not euqal.
I'm looking for your help.

d.curie

Parents
  • "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

Reply
  • "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

Children
No data