my dog learned polymorphism
The moose likes Beginning Java and the fly likes double precision? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double precision?" Watch "double precision?" New topic

double precision?

Darren Tweedale

Joined: Jan 30, 2002
Posts: 16
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!
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124

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 ]

a beginner in java
Norm Miller
Ranch Hand

Joined: May 21, 2002
Posts: 56
Just a little more info: If you really want to see your results with, say two decimal places, just do the following
Darren Tweedale

Joined: Jan 30, 2002
Posts: 16
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.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

see previous discussions like this one or search this forum for "double precision" for a list of other previous discussions on this topic.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Java API Documentation
The Java Tutorial
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Java follows the IEEE specification which determines the behavior you are seeing.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
(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).

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: double precision?
It's not a secret anymore!