aspose file tools*
The moose likes Beginning Java and the fly likes multiplication using double type variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiplication using double type variables" Watch "multiplication using double type variables" New topic
Author

multiplication using double type variables

Yogesh Bhate
Greenhorn

Joined: Oct 10, 2007
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

Joined: Jan 29, 2003
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!


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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Aug 31, 2004
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.


"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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
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

Joined: Aug 31, 2004
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

Joined: Oct 13, 2005
Posts: 39784
    
  28
It is a really heavy article, but it covers all sorts of things other than precision in floating-point arithmetic.
marc weber
Sheriff

Joined: Aug 31, 2004
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiplication using double type variables