aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About  casting(How it is possible?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About  casting(How it is possible?)" Watch "About  casting(How it is possible?)" New topic
Author

About casting(How it is possible?)

Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
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


Shubhada
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: May 09, 2005
Posts: 63
Go to the following link, it gives a good explanation bout widening/narrowing conversions,
SCJP Tipline
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
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

Joined: Mar 14, 2003
Posts: 810
hi shubha...

this link

will be bit helpfull...

regards,
amit


Thanks and Regards, Amit Taneja
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
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

Joined: Dec 01, 2003
Posts: 226
A comprehensive link explaining everything you will ever need to know about floating point math is here.
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
But is my point right or wrong?

shubha. :roll:
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
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.
 
Consider Paul's rocket mass heater.
 
subject: About casting(How it is possible?)