And perhaps if the value is NaN, then do some direct assignment to the last known “good” value, until it becomes not NaN. (Yeah that’s technically a double negative haha)
Quaternions don’t work this way, you’ll be better off using Euler angles as suggested above
On this site you can calaculate angles ↔ quaternions and see how they work.
Also it is normal to have “different” angles collapsing to same Quaternion.