Rotational Limits

I’m trying to make it so that and object rotates to the mouse, but only within a specific range. In the class file for Object1, I want it to be able to rotate a maximum of 45 degrees in either direction from the rotation of Object2. So far this is all I could come up with:

rotation=Math.atan2(stage.mouseY-y,stage.mouseX-x)/(Math.PI/180);
if (rotation<Object2.rotation-45) {
	rotation=Object2.rotation-45;
}
if (rotation>Object2.rotation+45) {
	rotation=Object2.rotation+45;
}

The possible rotations of Object2 are 0, 45, 90, 135, 180, -135, -90, and -45. The code seems to work fine if the rotations are between -90 and 90 (the right half), but because the left half goes from -179 to 180 to 179, it obviously fails. How would I accomplish the effect I want? Thanks :smiley: