Results 1 to 7 of 7
  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default possible loss of precision?

    OK, my program comes up with a possible loss of precision error when I compile:
    Java Code:
     class Square {
     	int x;
     	int y;
     	int order;
     	int[][] square;
     	int diag;
     	int num;
     	int counter;
     	public Square(int order) {
     	int diag = 1;
     	int num = 1;
     	int counter = 1;
     	}
     	public void oddSquare() {
     		int[][] square = new int[order][order];
     		y = 0;
     		x = (double)order / 2 + 0.5; //This is the line with the error
     		square[y][x] = num;
     		while (counter <= order) {
     		while (diag <= order) {
     			num += 1;
     			x += 1;
     			y -= 1;
     			if (x > order - 1) {
     				x = 0;
     			}
     			if (y < 0) {
     				y = order - 1;
     			}
     			square[y][x] = num;
     			diag += 1;
     		}
     		diag = 1;
     		counter += 1;
     		y += 1;
     		}
     	}
     	public void singleSquare() {
     		
     	}
     	public void doubleSquare() {
     		
     	}
     	public void printSquare() {
     		System.out.print("+");
     		for (int i = 1; i <= order; i++) {
     		System.out.print("-----");			
     		}
     		System.out.println("+");
     		for (int row = 0; row < order; row++) {
     			for (int col = 0; col < order; col++) {
    			System.out.print("|");
    			System.out.format("%3d" + square[row][col]);
     			}
     		System.out.println("|");
     		System.out.print("+");
     		for (int i = 1; i <= order; i++) {
     			System.out.print("----");			
     		}
     		System.out.println("+");
     		}
     	}
     }
    The program is made to make a magic square of orders 3-20. I haven't started on the methods that make even magic squares yet, but I'm trying to test the odd method. I have no idea what is wrong with it.

  2. #2
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    well, you are turning a double into an int. So if the double is 1.020983, it will just turn into 1 and that is your loss of precision.

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    lets look at this line:

    int x;
    //...
    x = (double)order / 2 + 0.5; //This is the line with the error

    The problem is that you're trying to assign a double value into an int variable.

    You know better than doing that!

    Solution 1: x = (int)((double)order / 2 + 0.5);

    Solution 2: x = (order + 1) / 2; (since order is also an int, why use doubles at all?)

  4. #4
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    I get it. Thanks! Didn't realize that it wouldn't automatically truncate. The reason I was casting was so it would round up.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gandalf5166 View Post
    I get it. Thanks! Didn't realize that it wouldn't automatically truncate. The reason I was casting was so it would round up.
    What else is it supposed to do? It has to squeeze a double value into a slot that can only contain an int. If you want it to round up add a bit less then 1 and explicitly cast the result to an int to keep the compiler happy so that it sees that you know (or think to know) what you're doing.

    kind regards,

    Jos

  6. #6
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    so that it sees that you know (or think to know) what you're doing.
    That seems kind of snide.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gandalf5166 View Post
    That seems kind of snide.
    Don't be too optimistic; I've been bitten by my own 'I know what I'm doing' attitude more than often.

    kind regards,

    Jos

Similar Threads

  1. Precision
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 11:36 AM
  2. Wrong precision
    By lclclc in forum New To Java
    Replies: 0
    Last Post: 09-28-2009, 09:55 AM
  3. data loss in the secure file transfer using java
    By antony75 in forum Networking
    Replies: 4
    Last Post: 02-09-2009, 06:56 AM
  4. help me- loss of precision error??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-01-2008, 09:23 AM
  5. Connection Loss
    By CrazyShells Slam in forum New To Java
    Replies: 0
    Last Post: 05-15-2008, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •