Java Arithmetic - Using MOD (%)

Discussion in 'Java' started by Steven Davies, Feb 1, 2005.

  1. I'm trying to iterate over an array, copying objects from one array into
    another and "wrap around" over the boundary of the initial array, but I
    have a problem:

    I'm using the % operator to try and get the value to wrap around, but it
    keeps jumping to negative numbers.

    Here's a code snippet:

    > for (int x = 0; x < (sightDist * 2 + 1); x++) {
    > for (int y = 0; y < (sightDist * 2 + 1); y++) {
    > visibleArray[x][y] = theTiles[oldX][oldY];
    > oldY = (oldY + 1) % 100;
    > }
    > oldY = (oldY - (sightDist * 2 + 1)) % 100;
    > oldX = (oldX + 1) % 100;
    > }


    Does anyone have any idea why the statements at the bottom don't seem to
    mod the number properly? Any more details you need, ask me :)

    (oldX and oldY are definitely positive before this is executed, the
    array called theTiles is 100 by 100.)

    Thanks,
    Steven Davies
     
    Steven Davies, Feb 1, 2005
    #1
    1. Advertising

  2. Steven Davies

    Guest

    What value does it give you?
     
    , Feb 1, 2005
    #2
    1. Advertising

  3. Steven Davies

    Yamin Guest

    a negative return from a mod is perfectly valid mathemetically.
    99 % 100 is the same as -1 % 100. I haven't done any research into
    Java's implementation of mod, but perhaps it is returning that negative
    version. Assuming this is the case...you can get the mod you want by:
    value = equation % 100;
    if( value < 0) value= value + 100;

    Yamin
     
    Yamin, Feb 1, 2005
    #3
  4. Yamin wrote:
    > a negative return from a mod is perfectly valid mathemetically.
    > 99 % 100 is the same as -1 % 100. I haven't done any research into
    > Java's implementation of mod, but perhaps it is returning that negative
    > version. Assuming this is the case...you can get the mod you want by:
    > value = equation % 100;
    > if( value < 0) value= value + 100;
    >
    > Yamin


    Yeah, I read the Java language spec and indeed if you do z = x % y and x
    is negative, so is your result. A quick z+=array.length worked a treat :)

    Thanks.
    --
    Steven Davies, sed3
    Computer Science and German
     
    Steven Davies, Feb 1, 2005
    #4
    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. TheVooDooChild

    Using MOD function in <logic:equal> tag

    TheVooDooChild, Feb 24, 2005, in forum: Java
    Replies:
    2
    Views:
    2,937
    Oscar kind
    Feb 24, 2005
  2. Hari Sekhon
    Replies:
    0
    Views:
    555
    Hari Sekhon
    Jun 20, 2006
  3. joshc
    Replies:
    5
    Views:
    588
    Keith Thompson
    Mar 31, 2005
  4. ryles
    Replies:
    3
    Views:
    575
    Piet van Oostrum
    Jul 26, 2009
  5. T. Onoma
    Replies:
    9
    Views:
    390
    Dave Thomas
    Dec 15, 2003
Loading...

Share This Page