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>>

Mathematical Functions

The following table lists three mathematical functions.

Function Use Code Example Result
Fix(x) Returns the integer portion of x by truncation Fix(3.6) 3
Int(x) Returns the integer portion of x by truncation Int(3.6) 3
Rnd(x) Returns a random number in the range of 0 to 1 (but less than 1) (See the following for additional explanation) Rnd() Fractional random number

Fix and Int differ in their way of handling negative values. Fix truncates the fraction; Int gives the next negative integer smaller than the parameter. For example, Fix(-3.3) will return -3, whereas Int(-3.3) will return –4.

Rnd is a random number generator that returns a fractional number of the Single type between 0 and 1 each time it is called. Depending on the value of the parameter given, it returns different results. If the parameter is a negative value, that value is used as the seed and results in the same random number if the same parameter is used. If the parameter is equal to zero, it returns the most recently generated random number. If no parameter is given, or if the parameter is greater than zero, it returns a random number in the sequence.

Rnd uses a seed to start the random number sequence. To avoid repeating the same sequence each time you run a program, you can use the Randomize statement to set a different seed as follows:

Randomize

The Randomize statement uses the system timer as the seed for the random number sequence. Because it is virtually impossible to have the same timer value repeated, there is no chance that the same sequence of random numbers will be generated.

The Random Object

In the preceding section, you saw the Random object used as an example to illustrate how a variable should be declared and associated with an object. The use of this object, which provides two methods of random number generation, is now explored. The NextDouble method takes no parameter, and returns a random number (of the Double type) that is greater than or equal to 0.0 and less than 1.0, providing the same functionality of the Rnd function. The Next method takes up to two integer parameters. When no argument is specified, the method returns an integer random number. When one argument is specified, the method returns a random number less than the argument. When two arguments are specified, the method returns a random number that is greater than or equal to the first argument, but less than the second argument. The second argument must be at least equal to the first argument; otherwise, you will encounter an argument out of range exception. The following statements will generate a random number between 1 and 100, inclusive:

Dim Rand As New Random() Dim R As Integer R = Rand.Next(1, 101)

As you can see, when you need to generate a series of integer random numbers within a range, this method is more convenient to use than the Rnd function. Note that the Random object randomizes its seed when it is instantiated. There is no need to use the Randomize statement.

Methods in the Math Object

VB 2008 also provides a Math object that contains many methods for mathematical computations. The following table gives a selected list of these methods:

Function Use Code Example Result
Abs(x) Returns the absolute value of x Abs(-3.2) 3.2
Ceiling(x) Returns the smallest whole number Larger than or equal to x Ceiling(2.3) 3
Cos(x) Returns the cosine value of x, where x is an angle in radians Cos(3.14159265359) -1
Exp(x) Returns the value of ex Exp(1) 2.71828182845905
Floor(x) Returns the largest whole number smaller than or equal to x Floor(2.3) 2
Log(x) Returns the natural logarithm of x Log(2.71828182845905) 1
Log10(x) Returns the common logarithm of x Log10(100) 2
Max(a, b) Returns the larger of the two numbers Max(10, 100) 100
Min(a, b) Returns the smaller of the two numbers Min(10, 100) 10
Round(x) Returns a whole number nearest to x Round(5.6) 6
Sign(x) Returns the sign (1, 0, or –1) of x Sign(-9) -1
Sin(x) Returns the sine value of x, where x is an angle in radians Sin(0) 0
Sqrt(x) Returns the square root of x Sqrt(100) 10

To use these methods, precede the method name with Math and a dot, similar to any typical method in an object. For example, to obtain the square root of 100, you code:

Math.Sqrt(100)

Tip
The Math object also provides two commonly used constants in math: E and PI. You can conveniently use PI to perform trig computations. For example, you can code the following to obtain the cosine value at 180 degrees:
Math.Cos(Math.PI)
Use the keyword Math to search the Index tab of the help file for all members in the Math objects.

A Side Note on Referencing Object Methods

Are you wondering about the difference in the treatment of objects between Random and Math? To use the Random object, you have to create (instantiate) and associate it with a variable. But to use the Math object, you have not done so. What causes the inconsistency—or more appropriately, difference—in treatment? The difference has to do with the types of methods in these objects. Technically, Math is a class. Methods provided in the Math class are static. These methods exist without having to be instantiated. You directly reference static methods and properties by using the class name, instead of the name of the instance (object) instantiated from the class.
On the other hand, the methods in the Random class are instance methods. You must have an instance of the class before you can reference these methods. And you must qualify the method name with the instance (object) name, not the class name.

Last change: February 13 2016 18:47:37.
<<PreviousNext>>