Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Double addition gives wrong result?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This line of code does not work as expected:

Obviously, i expect to get 4.82 as a result, but i get 4.819999999999999.

Why?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use java.math.BigDecimal if precision is important to you.
 
M Bryan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should i save numbers like 481 and devide them by 100 if a print them or can i save them like 4.81 but use bigdecimal instead of double?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all these numbers are currency values, you should use BigDecimal witch scale=2.

ETA: Money In Java
 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It really depends on what you're trying to achieve. For instance, if you still need 4.81 in various other calculations and with the same high precision, then I would store it in a BigDecimal.

Btw, the reason why you lose precision in number formats such as Double, is because there is only a limited amount of number combinations that can be stored within the 64 bit constraints of a Double. As you know, there is an unlimited amount of numbers in between each range of numbers, for instance between 1 and 2 you can have 1.1, 1.11, 1.111, 1.00000001, etc... But since 64 bits cannot hold each and every possible number, it gets "rounded" to the nearest possible value, thereby causing some loss in precision. This is a limit that originates from the size of CPU and/or FPU registers, so basically hardware limits.
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic