Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Negative integer division  (Read 2723 times)
0 Members and 1 Guest are viewing this topic.
Offline loom_weaver

JGO Coder


Medals: 17



« Posted 2011-11-21 01:32:35 »

Here's a puzzler for you.

I'm designing continuous terrain exploration by dynamically loading/unloading 100x100 blocks of tiles.

Given the following coordinates (same for x or y-axis):
1  
...|-300 ... -201|-200 ... -101|-100 ... -1|0 ... 99|100 ... 199|200 ... 299|...


Can you come up with a simple function that given an int, returns the anchor point?  The anchor point is the left-hand number in the range and is a multiple of 100.  For example for any value between -200 ... -101 return -200.  For 100 ... 199 return 100.

The biggest problem for me is that integer division of negative values seems to be different from language to language!  (-1 / 100) gives me -1 in python but 0 in Java.   Huh

Otherwise I'd use f(x) = x / 100 * 100
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-11-21 01:38:45 »

1  
return (x - ( x<0 ? 99 : 0 )) / 100 * 100;

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SimonH
« Reply #2 - Posted 2011-11-21 01:55:05 »

Another option would be to limit the play area to +ve coordinates only.
Integer.MAX_VALUE*Integer.MAX_VALUE is a quarter the size but it's still very, very big!

People make games and games make people
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2011-11-21 02:17:13 »

1  
return (x - ( x<0 ? 99 : 0 )) / 100 * 100;

... or just
1  
return (int)Math.floor(x/100.0);

It might be slower, though, but it's at least more readable.  Wink

Myomyomyo.
Offline counterp

Senior Member


Medals: 11



« Reply #4 - Posted 2011-11-21 03:00:02 »

1  
2  
3  
   public static final int anchor(int in) {
      return (in / 100 + (in >> 31)) * 100;
   }


or

1  
2  
3  
   public static final int anchor(int in) {
      return (in / 100 - (in >>> 31)) * 100;
   }


depending on if you want -100 to be -200 or -100
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2011-11-21 03:18:31 »

1  
2  
3  
   public static final int anchor(int in) {
      return (in / 100 + (in >> 31)) * 100;
   }


or

1  
2  
3  
   public static final int anchor(int in) {
      return (in / 100 - (in >>> 31)) * 100;
   }


depending on if you want -100 to be -200 or -100
both these examples are incorrect (partly because they are identical), loom_weaver is very clear about the spec. you're off by one for all negative multiples of 100. there is a reason i used 99 as opposed to 100 in my solution. both -1 and -100 should result in -100.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #6 - Posted 2011-11-21 03:36:40 »

1  
return (x - ( x<0 ? 99 : 0 )) / 100 * 100;


f(-250) = -400.  Should be -300.

I think this is the best I can do.  I was hoping for a non-branching version:

1  
2  
3  
4  
5  
6  
7  
int anchor(int x) {
    if (x < 0) {
        return -((-x + 99) / 100 * 100)
    } else {
        return x / 100 * 100
    }
}
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2011-11-21 03:42:12 »

1  
return (x - ( x<0 ? 99 : 0 )) / 100 * 100;


f(-250) = -400.  Should be -300.
I don't know how you tested that, but it simply returns -300, as it should.

1  
2  
3  
4  
5  
6  
7  
8  
9  
   public static final int anchor(int x)
   {
      return (x - ( x<0 ? 99 : 0 )) / 100 * 100;
   }

   public static void main(String[] args)
   {
      System.out.println(anchor(-250)); // "-300"
  }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #8 - Posted 2011-11-21 03:54:41 »


Oops... was testing in Python again.  Argh.  Oh well my lesson learned is be careful with division involving -ve integers.

In the old C standard it did not define what direction to round to (-ve INFINITY or 0) if dividing with a negative int.  In later standards, it rounds towards zero.

http://stackoverflow.com/questions/828092/python-style-integer-division-modulus-in-c
http://stackoverflow.com/questions/319880/integer-division-rounding-with-negatives-in-c
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2011-11-21 03:57:02 »

As you were hoping for one, here is a non-branching solution:

1  
2  
3  
4  
   public static final int anchor(int x)
   {
      return (x - ((x >>> 31) * 99)) / 100 * 100;
   }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2011-11-21 03:59:27 »

If you want the offset (relative to your anchor), in a non-branching way:

1  
2  
3  
4  
   public static final int offset(int x)
   {
      return (x%100+100)%100;
   }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #11 - Posted 2011-11-21 04:03:48 »

Dang... taking the high-order bit and multiplying by 99.

Thanks for the formulas guys!
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2011-11-21 04:06:10 »

it you really want to get it 'fast', you can take out the multiply...
1  
((x >>> 31) * 99) == ((x >> 31) & 99)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 340
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-11-21 06:40:59 »

1  
return (x - ( x<0 ? 99 : 0 )) / 100 * 100;

... or just
1  
return (int)Math.floor(x/100.0);

It might be slower, though, but it's at least more readable.  Wink
Your example is completely incorrect Wink

@Riven
That last one returns true or false, not a value. What is its purpose?
Hehe looked at it a bit more and realized that you mean to substitute the right-hand side for the left-hand side. Wow that is quite clever and much much faster!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!