Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As it states from oracle

Reference from Oracle Docs

Widening Primitive Conversion 19 specific conversions on primitive types are called the widening primitive conversions:

  1. byte to short, int, long, float, or double
  2. short to int, long, float, or double
  3. char to int, long, float, or double
  4. int to long, float, or double
  5. long to float or double?
  6. float to double

If a float has 32 bits and a long has 64 how is that considered widening? Shouldn't this be considered narrowing?

like image 744
stackoverflow Avatar asked Mar 05 '13 16:03

stackoverflow


People also ask

What is a widening conversion in Java?

Widening conversion takes place when two data types are automatically converted. This happens when: The two data types are compatible. When we assign value of a smaller data type to a bigger data type.

What is widening primitive conversion?

A widening primitive conversion does not lose information about the overall magnitude of a numeric value in the following cases, where the numeric value is preserved exactly: from an integral type to another integral type. from byte , short , or char to a floating point type. from int to double.

Can long be converted to float?

long to float is a potential loss of precision but not magnitude because the value range for floats is larger than that for longs. So the rule is: Loss of magnitude: explicit cast required; Loss of precision: no cast required.

Is float bigger than long in Java?

Well, using scientific notation, we can represent a far wider range of values using fewer bits than if we had not used scientific notation. The largest number a long can hold is 263 - 1. Meanwhile, a float, which is 32 bits shorter than a long, can hold up to (2-2-23)�2127.


2 Answers

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.

like image 187
Ted Hopp Avatar answered Sep 18 '22 06:09

Ted Hopp


It is considered widening because the numbers that can be represented by a float is larger than numbers that can represented by long. Just because float uses 32 bit precision does not mean the numbers it can represent are limited to 2^32.

For instance the float (float)Long.MAX_VALUE+(float)Long.MAX_VALUE is larger than Long.MAX_VALUE, even though the float has less precision that the long.

like image 30
RudolphEst Avatar answered Sep 18 '22 06:09

RudolphEst