#### Archived

This topic is now archived and is closed to further replies.

# Smarth code wanted for normalising degree's

This topic is 6360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Something I do often in my code is normalise a numberto within a certain range. For example ensuring degree's are 0-359 or radians are 0-1. Can anyone suggest a smart trick for pulling this off? The best I can come up with is a sceme that cycles of the number adding or subtracting 360 until it falls in the range I want. Kludge. Is there a better way? Many thanks Chris Edited by - gimp on April 24, 2001 10:45:58 PM

##### Share on other sites
for integers

#define sgn(num) ((num) < 0 ? -1 : 1)
#define NORMALISE(num,min,max) sgn(num) * ((abs(num) - min) % (max - min)) + min

something like that.

For floats multiple num,min and max by 10000 covert to integers run the macro, divide result by 10000 (or whatever precision you want).

D.V.

##### Share on other sites
Thanks... I have a vague half thought of a method that would involve dividing the degree''s by the max and taking the remainder...

##### Share on other sites
Got it...

  inline float NormaliseDegrees(const float a_Value){ return fmod(a_Value, 360.0f);}

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007477
• ### Who's Online (See full list)

There are no registered users currently online

×