• 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

Decimal place result

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,
some colleagues and i were writting a calculator program, but the result of some of our calculations were giving approximate answers and not the exact one.for example:


subtraction gave -0.100000024 instead of -0.1.

please can anyone help us with what to do to get the correct answer and may be decimal place
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
floats are inherently limited in their precision, and some numbers just can't be expressed precisely using them. "0.1" is such a number, as is "0.9". Using doubles makes things a bit better (around 14 exact decimals instead of 7 or so), but the basic problem is the same. (If you're interested in the gory details, read up on What Every Computer Scientist Should Know About Floating-Point Arithmetic.)

If you require exact floating point arithmetic, you can use the java.math.BigDecimal class.
[ August 09, 2005: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are well known problems with floating point arithmetic. You could try BigDecimal in the java.math package.
 
it's a teeny, tiny, wafer thin ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic