Software Development optimize

Comparing Double vs. Single data types in VB6

VB6's two floating point types are called Single and Double. Learn what distinguishes Single from Double, and find out when you would be wise to use Double instead of Single in your programs.

The data types Single and Double are VB6's two floating point types; they're used to store numerical values that have digits to the right of the decimal point. The number of such digits can vary (hence the name "floating point") because the decimal point position can vary. For example:

1.23456
12.3456
123.456

Differences between Double and Single

Most programmers know that type Double can hold larger values than type Single, and Double also requires more space for storage—8 as opposed to 4 bytes.

The most important difference between Double and Single is the precision with which values can be represented. For instance, type Single is limited to 7 digits; thus, these values are fine for a type Single:

1.23
123999
79.098

However, if you try to store the value 123456789 in a type Single, it is rounded to 7 digits: 123456800. Likewise, the value 1.23456789 is truncated to 1.234568.

In contrast, type Double permits 15 digits of accuracy. So for programs that do a lot of mathematical calculations, be sure to use type Double when needed or the accuracy will suffer.

Note: The Visual Basic help talks about a data type named Decimal with 28 digits of accuracy. This sounds great but, unfortunately, it was never actually implemented.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Peter Aitken's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

0 comments