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?
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.
Go to the following link, it gives a good explanation bout widening/narrowing conversions, SCJP Tipline
Joined: Jun 10, 2005
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?
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?
A comprehensive link explaining everything you will ever need to know about floating point math is here.
Joined: Jun 10, 2005
But is my point right or wrong?
Joined: Dec 01, 2003
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.