Comparing vectors

In order to compare two vectors – to determine whether both vectors point to the same place, we need to compare the coordinates of both vectors and if they are equal, then the vectors are also equal. However, the coordinates of the vectors are specified by float values, the comparison of which for equality, due to the peculiarities of storing this type in computer memory, is never accurate.

Let’s look at this with an example. Add two objects to the scene, each of one edge. This edge will denote a vector in the 3D scene space. Name the objects “V0” and “V1” and place them in the scene in exactly the same place – from the beginning of the scene (0, 0, 0) to any other point. The easiest way is first to create one object, and then make a copy of it by pressing shift + d.

Now we can get the coordinates of the vectors formed by our objects:

As we can see, the coordinates of the vectors are completely equal.

Looking at this, it seems that we can write a simple function that will compare the corresponding coordinates of two vectors and return the result of the comparison for all three coordinates.

Something like this:

And if we try to test this function on our two vectors, we will get the correct answer.

However, let’s not rejoice too soon.

We know from math course that the value will not change if we perform any mathematical operation on it, and then perform the opposite operation. For example, add one, and then subtract one. Or divide by ten, and then multiply by ten.

Let’s experiment by dividing the X coordinate of our vector “V1” by three, and then multiply it back by three.

The final value should not differ from the original, but if we print the vector coordinates again, we will see some changes.

Starting from about the fifth decimal place, the value of the X coordinate now differs from the same value of the second vector, which we did not touch.

There are no contradictions with mathematics here, we just need to remember that computer memory is designed in such a way that it cannot store floating-point numbers with absolute precision.

And now, if we try to compare vectors again using the function we wrote above, we will get the wrong result.

How then do we compare vectors?

To get more predictable results when comparing floating-point numbers, they are usually not compared for absolute equality, but rather checked to see if one number differs from the other by more than a given delta – usually a small number with three to five zeros after the decimal point.

The simplest equation for such a comparison looks like this:

and its more applicable analogue:

This equation version allows us to conveniently spread the comparison delta if two very large numbers are compared. At the same time, such a formula does not work well if two numbers close to zero are compared.

Therefore, the most commonly used equation is one that takes into account two deltas: relative “rel_tol” and absolute “abs_tol”. In this case, the formula looks like this:

Let’s return to our vectors and try to apply this formula in the function of their comparison.

And use it for comparison of our vectors with different delta.

While our vectors are absolutely identical, we get the following result:

And after the noise appearing caused by mathematical operations:

So, comparing two vectors with given the delta, we can assume that the vectors are the same within the specified measurement error.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments