# double precision?

Darren Tweedale

Greenhorn

Posts: 16

posted 13 years ago

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

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

Mellihoney Michael

Ranch Hand

Posts: 124

posted 13 years ago

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 ]

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

Posts: 56

Darren Tweedale

Greenhorn

Posts: 16

posted 13 years ago

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

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

posted 13 years ago

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

Jamie

Jamie

Layne Lund

Ranch Hand

Posts: 3061

posted 13 years ago

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.

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.

Thomas Paul

mister krabs

Ranch Hand

Ranch Hand

Posts: 13974

posted 13 years ago

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

Sheriff

Posts: 7023

posted 13 years ago

(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).

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).