Floating point score is single-precision and double precision, single precision and double precision in Java is float and double. How do you know how float and double store?
Float accounts for 4 bytes, and Double accounts for 8 bytes. For convenience, only the float type is discussed. Float is actually the same as an int type, a total of 32-bit, the first representation symbol, 2-9 Represents an index, the latter 23 represents the fractional portion. Not much here, please refer to: http://blog.9cbs.net/treeroot/archive/2004/09/05/95071.aspx
Here is only an example, I hope to throw the jade, it is to study the storage form of floating point 0.1, first run this program.
Public class test {public static void main (string [] args) {int x = 0x3d800000; INT i = 1 << 22; int J = 1 << 4; float f = 0.1f; int y = float.floattoinTbits (f FLOAT REST = F - ((float) 1) / j;
While (i> 0) {j << = 1; float deta = ((float) 1) / j; if (REST> = DETA) {REST - = DETA; X | = i;} i >> = 1; } PR (x); Pr (y);
Static void Pr (INT I) {system.out.println (INTEGER.TOBINARYSTRING (I));
}
Results: 11110111001100110011001100110011001100110011001100110011001101
Program Description: int x = 0x3d80000; because the floating point representation is 1.F * 2N-127 We have to represent 0.1, you can know that n-127 = -4, to n = 123 symbol is positive, you can know the first 9 is 001111011, temporary Do not consider the number of 23 decisions behind, so we first assume x = 0x3d800000;
INT i = 1 << 22; i initially, the third right is 1, that is, the 10th place
INT j = 1 << 4; i is initially 4, because N-127 is -4, here is in order to seek the countdown.
Float f = 0.1f; int y = float.floatthannelBits (f); y is its 32-bit represented
Float rest = f - ((float) 1) / j; this REST indicates that the rest of the 1F, that is, 0.FWHile (i> 0) {j << = 1; float Deta = (( Float) 1) / j; if (REST> = DETA) {REST - = DETA; X | = I;} i >> = 1;} This loop is calculated to calculate the 23-bit fraction, if REST is not less than Deta, indicating this The bit can be set to 1.
Other don't say, the result is the same, you can say that this floating point must be inaccurate, but 0.5 can express exactly, think about why.