Search code examples
javadoublebigdecimal

Why is BigDecimal more precise than double?


I would like to know the exact difference between BigDecimal and double. I know that BigDecimal is more precise than double and you should use the former for calculations.

Why is it more precise? Why isn't it possible to customize double so that it works like BigDecimal? Or are there any advantages to calculating with double?


Solution

  • A double is a remarkably fast floating point data type implemented at a very low level on many chipsets.

    Its precision is sufficient for very many applications: e.g. measuring the distance of the sun to Pluto to the nearest centimetre!

    Always a performance trade-off when thinking about moving to a more precise data type as the latter will be much slower and your favourite mathematical libraries may not support them. Remember that the outputs of your program are a function of the quality of the inputs.

    As a final remark, never use a double to represent cash quantities though!