Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

Single and Double Types

In contrast to the data types discussed in the preceding subsection, the Single and Double types can handle data with fractional numbers and decimal points. These two data types use the floating point coding scheme to store data. This coding scheme breaks the stored data into two portions: One portion is used to keep track of the exponent, while the other portion stores the base value (mantissa) of the data. As an illustration, the number 123.45 can be expressed as 1.2345 × 102. The mantissa value, 1.2345, is stored in one part of the storage area while the exponent, 2, is stored in another part. This form of representation can also be used in code, where the mantissa is presented first followed by a symbol E (for exponent) and the exponent value. Here are some examples:


Actual Value
Floating Point Representation
34500 3.45E4
-34500 -3.45E4
.0002345 2.345E-4
-.0002345 -2.345E-4

Both the Single and Double data types can handle a wide range of values, as shown in the previous table; however, the Single type has a precision of approximately seven significant digits. This means if you have an amount like 1234567.89 that must be stored accurately, you should not use the Single type because the last few digits of the amount will be lost. If you need to use a floating point type to handle your data, you should keep this in mind. Note that you can append a suffix (F for single and R for Double) to the literal to explicitly show its type (force the compiler to handle it as such). For example, if you want 3.45 to be handled as Double, you can code:
3.45R

Try This
Type the following code in the code window of a new project without any VB control. Run the project and then click the form. What does the message box display? Change Single to Double in the Dim statement. Run the program again. What do you see this time?
Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click Dim Amount As Single Amount = 1234567.89 MsgBox("The amount is " & Amount) End Sub

The characteristics of the 12 numeric data types discussed in this subsection can be summarized as follows:

Data Type Value Range Precision Speed* Remark
Byte, SByte Small Low NA Seldom used for computation purposes but for handling character strings in ASCII code
Char Medium High NA Seldom used for computation purposes but for handling character strings in Unicode
Boolean Small Low NA Often used to represent state, such as True or False, On or Off
Short, UShort Small Low 2 Often used as counters
Integer, UInteger Medium High 1 Used for integer computations; cannot handle decimal point
Long, ULong Medium large High 3 Used for computations with large integer values; cannot handle decimal point
Single Large Medium 4 Can be accurate for about seven significant digits
Double Very large Very high 5 Requires twice the storage of the Integer or Single type
Decimal Large Very high 6 Requires twice the storage of the double type
* 1= fastest; 6= slowest      

Tip
When you are uncertain how many significant digits the actual data may be, use the data type with higher precision for the variable. Your program may become slower, and will consume more storage space. (Between Short and Integer, the latter is faster.) Nevertheless, you are assured that your program will not encounter a serious unexpected problem. Story has it that the stock market crashed once because a computer program used by a large bank in New York City failed to process all transactions when the assumed maximum number of transactions was exceeded on that day. Program robustness should outweigh the efficiency consideration.
Last change: February 13 2016 18:47:36.
<<PreviousNext>>