I need to generate a hash from two float vectors, extracted from an orientation matrix. For this purpose I used the following code:
QByteArray hashNumber(6*sizeof(float), Qt::Uninitialized);
QVector3D vect1 = getVectorN();
QVector3D vect2 = getVectorO();
float* floatNumbers = (float*)hashNumber.data();
floatNumbers[0] = vect1.x();
floatNumbers[1] = vect1.y();
floatNumbers[2] = vect1.z();
floatNumbers[3] = vect2.x();
floatNumbers[4] = vect2.y();
floatNumbers[5] = vect2.z();
The problem is that floating point arithmetic has precision problems so most of the time when I make some direct and inverse operation on vectors, hash number is changing.
For instance, let's say that vect1
vector has value (0, 0, 5.4878049)
and corresponds to some object position. When I move it back and forth, vect1
value changes in last digits (0, 0, 5.4878048)
, hence the hash value is not equal to previous.
I know that direct float values comparison is not correct, so the question is:
What is the best way to make hash form two float vectors?