In my application under GCC 3.2 on a Pentium machine, I do something like
to get an index from doubles mu and dmu. When mu = 1.0, dmu = 0.005, you get i = 199 on the pentium box (you expect 200). Presumably this is because the internal representation of mu/dmu on the Pentium is 199.999999999999.
On PPC under GCC 4, you get 200, and everything works fine.
This has caused me no end of grief; an entire day sorting out this weird error which turned out to be quite deep in the code. At least it wasn't an endianness issue or something more sinister. Since I now have to go through an enormous amount of code and take out all occurences of this trick to obtain the integer part of a double, what is the best way of doing what I'm trying to do that avoids this 0.999999999999 issue?
Code:
unsigned int i;
i = static_cast< unsigned int >(mu / dmu);
On PPC under GCC 4, you get 200, and everything works fine.
This has caused me no end of grief; an entire day sorting out this weird error which turned out to be quite deep in the code. At least it wasn't an endianness issue or something more sinister. Since I now have to go through an enormous amount of code and take out all occurences of this trick to obtain the integer part of a double, what is the best way of doing what I'm trying to do that avoids this 0.999999999999 issue?