- 雙語版Java程序設計
- 何月順主編
- 751字
- 2018-12-27 20:14:13
3.9 Java’s Floating Point Types
3.9.1 Primitive Floating Point Types
Java supplies two different primitive floating point data types.Table 3.2 lists characteristics of two floating point types.
Java提供了兩種浮點數據類型,其特征如表3.2所示。
Table 3.2 Java’s primitive floating point types.
The minimum and maximum values of the types are declared as manifest constant values in the corresponding Number class.For example the minimum and maximum double values are declared as java.lang.Double.MIN_VALUE and java.lang.Double.MAX_VALUE.
3.9.2 Integer Operators
The operators for the double data types are listed with examples. The operators for the float type are essentially identical.Table 3.3 lists relative operators of float data types.
Table3.3 The operators for float data type
3.9.3 Input and Output of Floating Point Values
The considerations(事項)for the input and output of floating point values are essentially identical to those for integer data types.
3.9.4 Casting of Floating Point to and from Integer Values, and Floating Point Literals
Casting a floating point value from a float to a double representation(表示法)is never dangerous, as it can be guaranteed that a double can always store the value accurately.
For example the following can never result in a dangerous operation and upon completion both variables will have the same value.
float floatVariable = someFloatValue; double doubleVariable; doubleVariable = floatVariable;
Although this is acceptable by Java it is regarded as good style to always indicate that the developer is aware that a type conversion is taking place by explicitly indicating a cast in the assignment.
doubleVariable = (double) floatVariable;
Likewise any integer value can be automatically(自動地) or explicitly(顯式地) cast to either floating point type without any danger, as the floating point variable will always be able to represent it. However the converse(相反的) conversion, from any floating point type to any integer type, can be accomplished by an explicit cast but will result in an unpredictable(不可預知的) value unless the floating point value is within the limits of the integer type’s range. This can be guarded against as shown in the following fragment.
if (((long) doubleVariable) >= java.lang.Long.MIN_VALUE && ((long) doubleVariable) <= java.lang.Long.MAX_VALUE ) { longVariable = (long) doubleVariable; } else { // Value cannot be converted } // End if
Floating point literals can be expressed in conventional ( e.g. 345.678) or exponent notation (e.g. 3.45678e+02), but both mantissa and exponent must always be expressed as denary (base 10) values. By default such literals are always assumed to be of type double. Should it ever be required a double value can be indicated by appending a D and a float by appending a F, e.g. 345.678D or 345.678F.
浮點型數字可以用傳統的符號和指數符號來表示。但它們的尾數和指數必須用十進制數表示。這些數字默認為double類型。為了明確表示double值要附加一個D,而float值要附加一個F,如345.678D,345.678F。
3.9.5 Floating Point Operations in the Standard Packages
A random float or double value between 0.0 and 1.0 can be obtained from the class-wide nextFloat( ) or nextDouble( ) actions in the java.util.Random class. The random number package is automatically seeded from the system clock which will ensure that a different sequence of random numbers is obtained every time an instance is constructed.
The java.lang.Math class supplies a class-wide random( ) action which can sometimes be more conveniently used to obtain a double value between 0.0 and 1.0. Other actions and class wide constants in java.lang.Math. Table 3.4 lists functions in java.lang.Math.
Table 3.4 Java.lang.Math
3.9.6 The Float Class
In addition to the primitive floating point types Java also supplies two classes which implement the double and float types as objects, the major reason for this is to allow them to be used with Java’s utility classes. For example if you wanted to store double values in a hash table then only Double instances could be used, not primitive double variables.
The two packages are java.lang.Number.Double and java.lang.Number.Float., the Double class is essentially identical. Table 3.5 lists functions in class java.lang.Number.Float.
Table 3.5 java.lang.Number.Float