[cfe-dev] [libcxx/test] Floating point comparisons

Edward Meewis ed at extraordinarymachine.nl
Thu Dec 1 10:54:34 PST 2011


Hi,

I am experimenting with libcxx on FreeBSD and got a lot of asserts on 
floating point comparisons, most notably in 
'libcxx/test/containers/unord', e.g.:

Assertion failed: (c.load_factor() == (float)c.size()/c.bucket_count()), 
function main, file assign_move.pass.cpp, line 71.

Switching on optimization makes them go away. I'm guessing one of the 
values gets promoted from a float to a double somewhere, which makes the 
comparison fail.

The question is: do the lvalue and rvalue need to be exactly the same? I 
think it would be better to use something like:

assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < 
FLT_EPSILON);

to sidestep the issue. It also has the benefit of being more 
implementation independent?

Regards, Ed.



More information about the cfe-dev mailing list