Results 1 to 5 of 5
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default long and float conversion

    i am confused with conversion between long and float.
    long is 64 bit , but float only 32 bit, then how can we assign a long variable to float variable without error?

    how i got this code compiled?

    class LongToFloat
    {
    long a = 1234567890;
    float b = a;
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    Because a float is a floating point so rounds it off.
    Try it with 12345678912345L and print out both a and b.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jomypgeorge View Post
    i am confused with conversion between long and float.
    long is 64 bit , but float only 32 bit, then how can we assign a long variable to float variable without error?

    how i got this code compiled?

    class LongToFloat
    {
    long a = 1234567890;
    float b = a;
    }
    It is a bit strange indeed but the conversion from long to float is called a 'widening conversion' because longs can store (integral) numbers up to 1E18 while floating point numbers can be as large as 1E38; you do lose accuracy though because floats can only store 7 digits while a long can store up to 18 digits. But still you can implicitly cast longs to floats as you already have noticed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default Other cast problems...

    Just to add a couple of other examples of problematic casts that you should be aware of. Even casting between floating point and integer types of the same number of bits is problematic. Try this:

    Java Code:
      		long l = 0x123456712345678L;
      		double d = l;
      		long lAgain = (long) d;
      		System.out.println(Long.toHexString(lAgain));
    Another example of a cast to a type with a larger number of bits but where the cast doesn't necessarily lead to the same value is converting from a byte to a char: a byte is signed, but char is unsigned. So converting (byte) 255 to a char will actually give you a char with a value of 65535...

    It's arguable whether casts from integer to floating point types should have been allowed to be unchecked.
    Last edited by neilcoffey; 01-06-2011 at 05:01 PM. Reason: Extra point added

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

Similar Threads

  1. Error in float value
    By JMaste in forum New To Java
    Replies: 5
    Last Post: 07-11-2010, 05:08 PM
  2. string to float
    By Sungron in forum New To Java
    Replies: 3
    Last Post: 02-02-2010, 10:23 AM
  3. Working with float
    By dardar in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 09:29 PM
  4. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 05:52 AM
  5. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •