Why won't my doubles hold their decimals?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathgeek314
    New Member
    • Jun 2012
    • 3

    Why won't my doubles hold their decimals?

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

    Code:
    public class Example {
    public Example()
        {
            for (int y = 0; y < game1.o.l; y++)
            {
                System.out.println("( "+game1.o.bind[0][y]+", "+game1.o.bind[1][y]+" ) -> ( "+game1.o.bind[2][y]+", "+game1.o.bind[3][y]+" )\n");
            }
        }
    public class Obstacle {
                    int cx, cy, dx, ex, ey;
                    int w, l;
                    double aw;
                    int[][] bind;
                    public Obstacle( int a, int cax, int cay, int eax, int eay, int dax )
                    {
                    cx = cax;
                    cy = cay;
                    dx = dax;
                    ex = eax;
                    ey = eay;
                    w = ex - dx;
                    l = ey - cy + 1;
                    aw = (cx - dx)/(l - 1);
                    System.out.println(aw+"\n");
                    bind = new int[4][l]; 
                    for ( int y = 0; y < l; y++ )
                    {
                        bind[0][y] = (int)Math.round( cx - ( aw * y ) );
                        bind[1][y] = cy + y;
                        bind[2][y] = (int)Math.round( cx - ( a * aw * y ) + w );
                        bind[3][y] = cy + y;
                    }
                }
            }
    }
    Sorry it looks so messy. Anyways, when I ran it, it gives me this:

    Code:
    run:
    0.0
    
    0.0
    
    0.0
    
    0.0
    
    ( 10, 10 ) -> ( 15, 10 )
    
    ( 10, 11 ) -> ( 15, 11 )
    
    ( 10, 12 ) -> ( 15, 12 )
    
    ( 10, 13 ) -> ( 15, 13 )
    
    ( 10, 14 ) -> ( 15, 14 )
    
    ( 10, 15 ) -> ( 15, 15 )
    
    ( 10, 16 ) -> ( 15, 16 )
    
    ( 10, 17 ) -> ( 15, 17 )
    
    ( 10, 18 ) -> ( 15, 18 )
    
    ( 10, 19 ) -> ( 15, 19 )
    
    ( 10, 20 ) -> ( 15, 20 )
    
    BUILD SUCCESSFUL (total time: 0 seconds)
    What am I doing wrong?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    How can that code even run? You're dividing by 0, that should throw an error.

    Comment

    • mathgeek314
      New Member
      • Jun 2012
      • 3

      #3
      Originally posted by Rabbit
      How can that code even run? You're dividing by 0, that should throw an error.
      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:

      Code:
      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.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • ddyer
          New Member
          • May 2010
          • 8

          #5
          IEEE floating point numbers cannot represent exact decimal values. Base 2 is not Base 10.

          Comment

          Working...