• 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

Assignment Java-7 (Sum)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody point me to an explaination of why 0.1D is really 0.099999999999998590 and 1000 * 0.1 becomes 99.9999999999986? It's hard to accept a 'law' without a reason behind it.

Thanks,
James McLeod
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because you cannot represent all decimal values exactly to 100% precision. most numbers are correct to (something like) eight significant digits. You can't actually store 0.1, but you can store 0.099999999999998590, which is usually close enough.

The reason (I think) is that java stores number by adding up powers of 2. so to get something close to 0.1, you store 1/16+ 1/32 + 1/128.... etc. (note these are not nec. the ACTUAL sum, I'm just trying to illustrate it ).

you can get pretty close most of the time, but not exact.

 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's actually not native to java, it's the very binary-ness (if I may invent words) of computers. The fact that they base everything off of 1s and 0s, means that they do their math in binary (base 2), which means that they have the same problems with dividing certain things by 10 as we do when we divide 10 by 3. In decimal based math that just doesn't compute very cleanly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic