Smarth code wanted for normalising degree's
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
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.
#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.
Thanks... I have a vague half thought of a method that would involve dividing the degree''s by the max and taking the remainder...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement