• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

long ----- float

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
  • long l = 1L;
  • float f = 1.0F;
  • //insert code here


  • f = l; //compiles fine
    l = f; //gives error
    I don't understand it, because long is 64-bit and float is 32-bit
    Thx,
     
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's not a matter of bits; it's a matter of precision. Longs can only hold integral values. Floats can hold fractional values. If your float variable contained the value 12.34, what happens to the 0.34? It gets truncated. The compiler will flag this for you as a loss of precision.
     
    Ranch Hand
    Posts: 3271
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    We often refer to things as widening or narrowing conversions. A widening conversion is a conversion that is "safe" - there can be no loss of data in a widening conversion. A narrowing conversion, however, is not safe. You can lose data in a narrowing conversion.
    To consider something a widening conversion, it must pass one test - all values representable by the first data type must be representable by the second. If that is possible, it is considered a widening conversion.
    Based on that, it's easier to see that a conversion is considered a widening or narrowing conversion based on the ranges of the data types, not the number of bytes those data types contain. If the range of the original data type is a subset of the range of the conversion data type, the conversion is considered to be widening. If not, the conversion is a narrowing conversion.
    As every value representable by a long is also representable by a float, converting a long to a float is a widening conversion. As every float, however, is not representable by a long, converting from a float to a long is considered a narrowing conversion.
    I hope that helps,
    Corey
     
    There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic