• 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

Primitive Casting

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Why does the following code compile without error? Here assign logn variable to float variable without explicitly cast to float.


[ April 03, 2006: Message edited by: Nuwan Priyadarshana ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with range. A float has a larger range than long, so a long will technically fit into a float.

Now, of course, since a float is only 32 bits, some data will be lost. In this case, you will lose precision.

Henry
 
Nuwan Priyadarshana
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Henry,

float is 32 and long is 64 bit,here why compiler give the error message ?


[ April 03, 2006: Message edited by: Nuwan Priyadarshana ]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a int is only 32 bits,
a short is only 16 bits
 
Nuwan Priyadarshana
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wang,

If it's give a compiler error, Why this compile fine ?

 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/qb]<hr></blockquote>
you should know that


The largest decimal literal of type long is 9223372036854775808L( 2 ~63).
but The largest positive finite float literal is 3.4028235e38f.
notice the range,so we an assign long to float ,Conversion of an int or a long value to float, or of a long value to double,
may result in loss of precision�that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode .

The largest decimal literal of type short is 2 ~15 -1.
so we can not assign int to short implicitly


[ April 03, 2006: Message edited by: Changchun Wang ]
 
Nuwan Priyadarshana
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wang,

Thank you Wang, I have solved the confusion, and your last reply really helps to solve the confusion. Thanks again.
 
There's a way to do it better - find it. -Edison. A better 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