Basic knowledge of floating-point numbers in Java

Source: Internet
Author: User
Tags float number

Accidentally viewing Math.Round's JDK

1   Public Static int round (float  a) {2         if//  greatest Float value less than 0.53             return (int) Floor (A + 0.5f); 4         Else 5             return 0; 6     }

Note that 0x1.fffffep-2f is the nearest 0.5 of the float type of decimal, gee, scientific notation with E for exponential I know, but this p is what ghost. Perhaps some readers will ask, why is this number the most close to 0.5 of the number, this number is exactly how much? So now there are two questions:

1, p stands for what.

2, 0x1.fffffep-2f in decimal to indicate exactly how much.

To announce the answer first, P replaces e as the symbol of the scientific notation index in floating-point numbers in the 16-decimal notation, and E in 1.fffffep-2f is 14 in hexadecimal; the second question is simple and complex. It's simple because it takes just a few lines of code to know what the value is.

SYSTEM.OUT.PRINTLN (0x1.fffffep-2f); BigDecimal BigDecimal=new BigDecimal (0x1.fffffep-2f); System.out.println (Bigdecimal.toplainstring ()); /* Output 0.499999970.4999999701976776123046875

To say it is complicated is to understand why it is the last 0.5 float number.

1, what is P exactly?

Originally, in order to distinguish it from the e in hexadecimal, the floating-point number represented by 16 in Java, we use p instead of e as the index symbol. So the constant represents 0x1.fffffe * 2^ ( -2), F (f) is a float suffix, and does not write to represent a double type.

A floating-point literal have the following parts: a whole-number part, a decimal or hexadecimal point (represented by An ASCII period character), a fractional part, an exponent, and a type suffix. A floating point number is written either as a decimal value or as a hexadecimal value. For decimal literals, the exponent, if present, was indicated by the ASCII letter E or E followed by an optionally signed I Nteger. for hexadecimal literals, the exponent are always required and are indicated by the ASCII letter P or p followed by an O Ptionally signed integer.

Reference link: https://stackoverflow.com/questions/8603232/p-in-constant-declaration/8603263#8603263 (Chinese version of the Java Language Specification 3.10.2)

2. Why is the number closest to 0.5

Any binary floating-point number V can be expressed in the following form:

 

(1) ( -1) ^s indicates the sign bit, when S=0,V is positive, when s=1,v is negative.

(2) m indicates mantissa, range is [0,1] (normalized) or [non-normalized].

(3) 2^e represents the order code.

IEEE 754 stipulates that for 32-bit floating-point numbers, the highest 1 bits are the sign bit s, then the 8 bits are exponential e, and the remaining 23 bits are the valid number M. E represents the unsigned number of the exponential part ek-1ek-2...e0,f represents the unsigned number of the part of the tail, 0.fn-1...f1f0.

For the exp nonzero number, normalized number, the value range of the exponent part is e=e-127, [-126,127], and the tail part is [1,2-2^ ( -23)] (m=1+f).

To get the nearest 0.5 number, we cannot use 1*2 (-1) because it is exactly equal. Second, we use 2-2^ (-23) * * (-2), a very close to 2 of the number (3.9999998) divided by 4来 means 0.5 is undoubtedly the most correct choice.

See here the attentive reader may ask, why not a very near 4 number removal with 8来 to the answer? Regardless of whether the hexadecimal floating-point number is written as 0X1.FFFFFEP2, or if it is written as 0X3.3FFFFF, which has a mantissa greater than 2, the interior of Java is automatically controlled by adjusting the order code to the tail part. So whether you use 3.99999 (approximate, below) divided by 8 or 7.9999999 divided by 16, the final is expressed as a mantissa within the range, multiplied by a step code. In other words, either 3.99999 divided by 8 or 7.9999999 divided by 16 The final result is the same. In order to look more intuitive, to avoid the loss of accuracy after the mantissa of the trouble, we directly fixed the mantissa to [to determine the order code is undoubtedly the most correct choice.

The 0x1.fffffep-2f binary representation is 0 11111100 1111 1111 1111 1111 1111 111.

We can guess that for the number of double types closest to 0.5, the mantissa should be all 1 and the order is-2. That is the 0x1.fffffffffffffp-2.

 Public Static Long round (double  a) {        if//  greatest Double value less than 0.5             return (long) floor (A + 0.5d);         Else            return 0;    }

Looking at the JDK, sure enough, shows that my analysis is no problem.

(The above basic knowledge is from "in-depth understanding computer system" 2.4.2)

Basic knowledge of floating-point numbers in Java

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.