Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About casting(How it is possible?)

 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends;

I have 1 doubt.
In the widening conversions of numeric data types it is perfectly legal to assign a char to an int because the size of an int is greater than that of char. But I have a doubt in conversion of a long to a float.The size of a long is greater than that of float (long=64bits & float=32bits). Then how JVM can implecitly convert a numeric value long to a float? why it is legal to assign a long to a float?

bye.
shubha
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shubha, your displayed name does not conform to the JavaRanch Naming Policy.

Please, we require two names in the format: <first name><space><family name>, preferably your real first name and family name.

Thanks,
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at Roedy Green's Primitives Table, you will see that the maximum value of a long falls within the range of values that a float can hold. But this does not mean that precision (accuracy) is preserved. The upward conversion only considers the compatibility of the ranges.
 
shetal bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to the following link, it gives a good explanation bout widening/narrowing conversions,
SCJP Tipline
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shetal;

I saw the site that u gave.It was really informative.But still not understood the following statements given in that artical.

Floating point numbers are the scientific notation of the computer world. With a float, only a small number of the bits represent the number while another bunch of bits represents the exponent to which this number should be raised - it's effectively base 2 scientific notation.

can any one please help me to understand this point?

Bye
Take care.

Shubha.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi shubha...

this link

will be bit helpfull...

regards,
amit
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have 1 doubt.

The MAX_VALUE and MIN_VALUE for a float is larger and
smaller than the MAX_VALUE and MIN_VALUE for a long respectively.
We can assign long value to the float(ya...some loss of precision may happen).
This much is okay.But the size of float is less than that of long.
This means there should something supporting to the float to store its values.
If yes what is that?

I think i m right.
m i???

Bye.
shubha.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A comprehensive link explaining everything you will ever need to know about floating point math is here.
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But is my point right or wrong?

shubha. :roll:
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, to grossly oversimplify, let's look at base 10 for a minute. With 9 decimal digits, you can represent the range of -999999999 to 999999999 (assuming the minus sign is free). Now, if precision was not so critical, you could estimate much larger numbers with fewer decimal digits, as long as you follow the right convention. Let's just assume that we want to have 3 digits representing the number with an assumed decimal point after the first, and 3 more to represent the exponent. Then you could estimate 999999999 as being 100009. There you have it, in 6 digits we can represent approximately the same number

With this convention, you can represent anywhere from -999999 to 999999 (for numbers very close to zero, we would need another sign bit) which represents -9.99 x 10^999 to 9.99 x 10^999. This is a large range, but doesn't have a lot of precision. You can't represent, as an example, 1005 within 5 units of its true value - i.e. 101003. This can be solved by shifting the decimal point to give the largest range possible with the needed precision.

So to answer your question, there is nothing but the float saving the digits of your number, it just does it with fewer bits and more uncertainty.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic