Is binary equality comparison of floats correct?

The main issue is nan values, as these are never equal to themselves. There is also two representations of 0 (+0 and -0) that are equal but not binary equal.

So strictly speaking, you cannot use memcmp for them, as the answer would be mathematically incorrect.

If you know that you don't have nan or 0 values, then you can use memcmp.


The first thing I would do if I were you is to check your optimisation settings.

It's fine to use memcmp for an array of floating points but note that you could get different results to element-by-element ==. In particular, for IEEE754 floating point:

  1. +0.0 is defined to compare equal to -0.0.

  2. NaN is defined to compare not-equal to NaN.