• 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

Conversions

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reference : http://radio.javaranch.com/corey/2004/06/01/1086120368000.html


The order of numeric data types (from narrowest to widest), looks like this:

byte -> short/char -> int -> long -> float -> double

The previous explanation breaks down in two cases. Let me rewrite that progression but I'll replace the data types with their respective sizes, in bytes:

8 -> 16/16 -> 32 -> 64 -> 32 -> 64



Isn't double having the same range as that of long ? If so, then if conversion from float to long is "narrowing" then shouldn't the conversion from float to double also be "narrowing" ?

Arnab
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, I would like to point out that it should be bits instead of bytes. long are double are 64 bits, float 32 bits...

Secondly, I am sure that the range of double and long is different. for long, which is 64 bits signed integer. it represents integer values ranging from -2^63 to 2^63-1. However, for double, 64 bits are separated into 3 parts, a sign bit, 12 bit of exponent(with a hidden bit), and 52 bits for fractions. thus, it represents floating numbers in the form of (sign)(fraction)*2^(exponent). if you think long as a huge meter rule, then double would be a bigger rule, with more numbers concentrated near zero and less numbers at both ends.

Thirdly, for narrowing, i think there are two cases to consider. you shouldn't simply look at the number of bits of the primary types. converting from a long to a int represents one case, as the range is reduced. converting from float to int is another case, as int cannot represent float numbers exactly, say (int)12.56 would give you 12 as the fraction part is truncated.
[ August 11, 2005: Message edited by: Lucas Jiang ]
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic