DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: unexpected results summing double type values

1. Lynn Miller Guest

## unexpected results summing double type values

I am stumped about something and I hope someone can help me. I have been
using Java for almost one year and can't figure this one out.

I am using Microsoft Visual J++ 6.0 with SP3 and the latest VM.

If the following code is run, the results will tell me that they do NOT equal:

double sumofDebits = 0;
double sumofCredits = 0;

sumofDebits += 5.54;
sumofDebits += 95.01;
sumofCredits += 100.55;

if (sumofDebits == sumofCredits)
Win32.MessageBox(0,"Equal","Title",0);
else
Win32.MessageBox(0,"Not Equal","Title",0);

However, if I change the values from 5.54 and 95.01 to 5.53 and 95.02, respectively,
it will say they equal (which it should)

Any ideas?

Thanks,

Lynn Miller

2. Tom Duffy Guest

## Re: unexpected results summing double type values

Hello Lynn:

This problem is encountered frequently when doing floating point math - the
numbers get rounded in sometimes unexpected ways. One workaround is to subtract
the two numbers in question and then compare their difference to a suitably
small number. If the difference is smaller than say 0.000001, treat the
numbers as equal. If it is larger, treat the numbers as unequal.

HTH,

Tom

"Lynn Miller" <lynn@interactivefs.com> wrote:
>
>I am stumped about something and I hope someone can help me. I have been
>using Java for almost one year and can't figure this one out.
>
>I am using Microsoft Visual J++ 6.0 with SP3 and the latest VM.
>
>If the following code is run, the results will tell me that they do NOT

equal:
>
>double sumofDebits = 0;
>double sumofCredits = 0;
>
>sumofDebits += 5.54;
>sumofDebits += 95.01;
>sumofCredits += 100.55;
>
>if (sumofDebits == sumofCredits)
> Win32.MessageBox(0,"Equal","Title",0);
>else
> Win32.MessageBox(0,"Not Equal","Title",0);
>
>However, if I change the values from 5.54 and 95.01 to 5.53 and 95.02, respectively,
>it will say they equal (which it should)
>
>Any ideas?
>
>Thanks,
>
>Lynn Miller
>
>

3. Lynn Miller Guest

## Re: unexpected results summing double type values

"Tom Duffy" <td4729@hotmail.com> wrote:
>
>Hello Lynn:
>
>This problem is encountered frequently when doing floating point math -

the
>numbers get rounded in sometimes unexpected ways. One workaround is to

subtract
>the two numbers in question and then compare their difference to a suitably
>small number. If the difference is smaller than say 0.000001, treat the
>numbers as equal. If it is larger, treat the numbers as unequal.
>
>HTH,
>
>Tom
>
>"Lynn Miller" <lynn@interactivefs.com> wrote:
>>
>>I am stumped about something and I hope someone can help me. I have been
>>using Java for almost one year and can't figure this one out.
>>
>>I am using Microsoft Visual J++ 6.0 with SP3 and the latest VM.
>>
>>If the following code is run, the results will tell me that they do NOT

>equal:
>>
>>double sumofDebits = 0;
>>double sumofCredits = 0;
>>
>>sumofDebits += 5.54;
>>sumofDebits += 95.01;
>>sumofCredits += 100.55;
>>
>>if (sumofDebits == sumofCredits)
>> Win32.MessageBox(0,"Equal","Title",0);
>>else
>> Win32.MessageBox(0,"Not Equal","Title",0);
>>
>>However, if I change the values from 5.54 and 95.01 to 5.53 and 95.02,

respectively,
>>it will say they equal (which it should)
>>
>>Any ideas?
>>
>>Thanks,
>>
>>Lynn Miller
>>
>>

>

Tom:

Thank you very much for your quick response! The workaround you suggested
will suit my needs. Coming from a VB/Visual Foxpro background, it just didn't
make sense what was going on.

Thanks again.

Lynn Miller

4. Karnos Guest

## Re: unexpected results summing double type values

Because doubles are 64-bit, they have a minimum number of significant figures
(I think 15), so the actual number Java is working with is 100.55000000000001
after the math. The quickest way to fix this is to use floats instead of
doubles. Since floats have a lower precision, they maintain a more accurate
reference to the lower mathematical values. Alternatively, you could throw
in some error into your calculation, by requiring the two numbers to be equal
to within a thousandth, instead of the trillionths.

"Lynn Miller" <lynn@interactivefs.com> wrote:
>
>I am stumped about something and I hope someone can help me. I have been
>using Java for almost one year and can't figure this one out.
>
>I am using Microsoft Visual J++ 6.0 with SP3 and the latest VM.
>
>If the following code is run, the results will tell me that they do NOT

equal:
>
>double sumofDebits = 0;
>double sumofCredits = 0;
>
>sumofDebits += 5.54;
>sumofDebits += 95.01;
>sumofCredits += 100.55;
>
>if (sumofDebits == sumofCredits)
> Win32.MessageBox(0,"Equal","Title",0);
>else
> Win32.MessageBox(0,"Not Equal","Title",0);
>
>However, if I change the values from 5.54 and 95.01 to 5.53 and 95.02, respectively,
>it will say they equal (which it should)
>
>Any ideas?
>
>Thanks,
>
>Lynn Miller
>
>

5. mike m Guest

## Re: unexpected results summing double type values

to perform precise floating-point arithmetic you should look into using the
BigDecimal class in the math package.

"Lynn Miller" <lynn@interactivefs.com> wrote:
>
>I am stumped about something and I hope someone can help me. I have been
>using Java for almost one year and can't figure this one out.
>
>I am using Microsoft Visual J++ 6.0 with SP3 and the latest VM.
>
>If the following code is run, the results will tell me that they do NOT

equal:
>
>double sumofDebits = 0;
>double sumofCredits = 0;
>
>sumofDebits += 5.54;
>sumofDebits += 95.01;
>sumofCredits += 100.55;
>
>if (sumofDebits == sumofCredits)
> Win32.MessageBox(0,"Equal","Title",0);
>else
> Win32.MessageBox(0,"Not Equal","Title",0);
>
>However, if I change the values from 5.54 and 95.01 to 5.53 and 95.02, respectively,
>it will say they equal (which it should)
>
>Any ideas?
>
>Thanks,
>
>Lynn Miller
>
>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise