# Loss of Precision (Double/Int)

• October 7th, 2010, 03:48 PM
Scotty
Loss of Precision (Double/Int)
Hiya. Im still a newbie so sorry! I get a loss of precision error on the Double (says should be int), however my intended values are decimals so I cannot use int.Works fine with int, but then I cant use decimals. Any way round this?

Thanks!

Code Java:

```public class officeBuilding { public static void main(String[] args) { int groDouPress, topDouPress; String groPress = JOptionPane.showInputDialog("Enter Pressure at Ground Level"); String topPress= JOptionPane.showInputDialog("Enter Pressure at roof of Building"); groDouPress = Double.parseDouble(groPress); topDouPress = Double.parseDouble(topPress); double feet = 25000*(Math.log(groDouPress/topDouPress)); JOptionPane.showMessageDialog(null,("The height is feet is " +feet), ("Building Height"), JOptionPane.INFORMATION_MESSAGE); } }```
• October 7th, 2010, 04:46 PM
Tjstretch
Re: Loss of Precision (Double/Int)
What do you mean by loss of precision?
There is a certain number of decimal places the point double can hold, and any more than that it is forced to round, you can't get an exact answer using double. If it is computing the math wrong let me give you an example of what it does.

Consider writing the value 1/3 in decimal notation:

0.333333333333333333

You would literally need an infinite number of 3's for it to give you 1/3, but a double cannot hold them, rather 64 bit(which correct me if I'm wrong relates to 14 decimal places), so it would look something like
0.3333333333334 (If I counted my digits right). Now if you multiplied it by another rounded number and another rounded number, it could possibly give you a very wrong answer. If your getting an answer like 0.123E2 It means it's giving it to you in scientific notation, and the E2 means * 10^2 There is a certain point where the number will just give you a wrong answer if the numbers are to big. Hope that helps
• October 7th, 2010, 05:07 PM
Scotty
Re: Loss of Precision (Double/Int)

Code java:

```D:\java\officeBuilding.java:9: possible loss of precision found : double required: int groDouPress = Double.parseDouble(groPress); ^ D:\java\officeBuilding.java:10: possible loss of precision found : double required: int topDouPress = Double.parseDouble(topPress); ^ 2 errors```

I dont get this if I use int, but I get an error when I run it if I use decimal values (obviously)
• October 7th, 2010, 06:20 PM
Norm
Re: Loss of Precision (Double/Int)
The variable on the left of the = is an int. The method on the right of the = does not return an int.
Use a cast to change the value to an int.
• October 7th, 2010, 06:22 PM
aussiemcgr
Re: Loss of Precision (Double/Int)
The error means that you are attempting to put a double value (a value with decimals) in an int (a value without decimals). This will give you an error as the compiler is worried you made a mistake and all hell will break loose or something.

Look at this bit of your code:
Code java:

```int groDouPress, topDouPress; ... groDouPress = Double.parseDouble(groPress); topDouPress = Double.parseDouble(topPress);```
You declared groDouPress and topDouPress as ints, but you attempt to set them as doubles.

To fix this, you just need to declare them as doubles:
Code java:

```double groDouPress, topDouPress; ... groDouPress = Double.parseDouble(groPress); topDouPress = Double.parseDouble(topPress);```

Tell me if that makes sense.

EDIT: Norm, he is trying to use decimals but declared it incorrectly:
Quote:

however my intended values are decimals so I cannot use int.Works fine with int, but then I cant use decimals. Any way round this?
• October 7th, 2010, 08:25 PM
javapenguin
Re: Loss of Precision (Double/Int)
int groDouPress, topDouPress;

what happens if you change those two to type double?
• October 9th, 2010, 01:45 PM
Scotty
Re: Loss of Precision (Double/Int)
Thank you!