Wyo VB Lecture Notes
Objective #1: Use the Math class
methods.
- The Abs method can be used to compute the absolute value of a number as in
intNum = Math.Abs(-3)
which stores 3 into the variable intNum
- The Pow method can be used to compute base to an exponent as in
intNum = Math.Pow(3, 4)
which simplifies to 81 which is 3 to the fourth power. You should use the Pow method rather than the ^ operator.
- The Sqrt method can be used to compute a square root as in
dblNum = Math.Sqrt(13)
- The Round method can be used to round decimal numbers to the nearest whole number. For example,
intNum = Math.Round(13.5)
rounds to 14 while
intNum = Math.Round(13.4)
rounds to 13. However, Visual Basic uses Banker's
Rounding with this built-in Round method. So if the decimal part of the unrounded number is .5 and the one's digit is an even number, the Round method does not round up. For example,
intNum = Math.Round(12.5)
rounds to 12.
Here is more info about Banker's Rounding.
- The Max and Min methods return the greatest or smallest value respectively.
intNum = Math.Max(4, -6)
causes 4 to be stored in intNum.
While
intNum = Math.Min(4, -6)
causes -6 to be stored in intNum.
- The Floor method causes numbers to be changed to the next smallest integer
intNum = Math.Floor(8.9)
causes 8 to be stored in intNum while
intNum = Math.Floor(-8.9)
causes -9 to be stored in intNum.
- The Ceiling method causes numbers to be changed to the next greatest integer
intNum = Math.Ceiling(8.1)
causes 9 to be stored in intNum while
intNum = Math.Ceiling(-8.9)
causes -8 to be stored in intNum.
- The Log10, Sin, Cos, and Tan method compute base 10 logs, sines, cosines, and tangents respectively.
intNum = Math.Log10(100) causes 2 to be stored in intNum since 10 to the second power equals 100.
dblNum = Math.Sin(3.14) causes 0 (or a number close to that) to be stored in dblNum since the sine of PI is 0.
dblNum = Math.Cos(3.14) causes -1 to be stored in dblNum since the cosine of PI is -1.
dblNum = Math.Tan(3.14/4) causes 1 to be stored in dblNum since the tangent of PI/4 is 1.
- Pretty accurate values for PI and e (Euler's constant) can be used since they are defined in the Math class as constants. The statement
MessageBox.Show(Math.PI) will display PI pretty accurately and
MessageBox.Show(Math.E) will display e pretty accurately.
Objective #2: Use the "macho" rounding formula
to implement normal rounding rather than Banker's Rounding like the Math.Round method.
- Consider the following assignment statement
intRounded = Math.Floor(dblUnrounded + 0.5)
The value of intRounded will be value of dblUnrounded rounded to the nearest
whole number with normal rounding
- Consider the following statement
dblRounded = Math.Floor(dblUnrounded * 100 + 0.5)
/ 100
The value of dblRounded will be value of dblUnrounded rounded
to the nearest
hundredth's place with normal rounding
- Only Mr. Minich calls this method of rounding "macho rounding".
You will not see this algorithm referred to as macho rounding anywhere else.