lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



"Luiz Henrique de Figueiredo" <lhf@tecgraf.puc-rio.br> wrote in message 20061221072046.A23167@lua.tecgraf.puc-rio.br">news:20061221072046.A23167@lua.tecgraf.puc-rio.br...
Directly compairing floating point numbers for equality is *always* a bad
idea.

Only if the numbers are supposed to be the same number but are the result
of two different computations.

For example, you can find that a floating point value which is not a
NaN is not equal to itself.

I find this very surprising. And possibily wrong, ie against IEEE 754.
The explanation that followed made no sense to me. It would make sense for
floating point values held in *different* variables, but not for the *same*
variable. It seems to me that the gcc docs is spreading unnecessary FUD.
But I must be missing something...

I've heard this same explanation elsewere too. (Somebody saw this problem in production code). Apparently, the registers used in the FPU are oversized, and thus calculations performed there-in have morie bits than the datatype should. Thus when compairing a value in the register to the truncated value in the memory, they can be non-equal. I lack access to the IEEE 754 standard, and cannot make heads or tails of much of the ISO C standard when it talks about floating point numbers, so I'm not certain if this behavior violates either standard, but I've seen this documented more than once.

On the other hand, it is highly unlikely that the code listed would ever be affected by this, but it is still troubling.