Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# multiplication using double type variables

Yogesh Bhate
Greenhorn
Posts: 1
Hello,
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)

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Hi, welcome to the ranch!

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!

Campbell Ritchie
Sheriff
Posts: 48445
56
Welcome to the Ranch.

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

Pat Farrell
Rancher
Posts: 4678
7
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

marc weber
Sheriff
Posts: 11343
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.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Thanks, that is easier to digest. I noticed i wrote "Numbers" instead of "Arithmetic" which is the actual title. That was one more work than I could remember for the time it took to type it. Sheesh. This stuff makes me miss COBOL which was designed for currency.

marc weber
Sheriff
Posts: 11343
That "heavy" article looks intimidating -- even to a math major. I've had it bookmarked for a long time, with the intention of digging into it some day. But on the other hand, it seems enough just to be aware of the problem (and simply avoiding it) without intimate knowledge of the gory details.

Campbell Ritchie
Sheriff
Posts: 48445
56
It is a really heavy article, but it covers all sorts of things other than precision in floating-point arithmetic.

marc weber
Sheriff
Posts: 11343
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.