< Day Day Up > 
The floatingpoint data types store numbers that can have fractional parts, and are most useful when you are dealing with values that are not integers. A floatingpoint 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 floatingpoint number. For example, the floatingpoint literal 1.5E3 is equivalent to the floatingpoint literal 1500.0 . While the floatingpoint 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 floatingpoint types is the literal 0.0 .
The exact behaviors of the floatingpoint types are defined by the .NET Framework and are beyond the scope of this book. However, a few pitfalls are worth mentioning. Floatingpoint 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 floatingpoint 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 floatingpoint 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 floatingpoint 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. FloatingPoint LiteralsFloatingpoint literals have same three parts as a floatingpoint 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 floatingpoint literals. Dim a As Single Dim b, c, d As Double a = 1.5F b = 3E30 c = 4R d = 0.4 If a floatingpoint value specifies an exponent, it can choose to omit the mantissa. The type of a floatingpoint 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 floatingpoint literal by adding the floatingpoint type characters.

< Day Day Up > 