Inaccuracy in matrices/rotation calculations

Hi developers!

In the mood to fix rotation issues with the Fbx exporter, I’ve been investigating about matrices and rotation calculations in Blender. Since Fbx needs bones matrices in parent space, the bone matrix is calculated this way:
bone_mat = parent_mat_inv @ bone.matrix
It seems there is accuracy issues when converting such matrices to quat and euler rotation with python. In certain cases, it leads to shaky animated bones, and or incorrect rotation (this one may be due to another cause though).
Has anyone any tip to make this conversion rock solid?


In the following video, bone1 is parented to bone2.

bone1 is animated.
Two other bones are added to visually witness the result of this conversion. One is quaternion based, the other is euler based. Each bone is keyframed with the resulting values of the matrix>rotation conversion We can see subtle differences in the output rotation.
The quaternion bone pops at frame 4 noticeably on the Y axis (~6°)
The euler bone is slightly offset (~0.15°) from the quaternion bone, but doesn’t pop. The offset is slight in this example, but in other testing file it lead to 1° offset which is more a problem, and also sometimes pop (shaky effect).
The blend file and script used to bake the values to these bones can be found here: