• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advantage of BigInteger Class  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!