• 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

Rounding problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following portion of code is from the JLS (4.2.4 Floating point operations).

The solution is
inexact results with float: 0 41 47 55 61 82 83 94 97
Can someone please explain what is happening.....
Thanx in advance.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Basant,
Loss of precision!
For floating point we can only have 7 significant digits. If you divide the ones that are printing we get:
0.99999994
This gets truncated to:
0.9999999
Which will yeild the different result after multiplication.
If you were to use a double instead of a float the following would fail: 0, 49 and 98. This result is expected because for doubles we only get 16 significant digits.
We get:
0.9999999999999999n where n is something other than 0
Truncates to
0.9999999999999999
which yields incorrect results upon multiplication.
Regards,
Manfred.
 
Basant Sahu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manfred !!!
I tried printing all those intermediate values.
I can completely relate to what you are saying.
Thanks again....:-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic