Archived

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

gimp

Smarth code wanted for normalising degree's

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Got it...

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

Share this post


Link to post
Share on other sites