Right, I have an array for floats which stores only 1s and 0s. I’m trying to just do a simply test/check that the current slot in the array is 1 it will print out a little message to say it is 1, otherwise, it is 0. Heres my code:
if(myArray[i] == 1)
{
cout << "this is 1 !!!!!" << endl;
}
else
{
cout << "this is 0 ";
}
but this just keeps entering the “else” section. i.e. only printing “this is 0”. Whats wrong with it (or whats wrong with me?? :P)??
Great link: What Every Computer Scientist Should Know About Floating-Point Arithmetic
After reading that you’ll realise that the floating-point representation of 1 isn’t quite the integer value 1. It’s close, but not quite, and that’s why your condition will always be false.
Why would you use
floats to store boolean data? Use an array ofbools or a bitvector.EDIT: I can’t actually think of any situation where (or why) you’d compare
floats to a literal, anyone know any?