Appearance
Float Point Operations
Compared with integer operations, floating point operations can only perform numerical calculations such as addition, subtraction, multiplication and division, but cannot perform bit operations and shift operations.
In computers, although floating-point numbers can represent a wide range, floating-point numbers have a very important characteristic, which is that floating-point numbers often cannot be represented accurately.
For example:
The floating point number 0.1
cannot be accurately represented in a computer, because 0.1
in decimal is an infinitely recurring decimal when converted to binary. Obviously, no matter whether you use float
or double
, you can only store an approximate value of 0.1
. However, the floating point number 0.5
can be accurately represented.
Because floating-point numbers often cannot be represented exactly, floating-point operations can produce errors:
java
// Floating point arithmetic error
public class Main {
public static void main(String[] args) {
double x = 1.0 / 10;
double y = 1 - 9.0 / 10;
// Observe whether x and y are equal
System.out.println(x);
System.out.println(y);
}
}
Because floating-point numbers have arithmetic errors, comparing two floating-point numbers to see if they are equal often produces erroneous results.
The correct comparison method is to determine whether the absolute value of the difference between two floating point numbers is less than a very small number:
java
// Compare whether x and y are equal, first calculate the absolute value of their difference:
double r = Math.abs(x - y);
// Then determine whether the absolute value is small enough:
if (r < 0.00001) {
// can be considered equal
} else {
// Not equal
}
The representation of floating point numbers in memory is more complex than that of integers. Java's floating point numbers fully comply with the IEEE-754 standard, which is also the standard representation method of floating point numbers supported by most computer platforms.
Type Promotion
If one of the two numbers involved in the operation is an integer, the integer can be automatically promoted to a floating point type:
java
public class Main {
public static void main(String[] args) {
int n = 5;
double d = 1.2 + 24.0 / n; // 6.0
System.out.println(d);
}
}
It is important to note that in a complex four-arithmetic operation, the operation of two integers will not be automatically promoted. For example:
java
double d = 1.2 + 24 / 5; // The result is not 6.0 but 5.2
The calculated result is 5.2
. The reason is that when the compiler calculates the subexpression 24 / 5
, it operates on two integers, and the result is still the integer 4
.
To fix this calculation, change 24 / 5
to 24.0 / 5
. Since 24.0
is a floating point number, 5
is automatically promoted to a floating point number when calculating the division.
Overflow
Integer operations will report an error when the divisor is 0
, while floating point operations will not report an error when the divisor is 0
, but will return several special values:
NaN
means Not a NumberInfinity
means infinity-Infinity
means negative infinity
For example:
java
double d1 = 0.0 / 0; // NaN
double d2 = 1.0 / 0; // Infinity
double d3 = -1.0 / 0; // -Infinity
These three special values are rarely encountered in actual operations, we just need to understand them.
Forced Transformation
Floating point numbers can be cast to integers. During conversion, the decimal part of the floating point number is discarded. If the maximum range that the integer can represent is exceeded after conversion, the maximum value of the integer will be returned. For example:
java
int n1 = (int) 12.3; // 12
int n2 = (int) 12.7; // 12
int n2 = (int) -12.7; // -12
int n3 = (int) (12.7 + 0.5); // 13
int n4 = (int) 1.2e20; // 2147483647
If you want to round, you can add 0.5 to the floating point number and then cast it:
java
public class Main {
public static void main(String[] args) {
double d = 2.6;
int n = (int) (d + 0.5);
System.out.println(n);
}
}
Summary
Floating point numbers often cannot be represented accurately, and the operation results of floating point numbers may have errors;
Comparing two floating point numbers usually compares whether the absolute value of their difference is less than a specific value;
When performing operations on integers and floating-point types, integers will be automatically promoted to floating-point types;
A float can be coerced to an integer, but out of range the maximum integer value will always be returned.