Why does Math.floor return a double?

前端 未结 6 2012
-上瘾入骨i
-上瘾入骨i 2020-12-13 23:05

Official Javadoc says that Math.floor() returns a double that is \"equal to a mathematical integer\", but then why shouldn\'t it return an in

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 23:17

    It's for precision. The double data-type has a 53 bit mantissa. Among other things that means that a double can represent all whole up to 2^53 without precision loss.

    If you store such a large number in an integer you will get an overflow. Integers only have 32 bits.

    Returning the integer as a double is the right thing to do here because it offers a much wider usefull number-range than a integer could.

提交回复
热议问题