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

4

17

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?

Britt answered 5/3, 2013 at 16:40 Comment(5)
i think its related with precision. I can only think about that :)Wormeaten
I didn't understand your question : If a long has 32 bits and a long has 64 how is that considered widening? Shouldn't this be considered narrowing? The long data type is a 64-bit signed two's complement integer in java. please refer to the following link : docs.oracle.com/javase/tutorial/java/nutsandbolts/…Getup
You said "If a long has 32 bits and a long has 64"... make your mind upPorche
Sorry I had a typo. See the revised questionBritt
Most valuable question. Indirectly I got a solution for a long running issue. Thank youJaws
A
27

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.

Airflow answered 5/3, 2013 at 16:41 Comment(4)
@Mrshll187 - Actually, a float is 32 bits, of which 8 are for the exponent. There are only 24 bits available to represent the mantissa. I don't know what "I can be up to 64 bits" would mean. A float can represent some of the numbers in the range 1.40129846432481707e-45 to 3.40282346638528860e+38 (as well as a few special values like NaN and ±infinity). A long can represent all numbers in the range -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. You can think of each type name as the name of a subset of numbers. The subsets overlap, but each subset has elements not in the other.Airflow
Thank You Ted! I was having a very hard time grasping this as I was thinking in terms of bits.Zechariah
Why do we consider it subtyping if not every value of long can be represented as a float?Sabayon
@Sabayon - I'm not aware that we do consider it subtyping. (By "it" I assume you are referring to long with respect to float.) As I pointed out in my answer, the set of float values does not subsume the set of long values. (That is, there are long values that are not float values -- at least not exactly--and vice versa.) This pretty much rules out long as a subtype of float by every definition of the term I know.Airflow
P
5

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.

Polyzoarium answered 5/3, 2013 at 16:42 Comment(0)
V
3

It's considered widening because float and double can represent larger values than long. You may lose precision, but it will be possible to represent the value (at least approximately).

Vermont answered 5/3, 2013 at 16:41 Comment(0)
R
2

If you look at the matter in simple terms, it is about how data has been represented by original designers.

ideally bit depth of long(64) is larger than float(32). But float data has represented using scientific notion which allows to represent considerably much larger range

Ex: 300[original number] : 3×102 [scientific representation]


Long : -2^63 to 2^63-1

Float : (-3.4)*10^38 to (3.4)*10^38

Notice the Long(power of two) Vs Float(power of ten) representational difference here which allow float to have higher range

hope this is helpful

Ravi answered 3/4, 2018 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.