4- floating point operations

Floating-point arithmetic 
floating point operations and integer operations compared to only these numerical arithmetic, and bit operations do not shift operation. 
Java's floating-point numbers in full compliance with the IEEE - 754 standard, which is the vast majority of computer platforms support standard floating-point representation. 
Large range of a floating point number, however, often can not be accurately represented by floating-point, an error is caused in the course of operation. 
Double X = 1.0 / 10 ; // 0.1 
Double Y = . 1 - 9.0 / 10 ; // .09999999999999998 

Thus, two floating-point equality comparison is often erroneous results may occur. 
Comparison method is to determine the correct absolute value of difference of two floating-point numbers is less than a very small number: 
// Comparative x and y are equal, the absolute value of the first difference calculated: 
Double R & lt the Math.abs = (x - y );
 // then determines whether the absolute value is small enough: 
iF (R & lt < 0.00001 ) {
     //Can be considered equal to 
} the else {
     // not equal 

if the two numbers involved in computing wherein a is an integer, the integer may be automatically promoted to float, but four in a complex calculation, calculation of two integers not Auto lifting occurs where type: 
Double D = 1.2 + 24 / 5 ; // 5.2
 // compiler calculates 24/5 subexpression, according calculates two integers, the result remains an integer of 4. 


Overflow 
integer arithmetic is being given divisor 0, and floating-point operations in the divisor is 0, not given, but several special return value: 
NaN3 Number The A indicates Not 
Infinity Infinity represents a
 - Infinity representing negative infinity
 Double D1 = 0.0 / 0 ; // NaN3 
Double D2 = 1.0 / 0 ; //Infinity 
Double D3 = - 1.0 / 0 ; // -Infinity 

three special values rarely encountered in actual operation, only need to know. 

Casts 
can float casts an integer. When the transformation, floating-point fractional part will be lost. If the transition exceeds the maximum range of integers can be represented, it will return to the maximum integer. For example: 
int N1 = ( int ) 12.3 ; // 12 is 
int N2 = ( int ) 12.7 ; // 12 is 
int N2 = ( int ) - 12.7 ; // -12 
int N3 = ( int ) ( 12.7 + 0.5 ); / / 13
int N4 = ( int ) 1.2e20 ; // 2147483647 

To be rounded, floating-point number can then add 0.5 casts: 
        Double D = 2.6 ;
         int n-= ( int ) (D + 0.5 ); // . 3

 

Guess you like

Origin www.cnblogs.com/nsss/p/11417410.html