how do I limit a range of numbers to between 0 and 1?

scitunesscitunes Member, Sous Chef Posts: 4,047
edited November -1 in Working with GS (Mac)
I have the accelerometer value + a constant (comes from my calibration method) and I want to limit the range of the sum of these two numbers to no less than 0 and no more than 1. So -.2 returns a value of 0 and 1.3 returns a value of 1.

Any ideas? floor? ceil?

Thanks!

Comments

  • PhoticsPhotics Member Posts: 4,172
    You might be able to use rules to straighten things out, so if the value is less than zero just make it zero.

    ...and what about multipliers...

    floor((abs(your-value*10))/10)
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Here is the Math formula to constrain a number between both an upper limit and a lower limit:

    min(upper limit, max(lower limit, original number));

    So in your case it would look like this (with limits of 0 and 1):

    game.constrainedValue = min(1,max(0,(accelerometer value + constant)))

    Hope this helps!
    Joe
  • PhoticsPhotics Member Posts: 4,172
    Heh, as I woke up this morning and realized that my formula wouldn't work. Negative numbers wouldn't become zero.

    Although, I think the min and max numbers are reversed in the newer formula.
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    thanks guys! I'll give this a try.
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    Thanks, Joe! It seems to be working for my tilt calibration system. I appreciate it!
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Cool! Glad I could help!

    @photics: The min() and max() are not reversed...
  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    firemaplegames said:
    @photics: The min() and max() are not reversed...

    Right. `MIN` does not "set" the low end of a range, it just selects the smallest of the listed values, while `MAX` selects the largest of the two...

    Think of it this way:

    `MIN` = getMin
    `MAX` = getMax
Sign In or Register to comment.