The representation range and precision of a Java floating point number float or double type

Source: Internet
Author: User

Vaguely remember, floating point size seems to have a trap, because the bottom of the binary number can not accurately represent all the decimals. Sometimes there are things that make people feel strange.

As in Java,

0.99999999f==1f//true

0.9f==1f//false

To understand these, the first thing to figure out is the float and double in the memory structure


1. Memory structure

The range of float and double is determined by the number of digits of the exponent.
Float has an exponential position of 8 bits, and a double has a 11-bit exponent, distributed as follows:
Float
1bit (sign bit) 8bits (digit digit) 23bits (trailing digit)
Double
1bit (sign bit) 11bits (digit digit) 52bits (trailing digit)
Thus, the index range of float is -128~+127, and the double index range is -1024~+1023, and the digits are divided in the form of complement.
The negative exponent determines the absolute minimum non-zero that the floating point can express, and the positive exponent determines the maximum number of absolute value that the floating point can express, which determines the range of the floating-point value.
The range of float is -2^128 ~ +2^127, that is, the range of -3.40E+38 ~ +3.40e+38;double is -2^1024 ~ +2^1023, or -1.79E+308 ~ +1.79E+308.

2. Accuracy of float and double is determined by the number of digits in the mantissa. Floating-point numbers are stored in memory by scientific notation, and the integer part is always an implied "1", because it is invariant and therefore cannot affect precision.
float:2^23 = 8388608, altogether seven bits, because the leftmost one is omitted, which means that it can represent a maximum of 8 digits: 2*8388608 = 16777216. There are 8 significant digits, but it is guaranteed to be 7 bits, which is the precision of float is 7~8 bit valid number
double:2^52 = 4503599627370496, altogether 16, the same, the precision of the double is 16~17 bit

The reason why the F1==F2 can not be used to judge the two number is equal, because although F1 and F2 in may be two different numbers, but by the floating-point number to indicate the accuracy of the limit, it may be wrong to judge two numbers equal!

We can check the following code:

Float f1 = 16777215f;for (int i = 0; i <; i++) {System.out.println (F1); f1++;}

The representation range and precision of a Java floating point number float or double type

Related Article

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.