saikrishna cinux

Ranch Hand

Posts: 689

posted 11 years ago

hey we 've got all the functionalities of mathematic calculations in Math class then what is the necessity of MATH package's BigInteger,BigDecimal.???

please can any one explain me

thanx in advance..

cinux

please can any one explain me

thanx in advance..

cinux

A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE

SCJP 1.4

Keith Lynn

Ranch Hand

Posts: 2409

posted 11 years ago

The problem is that the methods in the Math class work with ints and doubles. Those types can hold only finite numbers. The BigInteger and BigDecimal classes can hold arbitrarily large numbers.

Another advantage of BigDecimal is that because of the problem of representing decimal numbers in binary, doubles are not appropriate for financial programs. But BigDecimal gives you accuracy.

Another advantage of BigDecimal is that because of the problem of representing decimal numbers in binary, doubles are not appropriate for financial programs. But BigDecimal gives you accuracy.

Peter Chase

Ranch Hand

Posts: 1970

posted 11 years ago

Integer and floating point types both can only store numbers over a particular range. Also, while floating point types superficially can store any real number, they actually have limited precision and cannot represent every real number exactly.

Floating point types can store representations of infinity, too, though you rarely find an instance when you can use these.

I agree that floating point is totally inappropriate for storing sums of money, because of their limited precision. It's a common newbie mistake, but it's 100% wrong.

However, you don't need to resort to BigDecimal for most financial applications. You can use integer arithmetic, working in the smallest unit of the currency (e.g. cents, pence...). A Java int will cope with any amount of money encountered in normal life. However, if you are dealing with high finance, you might want to use a Java long, just to be sure. Whichever you choose, it will run much faster than BigDecimal.

Originally posted by Keith Lynn:

The problem is that the methods in the Math class work with ints and doubles. Those types can hold only finite numbers. The BigInteger and BigDecimal classes can hold arbitrarily large numbers.

Integer and floating point types both can only store numbers over a particular range. Also, while floating point types superficially can store any real number, they actually have limited precision and cannot represent every real number exactly.

Floating point types can store representations of infinity, too, though you rarely find an instance when you can use these.

Another advantage of BigDecimal is that because of the problem of representing decimal numbers in binary, doubles are not appropriate for financial programs. But BigDecimal gives you accuracy.

I agree that floating point is totally inappropriate for storing sums of money, because of their limited precision. It's a common newbie mistake, but it's 100% wrong.

However, you don't need to resort to BigDecimal for most financial applications. You can use integer arithmetic, working in the smallest unit of the currency (e.g. cents, pence...). A Java int will cope with any amount of money encountered in normal life. However, if you are dealing with high finance, you might want to use a Java long, just to be sure. Whichever you choose, it will run much faster than BigDecimal.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

It is sorta covered in the JavaRanch Style Guide. |