Basic quaternion math yields NAN after a few loops

Hi,

I’m working with quaternions and I found a very nasty issue with quaternions. It appears that multiple compositions with null quaternions can yield invalid data. The members of the output quaternion will have a denormalized value (between absolute zero and the minimum float quantum), with a nice #DEN suffix in Visual Studio.

FQuat Test1 = FQuat::MakeFromEuler(FVector(0, 0, 0));
FQuat Test2 = FQuat::MakeFromEuler(FVector(0, 0, 0));

for (int i = 0; i < 100; i++)
{
	FQuat Test3 = Test1 * Test2.Inverse();
	Test1 = Test1 * Test3;
}

FRotator Debug(Test1.Rotator());
UE_LOG(LogFlare, Display, TEXT("%f %f %f"), Debug.Roll, Debug.Pitch, Debug.Yaw);

LogFlare:Display: 1.#QNAN0 -90.000000 -1.#IND00

Things get ugly after ~50 iterations. 10 is fine, 100 fails. Once one quaternion has failed, it will invalidate every other quaternion as well through math operations.

I understand that the above code can be seen as looking for trouble, but the above logic is actually in my game’s Tick and thus, less than 2 seconds worth of empty (yet valid) data will completely crash the logic.

Any thought?

The solution was to normalize the quaternion at each step.