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?

  • 1
    $\begingroup$ 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. $\endgroup$ Commented Feb 23, 2012 at 17:47
  • $\begingroup$ This is not a question about floating-point, but a question on the application of mathematical concepts. $\endgroup$
    – shuhalo
    Commented Feb 23, 2012 at 18:59
  • 1
    $\begingroup$ 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? $\endgroup$
    – MRocklin
    Commented Feb 23, 2012 at 23:47

1 Answer 1


The answer to this question depends largely on your application. Instead of sweating the exact numerical implementation think more about what each potential embodiment you suggested means. For example, does one or more of the computed distances have a physical interpretation? Are the units and scales of the vector components the same?

In the scenario where all parameters have the same units, and different scales of values, you should think more about the best way to dimension your system. Nondimensionalization helps to chase away the evil numerical precision gnomes.

As you use x, y, and z as value names, I suspect you are looking at some sort of position in space. The two norm has the distinct advantage of having a continuous derivative, so lacking any real problem information I would probably start there.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.