• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float requires 4 byte and long requires 8 bytes, and we can assign long variable to float variable. how it is possible?
 
Marshal
Posts: 80068
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using floats at all?

The reason you can assign it is that it counts as a widening primitive conversion, which the language permits implicitly. Even though there is likely to be loss of precision. I think widening may refer to the range rather than the precision; every valid long value can be mapped to a float value, even though several longs can map to the same float.
If you try it the other way round, there are float values which are completely outwith the range of longs, so that counts as a narrowing conversion and you need an explicit cast. Anyway, the JLS link I gave you is quite clear.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when using java, there is no specification as to how many bits (bytes) a type utilizes.

Two majors questions:

Why do you think this is important?

Where did you git for perception?

-steve
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Fahlbusch wrote:when using java, there is no specification as to how many bits (bytes) a type utilizes.





JLS 4.2 Primitive Types and Values:
The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units


JLS 4.2.3 Floating Point Types, Formats, and Values:
The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).



Although the physical size in a given JVM implementation may be larger than the sizes mentioned above, the various types must occupy at least as many bits or bytes as indicated. More to the point, though, the types must behave as though they were of those sizes, and that directly drives the OP's question.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
McDonalds used to have signs in front of many of their stores, telling how may people they has served. I remember when they use to say something like "350 million served". The number kept going up, and eventually it was changed to "2 billion served", or whatever.

They went from a 3-digit number (350) to a 1-digit number (2) - because there is another part that tells you the magnitude. Some accuracy has been lost, but you could change the 350 into a 1-digit number by saying ".3 billion". or if you'd prefer... "3 onehundredmillion".

That's more or less how floating point numbers work in just about any computer language. You have the "value" part, and the "magnitude" part. When you convert from on to the other, some accuracy is lost, but it is often possible.

Further, not every number can be stored. Just like on the McDonald's sign, they could store 350 million, then 351 million...missing 999,999 number in between. When they converted to billions, they could go from 2 billion to 3 billion, missing quite a few numbers in between.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic