BigDecimal Underflow

拟墨画扇 提交于 2019-12-04 14:59:33

While BigDecimal is much more powerful and flexible than double it does still have limits; namely its scale is an int:

A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.

This means that you cannot represent numbers larger or smaller than are scaled by a factor of more than 2^31. This is a massive (or minuscule) number (10^2^31 being the largest possible multiplier), and for almost any possible use case it's an impractical edge case to run into. By comparison there are "only" approximately 4×10^80 atoms in the universe.

So what does it mean if you're running into Overflow or Underflow errors? The scale of the numbers you're working with are so ludicrously large or small that BigDecimal can't support them. This all-but-certainly means you've made a logic error of some sort, and are aren't doing the operations you intended to - double-check your math.

Occasionally the problem is the order of operations - e.g. your result might be a reasonably sized number, but the intermediary steps are unworkable. Computing the binomial coefficient is an example of this. In such cases you need to experiment with other orders of operation that avoid such unreasonable numbers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!