# multiplication using double type variables

I am getting a wierd problem(atleast i think its wierd)

The program below prints the output as

Value = 20172.239999999998

I want to know why ?? Because the product of a*b should be 20172.24

public class Test

{

public static void main(String argsv[])

{

double a=1681.02;

double b= 12.0;

double c=a*b;

System.out.println("Value = " + c);

}

}

This is my java version.....

java version "1.4.2_08"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)

Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)

Ranch Hand

Floating types - float and double - are approximations of sorts and don't give the results we might expect for decimal work. See What Every Computer Scientist Should Know about Floating Point Numbers for all the gory details.

One short hint: Never use them for money amounts!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

The reason you don't get exactly the number is the same as why you can't write one-third exactly in decimal. Try it. Get a very sharp pencil and write and don't stop until you have got enough threes to give the exact answer.

No fractional arithmetic can be represented exactly in a 123.45 format unless the factors of the denominator of the fraction are all shared by the radix.

So in decimal 10 = 5 X 2, so it is possible to represent such fractions as � � ⅛ 1/5 1/10 1/20 1/25 exactly, not 1/3 or 1/7 or 1/11.

In the case of computers, they always use binary arithmetic, so the only fractions which can be accurately represented are those whose denominators have 2 as a factor, nothing else, so � � ⅛ 1/16 1/32 can be represented, not 1/3 1/5 etc. Any other fraction, which includes 0.02 (1/50) can only be approximated.

So every now and again you will notice this imprecision. Like 1681.02*12 ->201722.39999999998

There is an explanation on the Javaranch FAQs click here and look for no 20. That gives three links which might provide a better explanation than I did. CR

Originally posted by Stan James:

Hi, welcome to the ranch!

One short hint: Never use them for money amounts!

We really, really ought to have every beginning programming book, in any language have the above in bold.

**Never use floating point for currency**

You will solve huge numbers of avoidable errors and bugs if you follow Stan's advice

Originally posted by Stan James:

...See What Every Computer Scientist Should Know about Floating Point Numbers for all the gory details...

To help ease into these "gory details," here's a lighter version, Some Things You Should Know About Floating-Point Arithmetic.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Ranch Hand

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Originally posted by Campbell Ritchie:

It is a really heavy article, but it covers all sorts of things other than precision in floating-point arithmetic.

Okay, you talked me into it. I started it last night, and will return to it later today after I've had more coffee.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org