Here's a kind of generic solution:
while (a < (b-180)) {
a = a + 360;
}
a = a - 360;
while ((b-180) > a) {
b = b - 360;
}
b = b + 360
If you run this, you'll end up with an angle $a$, and an angle $b$ that's numerically greater than $a$, but not by more than 360. It's not very efficient code if $a$ and $b$ differ by $100,000$ degrees, for instance, but for the cases you care about, it works surprisingly well in practice.
What do you do with these values? Well, you might say that if $b$ is more than 180 degrees above $a$, then $a$ is greater than $b$, but if $b$ is less than 180 degrees more than $a$, then $b$ is greater than $a$. if $b - a = 180$, all bets are off. :)