General modulo-question

Discussion in 'Java' started by Christian Kruggel, Jul 7, 2003.

  1. 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
    Christian Kruggel, Jul 7, 2003
    #1
    1. Advertising

  2. Christian Kruggel

    Brad BARCLAY Guest

    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
    back down into your desired range by adding b.

    Would that satisfy your requirement?

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    Brad BARCLAY, Jul 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    652
    Darryl L. Pierce
    May 20, 2004
  2. Tjerk Wolterink

    [xsl] sort & modulo

    Tjerk Wolterink, Apr 21, 2005, in forum: XML
    Replies:
    3
    Views:
    556
    Dimitre Novatchev
    Apr 22, 2005
  3. silentlights

    Fast Division/Modulo Operation

    silentlights, Apr 16, 2004, in forum: C Programming
    Replies:
    8
    Views:
    966
    Dik T. Winter
    Apr 23, 2004
  4. Griff

    Problems using modulo

    Griff, Apr 19, 2004, in forum: Python
    Replies:
    5
    Views:
    384
    Diez B. Roggisch
    Apr 20, 2004
  5. Griff

    Problems using modulo

    Griff, Apr 19, 2004, in forum: Python
    Replies:
    0
    Views:
    284
    Griff
    Apr 19, 2004
Loading...

Share This Page