Smarth code wanted for normalising degree's

Started by
2 comments, last by gimp 22 years, 11 months ago
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
Chris Brodie
Advertisement
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.
D.V.Carpe Diem
Thanks... I have a vague half thought of a method that would involve dividing the degree''s by the max and taking the remainder...
Chris Brodie
Got it...

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

Chris Brodie

This topic is closed to new replies.

Advertisement