• 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 conversions

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Khalid Mughal as well as RHE it is said that,
int -> long -> float
int is 32 bits, long is 64 bits and float is 32 bits
How can long (64 bits) be implicitly (widely) converted to float
Any help is greatly appreciated.
-Jay
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay,
Here is some information paraphrased from the JLS.
Conversion of an int (or a long) to a float may result in a loss of precision. As you noted, some of the least significant bits will be discarded. This is following the IEEE 754 round to nearest standards--which means that the resulting floating point will be rounded to the integer value.
It basically means that this is the way that they decided to do it-and that you need to know what it means (a possible loss of precision).
Cheers.
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hence, implicit conversion from long (64) to int (32) is a narrowing conversion Thanks.
-Jay
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,
<code>
The compiler will implicitly convert integer type to float and double type, even though some precision may be lost, like: float f = Long.MAX_VALUE;
</code>
Rgds,
Guoqiao

Originally posted by Jay Kay:
Hence, implicit conversion from long (64) to int (32) is a narrowing conversion Thanks.
-Jay


 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to add the considerations regarding

'E' in floating in floating point numbers


Would anyone like how 'E' works with floating point numbers
and in some codes comparing two floats after some calculations will return false...

------------------
azaman
 
reply
    Bookmark Topic Watch Topic
  • New Topic