[Gambas-user] function to "wrap" a number between upper and lower bounds

Benoît Minisini gambas at ...1...
Wed Dec 12 23:40:00 CET 2012


Le 12/12/2012 23:33, Kevin Fishburne a écrit :
> I need a function like:
>
> Wrap(number, lowerbound, upperbound)
>
> such that Wrap(5,1, 3) will return 2. In other words, it "wraps" the
> number around the supplied bounds and returns where it "lands". I
> believe it would work similarly to assigning a value to a datatype
> outside its scope, like assigning 300 to a Byte type.
>
> I looked up some solutions online and none of them really seemed to work
> properly and their comments were surrounded by debate. Is there an
> efficient way to do this, and does anyone think it would be a useful
> function to add to Gambas itself?
>

Wrap(x, y, z) = y + ((x - y) MOD (z - y + 1))

-- 
Benoît Minisini




More information about the User mailing list