Java Why is converting a long (64) to float (32) considered widening?

前端 未结 4 1403
一个人的身影
一个人的身影 2021-01-31 22:08

As it states from oracle

Reference from Oracle Docs

Widening Primitive Conversion 19 specific conversions on primitive types are called

4条回答
  •  不思量自难忘°
    2021-01-31 22:47

    The range of values that can be represented by a float or double is much larger than the range that can be represented by a long. Although one might lose significant digits when converting from a long to a float, it is still a "widening" operation because the range is wider.

    From the Java Language Specification, §5.1.2:

    A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

    Note that a double can exactly represent every possible int value.

提交回复
热议问题