By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

Why won't my doubles hold their decimals?

P: 3
I am trying to write a program to create boundaries for a polygon on a two-dimensional coordinate plane. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. public class Example {
  2. public Example()
  3.     {
  4.         for (int y = 0; y < game1.o.l; y++)
  5.         {
  6.             System.out.println("( "+game1.o.bind[0][y]+", "+game1.o.bind[1][y]+" ) -> ( "+game1.o.bind[2][y]+", "+game1.o.bind[3][y]+" )\n");
  7.         }
  8.     }
  9. public class Obstacle {
  10.                 int cx, cy, dx, ex, ey;
  11.                 int w, l;
  12.                 double aw;
  13.                 int[][] bind;
  14.                 public Obstacle( int a, int cax, int cay, int eax, int eay, int dax )
  15.                 {
  16.                 cx = cax;
  17.                 cy = cay;
  18.                 dx = dax;
  19.                 ex = eax;
  20.                 ey = eay;
  21.                 w = ex - dx;
  22.                 l = ey - cy + 1;
  23.                 aw = (cx - dx)/(l - 1);
  24.                 System.out.println(aw+"\n");
  25.                 bind = new int[4][l]; 
  26.                 for ( int y = 0; y < l; y++ )
  27.                 {
  28.                     bind[0][y] = (int)Math.round( cx - ( aw * y ) );
  29.                     bind[1][y] = cy + y;
  30.                     bind[2][y] = (int)Math.round( cx - ( a * aw * y ) + w );
  31.                     bind[3][y] = cy + y;
  32.                 }
  33.             }
  34.         }
  35. }
Sorry it looks so messy. Anyways, when I ran it, it gives me this:

Expand|Select|Wrap|Line Numbers
  1. run:
  2. 0.0
  3.  
  4. 0.0
  5.  
  6. 0.0
  7.  
  8. 0.0
  9.  
  10. ( 10, 10 ) -> ( 15, 10 )
  11.  
  12. ( 10, 11 ) -> ( 15, 11 )
  13.  
  14. ( 10, 12 ) -> ( 15, 12 )
  15.  
  16. ( 10, 13 ) -> ( 15, 13 )
  17.  
  18. ( 10, 14 ) -> ( 15, 14 )
  19.  
  20. ( 10, 15 ) -> ( 15, 15 )
  21.  
  22. ( 10, 16 ) -> ( 15, 16 )
  23.  
  24. ( 10, 17 ) -> ( 15, 17 )
  25.  
  26. ( 10, 18 ) -> ( 15, 18 )
  27.  
  28. ( 10, 19 ) -> ( 15, 19 )
  29.  
  30. ( 10, 20 ) -> ( 15, 20 )
  31.  
  32. BUILD SUCCESSFUL (total time: 0 seconds)
  33.  
What am I doing wrong?
Jun 2 '12 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,422
How can that code even run? You're dividing by 0, that should throw an error.
Jun 3 '12 #2

P: 3
@Rabbit
The only place where division occurs is in line 23, and the only way you can divide by zero is if there is no difference between cy and ey, meaning the shape is a line, which I would not do within the parameters of this program. Although perhaps your confusion is related to that fact that I somehow forgot a certain line of code:

Expand|Select|Wrap|Line Numbers
  1. Obstacle o = new Obstacle(1,10,10,10,20,5);
Or maybe it's just because l and 1 look a bit alike in the code font.
Jun 3 '12 #3

Rabbit
Expert Mod 10K+
P: 12,422
That's probably what it was. It looked like one minus one to me. The reason you're not getting a fraction is probably because all the math is on ints. Change the 1 to 1.0 and that should force it into float math.
Jun 4 '12 #4

P: 8
IEEE floating point numbers cannot represent exact decimal values. Base 2 is not Base 10.
Jun 4 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.