Velocity Reviews > Java > General modulo-question

# General modulo-question

Christian Kruggel
Guest
Posts: n/a

 07-07-2003
Hi!

I try to do some basic programming and got stuck with the
modulo-operator an the problem how to shift any number n into the
interval [b;e] with b < 0 and e > 0.

My first approach was to calculate the spread of [b;e] by e - b and
then to calculate n % spread. Unfortunately this just works for
numbers that are already in [b;e] while other numbers smaller than b
and greater than e are not shifted correctly.

Has anybody programmed such calculation already and could give me a
helping hint?

Christian

Guest
Posts: n/a

 07-07-2003
Christian Kruggel wrote:
> Hi!
>
> I try to do some basic programming and got stuck with the
> modulo-operator an the problem how to shift any number n into the
> interval [b;e] with b < 0 and e > 0.
>
> My first approach was to calculate the spread of [b;e] by e - b and
> then to calculate n % spread. Unfortunately this just works for
> numbers that are already in [b;e] while other numbers smaller than b
> and greater than e are not shifted correctly.
>
> Has anybody programmed such calculation already and could give me a
> helping hint?

It might be easier to first treat the interval as being [0;e-b], and
then do the modulus (n-b)%(e-b). You should then be able to shift this

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org