• Post Reply Bookmark Topic Watch Topic
  • New Topic

"Possible loss of precision" error  RSS feed

 
raymond edwards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to java. I just started learning it a few hours ago. And now I'm stuck. I downloaded an editor called bluej from www.bluej.org. The compiler is getting stuck on a line: "investment = (investment * 1.4);". Its displaying "possible loss of precision error". For some reason its not letting me assign a new value to the var investment. Please help.
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing investment is an int? You have a few options:

1) Make it a double instead of an int.
2) Cast it like so: investment = (int)(investment * 1.4);
but you'll lose the decimal places.
3) Round it using a rounding function.

Since we're talking money here, you'll probably want to use doubles

Edit: The reason your code doesn't work is because integers cannot have decimal places, and you were trying to assign it a value with a decimal place (a 'double' in Java). Hence the "loss of precision."
[ June 19, 2004: Message edited by: Darin Niard ]
 
raymond edwards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the same thing will happen if investment is a float.

In addition to the other suggestions, you could change the line to

In your version, 1.4 is a double, so the product is a double, so assigning the product to a float results in a possible loss of precision. 1.4f is a float, so the product is a float.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!