• 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 precision?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am doing extensive calculations in my project which involves the use of "double" variable type as it is big enough to hold its values. However I came across some bizare results! Take for example in the code below:

Now, I am expecting the total to be 8961.28, but the result I am getting is
"a: 4190.04 b:4771.24 total:8961.2799999999"
Surely a simple addition of two doubles shouldn't be required to be rounded everytime?
Is there a solution to this problem?
I am using jdk1.3.1b if that would be any help!
Thanks
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

double a=4190.04d;
double b=4771.24d;
double total =0d;
total=total+a+b=0d+4190.04d+4771.24d=8961.28d as you wish.But the result 8961.2799999999 is what
the double precision make sense!Double type always keep the precision fragment as long as it can.In other words,8961.28d equal to 8961.2799999999.

(Marilyn formatted code so it's not all on one line)
[ December 19, 2002: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a little more info: If you really want to see your results with, say two decimal places, just do the following
 
Darren Tweedale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry Marilyn, but I still can't understand why there should be a loss of 0.0000001 in the simple addition of two double values!
Thanks to norm, however, I need functionaility to either round up or down to so many decimal places (i use BigDecimal.setScale to do this) and store its proper values into the double varable) and I don't display the results.
Oh well, it looks like I have to change everything to long or float as they seemed to work!
Thanks for your inputs.
Bye
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see previous discussions like this one or search this forum for "double precision" for a list of other previous discussions on this topic.
Jamie
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem isn't with Java or any programming language in particular. Remember that all numbers are stored as binary in the computer. Many floating point numbers cannot be stored accurately, so there will always be round off errors when doing any kind of calculations with them (even addition).
To help you understand, a simple example may make it more clear. Take the fraction 1/3. In base 10 decimal, this can be written as .33333-repeating forever. However, most people get tired of writing 3's forever, so we settle for rounding off to a certain number of digits. Let's just say we take two digits after the decimal place. So we can add .33 + .33 + .33 = .99. However, adding the original fraction we get 1/3 + 1/3 + 1/3 = 3/3 = 1. During the calculation, we lost .01.
The same thing happens when decimals are converted to binary floating point numbers. The loss of precision happens during the conversion, not when the converted numbers are added.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java follows the IEEE specification which determines the behavior you are seeing.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Allow me to repeat myself today...)
Take a look at this past conversation in the Cattle Drive forum for two very good links on this topic (that are listed later in the conversation).
reply
    Bookmark Topic Watch Topic
  • New Topic