• 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

Casting of long to float

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the casting of long to float implicit. I would think that because long is 64 bits and float 32 bits, an explicit cast would be required. But that is not the case...any ideas??
 
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


This question is actually commonly asked on this forum -- you should search for previous topics.

Henry
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can read the following explanation:

A widening conversion from a long to a float value or from a long to a double value (which both have 64 bits) may result in a "loss of precision". The floating point in the target type is then a correctly rounded approximation of the integer value. Note that precision is the number of significant bits in the value and most not be confused with magnitude which relates how big a value can be represented.

Not make sense? Here is an example:

long bigInt = 98765432112345678L;
float fp = bigInt; //Widening but loss of precision: "fp= 9.8765436E16"

You should know that float and double can store much bigger values than for example long. You can prove it to yourself by printing the Float.MAX_VALUE or Double.MAX_VALUE. So they have bigger magnitude and smaller precision than long

Basically we use the term narrowing (which needs an explicit cast) whenever there is a possibility in loss of magnitude information and not solely loss precision. e.g: conversion from long to int.

Hope this helped...

 
ani pillai
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it...went through this article http://radio.javaranch.com/corey/2004/06/01/1086120368000.html

Thanks for your help
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic