The BigDecimal
class has a number of nonintuitive behaviors in Scala 2.10. This will get better in 2.11, but I can't quite tell from your example whether it will fix what you want. Probably not; Scala has a default MathContext
which keeps about 128 bits of information (~34 decimal digits), and I think that's what you're running into here.
If you don't have a decimal problem--and here you don't--then the easiest thing to do is just use BigInt
instead. It will scale to however many digits you actually need.
If you must express this as a decimal problem, you should explicitly supply a MathContext
that has enough digits:
if (c==1) BigDecimal.apply(begin, new java.math.MathContext(60))
and that MathContext
will, if always used on the left-hand side of operations, propagate through to your result.