Quote:Original post by jykUnfortunately you can't rely on anything like that when dealing with floating point numbers.Quote:Original post by dmailThe code that Sneftel posted is fine. What is required in this case is a strict weak ordering, which is exactly what the code implements.
Sneftel, yes I know how to create a sorting based on the individual components of the vector and if I were to write one it would not look like what you suggest for the point which you have skipped; floating point comparison i.e. the precision of floating points.
The classic counterexample is the x87 FPU which internally performs calculations on 80-bit registers, and typically only stores truncated 32-bit floats or 64-bit doubles in memory. This means that a < b and b < a can both be true, if the optimizer decides to spill the a or b register to memory between the comparisons.
And then there's NaNs of course, which always compare false. Not that you're likely to encounter any in this example but you can design some pretty nifty exploits by forcing one into, say, an algorithm which uses sentinels.