• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Widening

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain to me why going from a long(64bits) to a float(32 bits) is considered widening?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
https://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 ]
 
donal horgan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers for the link.
Now i get it.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Krishnakumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mike!!
reply
    Bookmark Topic Watch Topic
  • New Topic