| Author |
Widening
|
donal horgan
Ranch Hand
Joined: Mar 11, 2005
Posts: 54
|
|
|
Can someone please explain to me why going from a long(64bits) to a float(32 bits) is considered widening?
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
In short (no pun intended), the approximate range of a long is plus or minus 9.2 X 10^18, but the approximate range of a float is plus or minus 3.4 x 10^38. Note the magnitudes here: Ten to the 18th for a long, compared to ten to the 38th for a float. In other words, the 32-bit float has a vastly greater range than the 64-bit long. The reason is that floating-point primitives (Java's float and double) sacrifice precision for range. See this thread for detailed explanations: http://www.coderanch.com/t/247234/java-programmer-SCJP/certification/method-parameter And see this site for more details on IEEE 754 standards: http://www.public.iastate.edu/~sarita/ieee754/homepage.html [ March 18, 2005: Message edited by: marc weber ]
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
donal horgan
Ranch Hand
Joined: Mar 11, 2005
Posts: 54
|
|
Cheers for the link. Now i get it.
|
 |
Krishnakumar
Greenhorn
Joined: Mar 15, 2005
Posts: 22
|
|
Hi, Thanks a lot for the posting...But why is the code below throwing a compiler error..? It says integer is too long. But a suffix of l or L solves the problem. float f = (long)9223372036854775807; I got a bit confused because to convert a double leteral into a float you cd either suffix it with a f or F or cast it with a (float)<double literal> Thanks!
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
From the Java Language Spcifications:
The largest decimal literal of type int is 2147483648 (231). All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear, but the literal 2147483648 may appear only as the operand of the unary negation operator -.
The expression "(long)9223372036854775807" says to create an int with the value 9223372036854775807 and then cast it to type long. But 9223372036854775807 is greater than 2147483647, so it is not a legal int literal. This will work because there is no attempt to create an int first: float f = 9223372036854775807L; [ March 21, 2005: Message edited by: Mike Gershman ]
|
Mike Gershman
SCJP 1.4, SCWCD in process
|
 |
Krishnakumar
Greenhorn
Joined: Mar 15, 2005
Posts: 22
|
|
|
Thanks a lot Mike!!
|
 |
 |
|
|
subject: Widening
|
|
|