# What's the right way to compare vectors in floating-point?

I know that I should use a tolerance for comparing floating point numbers. But for comparing vectors, I can think of 3 possible solutions corresponding to different distance metrics:

1. Compare the components of each vector individually: the vectors are equal if all 3 are within tolerance. This option would behave like the uniform norm, giving a cube of tolerance.
2. Compare the sum of all the absolute differences to some tolerance. This would behave like the taxicab norm, giving a simplex of tolerance.
3. Calculate the Euclidean length of (vecA - vecB) and see if it's within tolerance. This would give the standard Euclidean norm with a sphere of tolerance.

But my main concern is numerical stability. Euclidean norm "feels like" the best option, but I'm worried that all the calculations would induce more rounding errors. To a lesser extent option 2 could also introduce errors. (For example, if the x component of the vectors is much larger than y and z, adding together all the differences could swamp any contributions from y and z.) So I'm currently leaning towards option 1.

Can anyone weigh in with an authoritative take on this problem?

• It depends on what you want. Are you checking if the vectors have equal components, or equal magnitudes only? To check for equality in a programming sense, then option 1 is the way to go. – ja72 Feb 23 '12 at 17:47
• This is not a question about floating-point, but a question on the application of mathematical concepts. – shuhalo Feb 23 '12 at 18:59
• For most applications all of these approaches will be satisfactory, particularly if, as your wording implies, you're only interested in three dimensions. My first response would be "don't worry about it." Is there a specific reason why you're worrying about rounding errors in this context? – MRocklin Feb 23 '12 at 23:47