< Day Day Up > |
The floating-point data types store numbers that can have fractional parts, and are most useful when you are dealing with values that are not integers. A floating-point number has three parts : a whole part (the part to the left of the decimal point), a fractional part (the part to the right of the decimal point) called the mantissa, and an exponent . The exponent indicates the power of ten to multiply the other two parts by to get the actual value of the floating-point number. For example, the floating-point literal 1.5E3 is equivalent to the floating-point literal 1500.0 . While the floating-point types can represent a large range (such as 1E+308), they have limited precision. This means that a sufficiently large or small value can keep only a certain number of significant digits. The Double type can preserve approximately 18 digits of precision, while the Single type can preserve only about 8 digits of precision. The default value of the floating-point types is the literal 0.0 .
The exact behaviors of the floating-point types are defined by the .NET Framework and are beyond the scope of this book. However, a few pitfalls are worth mentioning. Floating-point values differ from integer values in that they have special values such as positive and negative zero, positive and negative infinity, and NaN (Not a Number). This last value is the most interesting because the result of dividing a floating-point number by zero is NaN instead of an exception. In the following example, the code will run without error even though there is division by zero, and the variable a will end up with the value of NaN. Dim a, b As Double a = 1.5 b = 0.0 a = a / b
Another thing to be aware of is that the .NET Framework may calculate floating-point operations at whatever precision the machine that the code is running on can support, and this may be a higher precision than Double or Single supports. As a result, comparing the results of floating-point operations may give unexpected results. Take the following example. Dim a, b As Double a = 1.0 / 3.0 b = 1.0 / 3.0 If a = b Then Console.WriteLine("equal") Else Console.WriteLine("not equal") End If Depending on how the code is compiled by the JIT compiler at runtime, either result may be printed because one or both of the division operations may be calculated at a higher precision than the other. Floating-Point LiteralsFloating-point literals have same three parts as a floating-point value: a whole part, a mantissa, and an exponent. The whole part is just an integer literal and is followed by a decimal point. The decimal point is followed by the mantissa represented as an integer literal. An exponent can be specified by adding the character E to the end and then a signed integer literal specifying the exponent. The following shows some examples of floating-point literals. Dim a As Single Dim b, c, d As Double a = 1.5F b = 3E-30 c = 4R d = 0.4 If a floating-point value specifies an exponent, it can choose to omit the mantissa. The type of a floating-point literal is always Double , unless it is explicitly typed using a type character: F for Single and R for Double .
An integer literal can also be turned into a floating-point literal by adding the floating-point type characters.
|
< Day Day Up > |