Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can all int values fit in float variable?  RSS feed

 
Vector Sanasam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read somewhere that not all int values can fit inside float variables. So i wrote a program, but the compiler allows to assign an int variable to a float variable. I thought it was not gonna compile.
What i am thinking is the compiler does not know which value is in the int variable, so i thought it was gonna fail. But it compiles and runs fine. Please explain.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you think was going to happen? Of course the compiler knows which variable is an int and which is a float.

Try again using n = Integer.MAX_VALUE to see what happens.

Bill
 
Vector Sanasam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not what i meant, if all int can't fit in float variable, how is it that float f=n compiles?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector Sanasam wrote:I have read somewhere that not all int values can fit inside float variables. So i wrote a program, but the compiler allows to assign an int variable to a float variable. I thought it was not gonna compile.
What i am thinking is the compiler does not know which value is in the int variable, so i thought it was gonna fail. But it compiles and runs fine. Please explain.


Java only uses the range to determine whether implicit casting is allowed. And since a float type has a range that completely overlaps the range of an int type, the implicit cast is allowed. Hence, your test program should compile.


On the other hand, that doesn't mean that you won't lose precision. There are definitely some values of int, that will have to be rounded, as it is converted to a float. So, wherever you "read somewhere" is, it is kinda correct ... there are values of the int type that won't exactly "fit" into a float.

Henry
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector Sanasam wrote:I have read somewhere that not all int values can fit inside float variables.

Then what you read is wrong. All int values will "fit" in (ie, can be converted to) a float.

However, not all int values will produce distinct float values - or, to put it another way, not all int values will produce the same value value when converted to a float and then converted back.

And this is because of the precision loss that Henry mentioned.

A float can take any value up to ≈ ±16.7 million without losing precision though. For full details, check out this page, although some of it is a bit technical.

HIH

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!