1. Code
2.Java The conversion rule of digital type in
All the factors in the test1 that appear to be in the divisor are taken out, leaving only 1000. But the actual output is 5, not the 1000 we expect. The reason for this is because Micros_per_day is calculated by the int type, and the result of the calculation is 86400000000, which exceeds the maximum value of type int, which is overflowed (because int is 32 bits, 2^31-1=2147483647), 24*60*60* 1000*1000 The final result is 500654080 (see the output of the Print1 in the program).
After the error was generated, the result was paid to a long type of Micros_per_day,long 64 bits, thus maintaining the error result, resulting in the end result of the error.
The solution to this problem is to use the long constant instead of the int constant as the first factor for each product, so that all subsequent computations in the expression can be enforced with a long operation so that the precision is not lost:
Long micros_per_day = 24l*60*60*1000*1000;
The six solid arrows in the figure represent a conversion without information loss, whereas three virtual arrows represent conversions that may lose precision.
3. floating-point type float, double data is not suitable for the area of financial computation that does not allow rounding errors.
For example, the above test2, we expect the result is 0.1, but the actual output is 0.8999999999999999.
This error is caused by the fact that the floating-point number is actually represented by a binary system. The point 1/10 is not exactly represented in the binary system, as it does in a decimal system that cannot be exactly 1/3. Read the 4th below to see why.
You need to use the BigDecimal class if you need an exact numeric calculation that does not produce rounding errors.
4. Now that the floating-point type is used in binary notation, then review the following.
1 ) A brief introduction to the IEEE754 Standard
The float,double in Java and its corresponding wrapper class float and double are all based on the IEEE754 standard.
A real number V in the IEEE 754 standard can be expressed in the form of v= ( -1) sxmx2e, as described below:
(1) The symbol s (sign) determines whether the real number is positive (s=0) or negative (S=1), and deals specifically with the symbolic bits of the value 0.
(2) The valid number m is a binary decimal, and the range of M is in 1≤m<2 or 0≤m<1.
Description: The mantissa M is expressed in the original code .
According to the normalization method of the original code, the highest digit bit (integral part) is always 1, the standard will this 1 default
Storage, so that the mantissa represents a range more than the actual storage. that M only the fractional part is stored.
( See the following examples will understand )
(3) The exponent E is the power of 2, and its function is to weighted the floating-point number.
Note: since E is represented by a shift code , a 32-bit float type needs to be added with an offset of 127, 64-bit double
Type to add an offset of 1023
The following figure is the storage format for float (32-bit) and double (64-bit):
2 ) The conversion of decimal decimal to binary decimal
cases 1 : Binary Turn decimal
cases 2 : decimal number converted into binary number, is the integer part and the fractional part of the conversion, the integer part of 2 In addition, take the remainder, the decimal part with 2 times, take the integer digit.
For Example: convert (13.125) 10 to binary number
1) Integer part: 13/2 quotient 6 1
6/2 Business 3 0
3/2 Business 1 1
1/2 Business 0 1
So the whole number of parts is divided into 1101
2) Fractional part:
So
3 Below are a few examples of normalization representations (take the float type, for example):
A) Decimal decimal 1.25
Binary representation is 1.01
Canonical binary representation i.e. *1.01*
Symbol bit: 0
Index part: 0+127=127
That is 01111111
Tail number part: 01000000000000000000000
Note: The tail number part only stores the decimal part (0.01) , the integer part of the 1 is stored by default. That's right .
verified 4-1)-(2) The problem to be explained.
The end result is 0 01111111 01000000000000000000000