# Normalize an angle in unity

How can I normalize angle between min value and max value?

like normalizing -1136316.78186234° between , 360° or between 180° , 360°.

Node: for now I'm using double value as angle.

• When you say "normalize", do you mean that the result will be between 0 and 1? Also, how would you like to normalize an angle for a range when it doesn't fall inside that range, like normalizing 150° between 180° and 360°? Commented Jun 4 at 17:35

That's what the fmod function will do for you:

result = fmod(input, 360);


Having said that, consider not having the explicit angle in the first place. I prefer manipulating a sin/cos pair using the various trigonometric identities. That will not be affected by wrapping in this way.

• Note that in C#, this would be written result = input % 360.0 as there's no top-level fmod function like there is in shader languages. This will however return a negative number for negative inputs, not in the 0-360° range that was requested. Commented Jun 4 at 13:15

For single-precision (32-bit) floats, you can use:

result = Mathf.Repeat(input, 360f);


or

result = 180f - Mathf.DeltaAngle(input, 180f);


These both take an input over any range and map it to the range [0, 360)

But since you're working with a double for the angle, you can peek at the implementation of Repeat to write equivalent double-precision code:

using System;

public class MathExtensions {

public static double Wrap0_360(this double degrees)
{
return Math.Clamp(degrees - Math.Floor(degrees / 360.0) * 360.0, 0.0, 360.0);
}
}


This lets you write myAngle = myAngle.Wrap0_360()

• The question says "normalize", so shouldn't the result be converted to the range 0 - 1? Commented Jun 4 at 17:34
• "Normalize" means to map into a standard range or to a standard magnitude. For many contexts, that range is [0, 1] (or [0, 1)), but for angles (-180°, 180°], [0°, 360°), (- π, π], or [0, 2 π) are all common choices. The question explicitly requests [0°, 360°) as one of the proposed outputs. Commented Jun 4 at 17:48
• I feel like "normalizing [...] between 0° , 360°" could mean "normalize the angle into the range [0, 1) such that 0 is 0° and 1 is 360°" but maybe I'm overthinking it. Commented Jun 4 at 17:53
• OP is welcome to reply or edit with a clarification if that is what they meant. Commented Jun 4 at 18:25