Why can float (floating point type) directly receive long (integer type) in Java

The most common sense, we all know that the floating point type occupies 4 bytes of space in memory, while the long type occupies 8 bytes of space. But why is the maximum value of the 4-byte float type greater than the maximum value of the long type?
  We all know that the range of the float type is: a 3.403E38~3.403E38. The range of the long type is: -2^63~2^63-1 (about 9*10^18).
  I used to simply remember that it was over, but I didn't think about why it was like this.
  Let me share with you my current understanding: The
  long integer number occupies 8 bytes in memory and a total of 64 bits. The value it represents is 2 to the 64th power, equally divided into positive and negative, and the value range is negative 2 63 times Square to the 63rd power of positive 2 - 1.
  The float occupies 4 bytes in memory, a total of 32 bits, but the floating point number is like this:
  V=(-1)^s * M * 2^E 32 bits of a
   667x190
  floating point number is not a simple direct representation size, but is allocated according to certain criteria.
  The first bit, the sign bit, is S.
  The next 8 bits, the exponent field, is E.
  The remaining 23 bits, the decimal field, namely M, the value range of M is [1, 2) or [0, 1).
  That is to say, the binary value of the floating-point number in the memory is not directly converted into a decimal value, but is calculated according to the above formula. Through this formula, although only 4 bytes are used, the floating-point number is larger than the long integer. is greater than the maximum value.

  This is the fundamental reason why the long type is converted to the float type during data conversion!

 

When a data is stored in the computer's memory, it needs a certain amount of space. This "space" is told to the compiler by the "data type" (such as int/long/float) in the program.
The data within the space occupied by the data type can be type converted or coerced, and there may be some errors, but generally there will be no bizarre "wrong results"
. ) to a data type that occupies a large space (such as long), generally there will be no "wrong results",
but conversely, when a data type that occupies a large space is converted to a small data type, the precision of the data may be lost due to , so the compiler will prompt an error; at this time, you can choose to cast the type, but sometimes you will get "unexpected results"

Java stipulates:
int occupies 4 bytes
long occupies 8 bytes
float occupies 4 bytes

int and The conversion of long is easy to understand.
4 students can go to the dormitory with 4 beds and the dormitory with 8 beds can be accommodated well;
8 students go to the dormitory with 8 beds, no problem; The 4-bed dormitory is not enough.

The question asked by the landlord can be transformed into: why long type occupies 8 bytes, but behaves like a "small data type" when compared to float which only occupies 4 bytes
(why long can be converted to float, and vice versa Sometimes problems occur when converting float to long)

Legal conversions between Java language data types

6 solid arrows represent conversions with no data loss
3 dashed arrows represent conversions with possible loss of precision
No arrows represent conversions generally not possible
(Reference: JAVA Core Technology Volume I: Fundamentals Figure 3-1)

The author of the core technology gives the following example:
int n = 123456789;
float f = n; // f is 1.23456792E8 When converting
from n to f, although Got the same size result, but lost some precision.

To put it bluntly, although the long type occupies 8 bytes, due to the very strict and precise expression of each digit,
the range of numbers that can be expressed is not as large as the float type that occupies 4 bytes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214144&siteId=291194637