Tracing -- why is the maximum Float Type in Java greater than the long type?

Source: Internet
Author: User

We all know that the floating point type occupies 4 bytes of space in the memory, while the long type occupies 8 bytes of space. But why is the maximum value of the four-byte Float Type greater than that of the long type?

We all know that the float type ranges from 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 even when I was done, I did not think about why it was like this.

 

The following is my understanding:

Long Integer, which occupies 8 bytes in 64-bit memory. It indicates that the value has the power of 2 and is evenly divided between positive and negative values, the value range is 63 power of negative 2 to 63 power-1 of positive 2.

Float occupies 4 bytes in the memory, which is 32 characters in total, but the floating point is in the memory as follows:

V = (-1) ^ s * m * 2 ^ e

 

The 32-bit floating point does not represent the size directly, but is allocated according to certain standards.

The value is 1st BITs, which is the symbol bit (s.

The next 8 digits are the index fields, that is, E.

The value range of the remaining 23 digits, a small number field, that is, M and M is [1, 2) or [0, 1 ).

That is to say, the binary value of a floating point in the memory is not directly converted to a decimal value, but calculated according to the above formula. Through this formula, although only four bytes are used, however, floating point numbers are larger than the maximum values of long integers.

 

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.