• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assignment  RSS feed

 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, check the following code

float f;
long l = 100L;
f = l;

it doesn't give a compile error how is it possible.
float -- 32 bits and long -- 64 bits

pls. explain
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesn't give a compile error how is it possible.
float -- 32 bits and long -- 64 bits

pls. explain


Float numbers have a larger range -- meaning all long numbers fit into a float number. In Java, casts from long to float can be implicit.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention... because of the fact that floats are only 32 bits, it has pretty poor precision. you should see "rounding" for many longs when they are casted to float.

Henry
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou sir,

won't the size cause an issue (64(long) -> 32(float)) during runtime
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whole numbers and decimal numbers are stored differently.
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... :-)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!