• Post Reply Bookmark Topic Watch Topic
  • New Topic

float error  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i got this error:
Main.java:20: error: possible loss of precision
f = -2.3;

what does this mean?
i don't understand what's wrong with my declaration.

==========
EDIT:

i think i kind of got this figured.
by default java considers decimals as double, and if i want to make it into a float, i have to add an "F" at the end.
a similar problem happened with ints and longs.

can someone verify (And possibly add explanation) this?
thanks!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using floats is usually an error because of their poor precision. But 2.3 is a double literal, so it cannot be converted to a float without an explicit cast.
If you insist on using floats, you should write 2.3f
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abigail Decan wrote:i think i kind of got this figured.
by default java considers decimals as double, and if i want to make it into a float, i have to add an "F" at the end.
a similar problem happened with ints and longs.
can someone verify (And possibly add explanation) this?

I can certainly verify it.

Java has only two type of literals that use only numbers and dot: int and double (the latter is used if the literal contains a dot anywhere, so '2.' is a valid double literal). Any other types must be explicitly stated by using 'f' or 'L' (tip: don't use 'l', because it's too easy to mistake for a '1'), or by casting.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly don't edit your posts with the answer like that; it makes my post look like nonsense
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!