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
num = Math.Abs(-3)
which stores 3 into the variable num
- The Pow method can be used to compute base to an exponent as in
num = 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
num = Math.Sqrt(16)
- The Round method rounds a decimal number to a whole number. However, Visual Basic uses something called Banker's
Rounding. When the number ends with .5 then the one's digit determines if the number is rounded up or not. If the one's digit is odd, then the number rounds up as usual. If the one's digit is even, the number does not round up! For more information search Google for "Banker's rounding" (another link).
Examples:
num = Math.Round(34.8) ' num is 35
num = Math.Round(19.99) ' num is 20
num = Math.Round(3.4) ' num is 3
Tricky examples due to Banker's Rounding:
num = Math.Round(11.5) ' num is 12
num = Math.Round(12.5) ' num is 12
num = Math.Round(13.5) ' num is 14
num = Math.Round(14.5) ' num is 14
The Round method can be used to round to specific decimal places like the hundredth's place as in these examples:
num = Math.Round(12.1234, 2)' num is 12.12 since it is rounded to 2 decimal places
num = Math.Round(12.1267, 2)' num is 12.13
num = Math.Round(12.59, 1) ' num is 12.6
The number after the comma specifies how many decimal places your value will be rounded to.
- The Max and Min methods return the greatest or smallest value respectively.
num = Math.Max(4, -6)
causes 4 to be stored in num.
While
num = Math.Min(4, -6)
causes -6 to be stored in num.
You cannot use more than 2 parameters in the parentheses with the Max and Min methods. The statement
num = Math.Max(3, 9, 2)
would generate a compile error. However, you can nest a method inside of another method as in
num = Math.Max(Math.Max(3, 9), 2)
which would cause 9 to be stored in num.
Normally, these methods would be used with variables as parameters in the parentheses as in the following examples
num1 = Val(txtUserInput1.Text)
num2 = Val(txtUserInput2.Text)
MessageBox.Show("The biggest number inputted is " + Math.Max(num1, num2))
- The Floor method causes numbers to be changed to the next smallest integer that would be found (to the left) on the number line.
num = Math.Floor(8.9)
causes 8 to be stored in num while
num = Math.Floor(-8.9)
causes -9 to be stored in num.
Here are some more examples.
Math.Floor(3.9) = 3
Math.Floor(10.01) = 10
Math.Floor(13) = 13
Math.Floor(-2.1) = -3
Math.Floor(-4.9) = -5
- The Ceiling method causes numbers to be changed to the next greatest integer that would be found (to the right) on the number line.
num = Math.Ceiling(8.1)
causes 9 to be stored in num while
num = Math.Ceiling(-8.9)
causes -8 to be stored in num.
Here are some more examples.
Math.Ceiling(3.9) = 4
Math.Ceiling(10.01) = 11
Math.Ceiling(13) = 13
Math.Ceiling(-2.1) = -2
Math.Ceiling(-4.9) = -4
- The following methods will not be used on worksheets or included on tests or exams. However, for advanced math students, the methods may be useful to know.
The Log10, Sin, Cos, and Tan method compute base 10 logs, sines, cosines, and tangents respectively.
num = Math.Log10(100) causes 2 to be stored in num since 10 to the second power equals 100.
num = Math.Sin(3.14) causes 0 (or a number close to that) to be stored in num since the sine of PI is 0.
num = Math.Cos(3.14) causes -1 to be stored in num since the cosine of PI is -1.
num = Math.Tan(3.14/4) causes 1 to be stored in num 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
roundedNum = Math.Floor(unroundedNum + 0.5)
The value of roundedNum will be value of unroundedNum rounded to the nearest
whole number with normal rounding
- Consider the following statement
roundedNum = Math.Floor(unroundedNum * 100 + 0.5)
/ 100
The value of roundedNum will be value of unroundedNum 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.