Is there a way to map floats to ints or unsigned ints so that with the exception of NaN, order is preserved?
So if a and b are floats, and F is the mapping function,
a < b implies F(a) < F(b)
and
a == b implies F(a) == F(b)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hm, just out of the
DawsonCompareroutine in Game Programming Gems 6, it’s a normal bit-cast followed by a sign flip (since negative floats order opposite then negative integers). I’ll borrow that idea.You have:
If you can guarantee your
intis 32 bits, you can just use that.Fun fact: The book goes on to use this (note, not with the exact code I present, since I stripped out the float-to-int part) to compare floating point values with tolerance:
This compares floats as true if their integer representations are within a certain range. (He uses 1000 as a good default.) A branch-less version called the
LomontCompareis presented with the same idea, but you have to buy the book for that. 🙂