Mapping from a rotational quaternion to a single angle I have given an attitude quaternion that describes the rotation of a device. This roation is relative to the magnetic north, so the quaternion includes implicit information about the heading of the device. Now I want to extract this information out of the quaternion, but I cant find a way to do this without having singularities, if the device is held in certain positions. 
My goal is to get a mapping from the attitude to the heading angle, such that small changes in the input quaternion result in only small differences in the resulting heading angle.
All previous tries ended up to have singularities where small changes of attitude result in a huge jump (worst case even a jump from $0$ to $\pi$). 
So my guess is, that there does not exist a surjective mapping $f\colon\, \mathbb H \to (-\pi,\pi]$ (a projection from 4D to a 1D space), that satifies this restriction of not having discontinuous points. I just can not mathematically prove it.
Is there a prove to this assumption, or maybe a disprove by example? I am stuck here...
Thanks in advance for any help!
 A: There do exist surjective continuous mappings $\mathbb H \to (-\pi,\pi]$, but these are not what you want (one example is to take a surjective map $\mathbb R\to(-\pi,\pi]$ like $x\to\pi(\frac2{1+x^2}-1)$ and compose it with any $\mathbb R$-linear projection $\mathbb H\to\mathbb R$). I think at the very least you want the map to be surjective when restricted to the unit quaternions, and already this is not possible. The reason is that the unit quaternions form a compact set while the interval $(-\pi,\pi]$ is not compact, and it is a theorem that the image of a compact set by a continuous map is always compact.
If you glue together the endpoints of $(-\pi,\pi]$ to form a circle (which is compact), there do exist continuous surjective maps. However these are probably still not what you want, because no doubt you want there to be some loop in the unit quaternions that maps to a complete tour around the circle. This is not possible because you can always continuously contract the loop within the unit quaternions to a point, and the image under your mapping would contract the tour around the circle continuously to a point, and again this is impossible.
These two impossibilities are related to the discontinuities of the longitude along respectively the date line and at the poles that Jyrki Lahtonen mentioned.
These kind of obstructions are studied in homology- and homotopy theory.
