Is floating-point == ever OK?

Yes, you are guaranteed that whole numbers, including 0.0, compare with ==

Of course you have to be a little careful with how you got the whole number in the first place, assignment is safe but the result of any calculation is suspect

ps there are a set of real numbers that do have a perfect reproduction as a float (think of 1/2, 1/4 1/8 etc) but you probably don’t know in advance that you have one of these.

Just to clarify. It is guaranteed by IEEE 754 that float representions of integers (whole numbers) within range, are exact.

float a=1.0;
float b=1.0;
a==b  // true

But you have to be careful how you get the whole numbers

float a=1.0/3.0;
a*3.0 == 1.0  // not true !!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)