if(float == double)

Interesting read on why not to compare a float with a seemingly equal value double. Explains it pretty well for people who never knew what happened behind the scenes with floating-point numbers.

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
This is a corollary of a much simpler rule: don't ever use the equality comparison for floating point values.

I raised this issue on the SFML forums a while back. The author of SFML expects the users to use their own custom floating point types with a nice operator== overload. Good for him, that allows for great simplification of the vector comparison operator; of course at the reasonable price of blowing up in the user's face.

My life... seems to morph into a roller coaster of hilarious despair.
This is a corollary of a much simpler rule: don't ever use the equality comparison for floating point values


The author here seems to think that comparing floating-point values of the same (ie float to float, or double to double), is alright as long as you haven't performed any complex calculations on them.

And as I just noticed, that link does not take you to the spot I was intending. Somehow he has 2 different pages with the same URL, unless you go at it a different way. Anyway, here's think the link I was going for.

http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
What kind of ***hole writes a float-based class without an approximate equality comparison, anyway?

The author here seems to think that comparing floating-point values of the same (ie float to float, or double to double), is alright as long as you haven't performed any complex calculations on them.
Of course, in practice it's almost always impossible to know what operations were performed on the data. If you did know that, you probably wouldn't need the comparison in the first place.
Last edited on
Topic archived. No new replies allowed.