• 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

Long2Float

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
i was reading a previous post and i have the difficulty understanding why the first code prints true while the second prints false.There should be some loss of lower bits in first code also.
thanks in advance
CODE1:


CODE2:
class Long2Float {

public static void main(String[] args) {

long l = Long.MAX_VALUE - 1;

float f = l; // we expect to lose some of the lower bits here

long l2 = (long)f;

System.out.println(l == l2); //prints false

}

}
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as most people know, a long is 64 bits while a float is 32 bits. Yet, despite the size difference, a float can hold a much larger range of values than a long can. It does so by "approximating" values. That means that, while a long may hold an exact value, when converted to a float, you may be left with an approximation (something close, but not quite dead on) of that value.

In this case, it appears that float can exactly represent one value while it can only approximate the other.

Check this out, as well.
 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but Corey in code1 the value of l is greater than that in code2.So if f can hold (Long.MAX_VALUE) exactly then it can also hold (Long.MAX_VALUE - 1) exactly.So in both cases result should be true.Plz explain.
thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A long, at 64 bits can contain 2^64 distinct values. A float, on the other hand, has only 32 bits so it can contain only 2^32 distinct values. However, because float uses scientific notation to represent values, a float can handle a much larger range of numbers than a long. The downside is that not all of those numbers may be represented exactly.

Which number is greater is irrelevant. There are some numbers that can be represented by a long exactly and can only be approximated by a float. Those numbers are not contiguous nor are they found only above some threshold. Rather, those numbers are strewn out throughout the range of float.

I'm sure there's a great explanation for which numbers those are, but float is all based on base-2 scientific notation and, as I haven't done much math for a while now, I don't know that I really want to get into that.

For the SCJP exam, it will suffice to know that converting from a long to a float is a widening coversion yet some precision may be lost while doing so because of the nature of a float.
 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Corey now i got the point.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic