If I have to design, say, a financial application that uses rational numbers (i.e., numbers with a fractional component), what datatype should I use for internally doing calculations?
I have two options:
Use
BigInt
(BigInteger
in Java) and emulate fixed point arithmetic. I have to keep track of the decimal point separately.Use
BigDecimal
and floating point arithmetic.BigDecimal
is already a wrapper over BigInt that does most of what I need to do for 1.
I am inclined to use BigDecimal (approach 2) unless someone can give strong arguments for integer arithmetic.
EDIT: In relation to the linked question, I want to know which out of BigDecimal or BigInteger is better, while the other question is more generic and talks about the "best datatype" (which may or may not be out of the two above). In my case, my choice is limited to the two so I cannot use Currency
type.