• 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

float behavior

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I aplogize if this topic has come up a lot. I understand that an int is supposedly more precise than a float but I still don't understand something about this output:

An explanation to a question related to this mentioned that a float is only precise to 6 digits. Where does the '4' come from, though, in this output when the float is first printed:
1.2345674E9 //7 '4'? E9
Can someone help me understand this whole float precision vs and int stuff. I also don't understand how a long could be 64 bits and yet it's range is less than that of a float? Is this maybe because a long uses the extra bits to be more precise than a float?
Thanks for any help.
Rick
[ February 04, 2002: Message edited by: Rick Reumann ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The float and double primitives use some of their bits to represent an exponent, therefore they have fewer bits available to represent a value. However, with an exponent they can represent a huge range of magnitudes, completely out side the range int or long can represent.
Integer representations are always precise since a bit is either there or it isn't - float and double have to try to represent values that have no exact binary expression.
Bill
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill I appreciate the response. That makes a bit more sense now. But I still don't get why there is a 4 out there. If the float is only precise to 6 digits wouldn't it behave as rounding off the last digit it could be precise to and then providing the exponent?
Why wouldn't the float representation of
int i = 1234567890 be
1.234567E9 or if rounded maybe
1.234568E9 ?
I don't get how the four is thrown in the representation 1.2345674E9 ?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick
For some of the nitty-gritty details check out the JLS section 5.1.2 This will make frequent reference to the IEEE 754 standard - which I can't find openly published on line but if you want a good article on it check out Suns numerical computation guide.
As you can see it is pretty intense - at least for me it is
The upshot of it is (at least form almost everyday programming) that you can lose precision with some conversions (both widening and narrowing). It is most important to know that it does happen and why, but the actual math involved is usually not required learning - unless you're actually creating a compiler or high precision program of some sort. If that is the case you can always try to find one that supports float-extended-exponent value set.
hope that helps
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There you go:
IEEE 754 (Postscript file)
How JAVA's Floating-Point Hurts Everyone Everywhere (PDF file)
Other information:
http://cch.loria.fr/documentation/IEEE754
HIH
[ February 04, 2002: Message edited by: Valentin Crettaz ]
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm sorry I asked The info in those links is WAY over my puny head. I was thinking it was going to be something simple..like "that's because the bits outside of the precision range have this X sort of behavior." Oh well, I'll just be content to know that precision can be lost when going from a float to an int. Seems so counter-intuitive since you'd think the datatype that could hold a larger number could be more precise. Thanks for the links.
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic