Math Mod Containing Numbers

Posted by TheDarkIn1978 on Stack Overflow See other posts from Stack Overflow or by TheDarkIn1978
Published on 2010-06-11T02:56:01Z Indexed on 2010/06/11 3:03 UTC
Read the original article Hit count: 199

Filed under:

i would like to write a simple line of code, without resorting to if statements, that would evaluate whether a number is within a certain range. i can evaluate from 0 - Max by using the modulus.

30 % 90 = 30  //great

however, if the test number is greater than the maximum, using modulus will simply start it at 0 for the remaining, where as i would like to limit it to the maximum if it's past the maximum

94 % 90 = 4  //i would like answer to be 90

it becomes even more complicated, to me anyway, if i introduce a minimum for the range. for example:

minimum = 10
maximum = 90

therefore, any number i evaluate should be either within range, or the minimum value if it's below range and the maximum value if it's above range

-76 should be 10
2 should be 10
30 should be 30
89 should be 89
98 should be 90
23553 should be 90

is it possible to evaluate this with one line of code without using if statements?

© Stack Overflow or respective owner

Related posts about math