# Thread: possible loss of precision?

1. Member Join Date
Feb 2010
Posts
75
Rep Power
0

## 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.  Reply With Quote

2. Member Join Date
Aug 2009
Posts
76
Rep Power
0

## 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.  Reply With Quote

3. Senior Member Join Date
Mar 2010
Posts
266
Rep Power
11

## 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?)  Reply With Quote

4. Member Join Date
Feb 2010
Posts
75
Rep Power
0

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

5. ##  Originally Posted by gandalf5166 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  Reply With Quote

6. Member Join Date
Feb 2010
Posts
75
Rep Power
0

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

7. ##  Originally Posted by gandalf5166 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  Reply With Quote

#### Posting Permissions

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