Some Tricks for Improving Accuracy VB Help

If your calculation  require the maximum precision possible, here are a few  but effective, tricks.

  1. Bias the numbers If you’re doing calculation In the range 100,010 to 100,100, subtract 100,000 from the Initial numbers to reduce the values In the range to 10 to 100′ When you’ are done, add the bias 100,000 to the result.
  2. Manipulate your expression  Examine your expressions and try to remove , redundancy An expression such as (a * b) / a need not be calculated the res’ult is b, no matter what. If the previous expression Is evaluated, it can’t yield a more accurate result, and It’S likely that it will Yield a less accurate result, This is ii simple example, but long expressions may contain redundancy that’s not as obvious.
  3. Specify variables: of the same type. Don’t expect that two floating-point numbers that are assigned the same value will be exactly the same This is odd, but here’s an example: Create a single-precision variable cl and a double-precision variable b, and assign the same value to them:
    a = 7.03007
    b = 7.03007

.Then print their difference:

The result won’t be. zerol It will be: -1.71 96655299756E-07. Because different numeric types tire stored differently in memory, they don’t quite match. What this means to you is that all variables in a calculation should be of the same type. In addition, don’t make comparisons like

If a = b Then {do something}

Use a threshold instead, If the difference is smaller than a threshold, then the two values can be considered equal (depending on the nature of your calculations, of course):

If (a – b) < 0.000001 Then {do something}

Declaring Numeric Variables To declare variables of any of the previous types, lISC the Dim statement, followed by the name of the variable, the As keyword, and the variable’s type. The following are all valid declarations of numeric variables: .

Dim count As Integer
Dim DayslnCentury As Long.
Dim length As Single
Dim Area As Double

You can also combine multiple declarations on the same line. If the variables are of the same type separate them with Commas:

Dim Area As Double, Volume As Double

You can also specify multiple variables with different types in the same statement:

Dim Area As Double, Count As Integer

In the following statement only the C variable will be declared as Integer; the other two, A and 8, will be declared as variants:

Dim A, B, C As Integer

You can use other keywords in declaring variables, such as Private, public, and Static. We’ll look at these keywords in later sections of this chapter. In the meantime, bear in mind that all variables declared with the Dim statement exist in the Module in which they were declared. If the variable Count is declared in a function, it exists only in this function. You can’t access it from outside the function, Actually, you can have a Count variable in multiple functions. Each variable is stored locally, Variables declared in different procedures have different values and don’t interfere with-one another .

String Variables

The String data type stores only text, and string variables are declared with the String type:

Dim someText As String

You can assign any text to the variable some-text,You can store nearly 2GB of text in a string variable, The following assignments arc all valid:

The second assignment creates an empty string, and the last one creates a string that just happens to contain numeric digits, which are also characters. The difference between the variables

someNumber = 15000
someText = 5.000

is that they hold different values. The sometext variable holds the digits “1”, /15″, “0”, “0”, and “0”, and someNumber holds a numeric value. You can, however, use the variable sometext in numeric operations, and you can use the variable someNumber  in string operations. Visual Basic performs the necessary conversions for you. After all, when you attempt to divide “15,000” by another number; your intentions are obvious, and your application need not crash with a runtime error (as it would with other versions of BASIC).

Declare and initialize two string variables, A and B, as follows:

Dim A As String, B As String
A = ‘123’
B = ’10’

Dividing A by Bis a valid operation, even though the two variables are defined as strings. The following statement prints 12.3 in the Immediate window:

Debug.Print A / B

This means that Visual Basic figured out that you wanted to use the two variables’ as numbers, converted them to integers, performed the division, arid converted the result to the proper data type for you.

You can even use the two variables as both string and numeric values in the same expression. The’ following statement won’t confuse Visual BASIC:

Debug:Print A & ‘divided by • & B & . is • & A / B

It will actually display this message:

123 divided by 10 is 12.3

The & operator concatenates (joins) two variables as strings, even if their values are numeric. To make sure that the variables A and B will be concatenated and not added, use the & operator. The following statement will print the string 12310 and not the numeric value 133:

Debug.Print A & B

The same result would be printed even if the variables A and B were numeric.

Fixed-Length Strings As you can see, string variables have variable lengths. They grow and shrink as needed to accommodate the values assigned to them. You can also specify fixed-length strings with a statement like:

Dim someText As String * 1000

This variable is long enough to hold 1,000 characters. If you assign a string with fewer than 1,000 characters ‘to it, the variable is padded with spaces. If you assign string that exceeds the maximum declared length, it will be truncated

Fixed-length strings are used in several situations. For example, you can declare a fixed-length string variable to accept user input when its maximum length is known. If a string variable’s size will change drastically during the course of an application, you might want to declare it as fixed-length to prevent Visual Basic from having to resize it constantly ..

Boolean Variables

The Boolean data type stores True/False values. Although a single bit would be adequate, for efficiency reasons Visual Basic allocates two bytes to this data type. Boolean variables are, in essence, integers that take the value -1 (for True) and 0 (for False). Actually, any non-zero value is considered True.

Boolean variables are declared as:

Dim failure As Boolean

and they are initialized to False.

Boolean variables are used in testing conditions, such as the following:

If failure Then MsgBox ‘Couldn’t complete the operation’ ,

They are-also combined with the logical operators AND, OR, NOT, and XOR . Th~ NOT operator toggles the value of Cl Boolean variable. The following statement is a toggle:

running = Not running

If the variable running is True, it’s reset to False, and vice versa. This statement is a shorter way of coding the following:

Date Variables

Date and time values are stored internally in a special format, but you don’t need to know the exact format. They arc double-precision numbers: the integer part represents the date and the fractional part represents the time. A variable declared as Date can store both date and time values with a statement like the following:

Dim expiration As Date

The date format is determined by the Regional Settings (open.the Control Panel and double-click the Icon Regional Settings). In the United States. it’s mm/dd/yy (in other countries the format is dd/mm/yy). If you assign invalid date to a date variable. like 23/04/99. Visual Basic will automatically swap the month and day values to produce a valid date. If you assign the previous value to a date (or variant) variable and then print the variable. you’lI see that Visual Basic converts It to 04123199.If the date is invalid even after the swapping of the month and day values. then a runtime error will b’e generated.

The Date data type is extremely flexible; Visual Basic knows how to handle date and time values without performing complicated conversions. To manipulate dates and times, use the Date and Time functions, which are explained in Appendix A on the CD. You can, however, directly subtract two date ‘variables to find out their difference in days. Let’s initialize two date variables:

The day1 variable will be the current ‘date. ‘The difference between the two dates is calculated by:

MsgBox day2 = day1

which is the number of days from the current date to the end of the millennium. On January 5,1997, the difference was 1091 days. You can easily find the number of years, months, and days between the two dates with the help of the Year(), Montht(), and Day() functions (also explained in Appendix A on the CD):

You can also add Jays to a date variable. integer values correspond to days. To add an hour, add 1/24 of a day; to add a minute, add 1/(24460) of a day; to add a second, add the value 1./(24.460″60.). If day1 is:

day1 = ’11/3/96 1:03:05 PM’

the following Print statements will display the results shown:

When other numeric data type are converted to the Date data type, values to the left of the decimal represents date information, and values to the right of the decimal represent time. Midnight is 0 and midday is 0.5. Negative whole numbers represent dates before December 30, 1899. To arrive at the date and time six hours from now you would use:

now + 0.25

If the current time Is less then six hours from midnight, the following statements will also change he date:

Posted on November 4, 2015 in Visual Basic: The Language

Share the Story

Back to Top
Share This