• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Can all int values fit in float variable?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic