Visual Basic I Programming CSC 162 Visual Basic I Programming
Math and Number Functions Rounding Functions Trigonometric Functions Exponential and Logarithmic Functions Sign-Number Functions Random Number Function
Math and Number Functions Rounding Functions Round(x, y) Rounds x to y decimal places Examples: Round(5.23832, 2) produces 5.24 Round(5.23832, 1) produces 5.2 Round(5.23832, 0) produces 5 Int(x) Greatest integer function (rounds x down to the closest integer): Examples: Int(3.78) produces 3 Int(-3.78) produces -4 Fix(x) Rounds x down to the closest integer towards zero Examples: Fix(3.78) produces 3 Fix(-3.78) produces -3
Int vs. Fix Int Int -1 1 -2 2 3 4 -3 -4 Fix Fix -1 1 -2 2 3 4 -3 -4
Math and Number Functions Trigonometric Functions Sin(x) Sine of angle x (x measured in radians): sin(x) Example: Sin(0.52) produces 0.4969... Cos(x) Cosine of angle x (x measured in radians): cos(x) Example: Cos(0.7) produces 0.7648... Tan(x) Tangent of angle x (x measured in radians): tan(x) Example: Tan(0.854) produces 1.1476... Atn(x) Arctangent of x (result measured in radians): arctan(x) Example: Atn(1.148) produces 0.8452...
Math and Number Functions Exponential and Logarithmic Functions Exp(x) Exponential function: ex Example: Exp(2.1) produces 8.1662... Sqr(x) Square root of x: Examples: Sqr(4) produces 2 Sqr(2) produces 1.4142... Log(x) Natural logarithm of x: ln(x) or loge(x) Examples: Log(24) produces 3.1781... Log(2.789) produces 1.0257...
Logarithm Bases and Names log10(x) = log(x) loge(x) = ln(x) log2(x) = lg(x)
Math and Number Functions Sign-Number Functions Abs(x) Absolute value of x: | x | Examples: Abs(4) produces 4 Abs(0) produces 0 Abs(-7.2) produces 7.2 Sgn(x) Signum function (sign of x) Examples: Sgn(5.1) produces 1 Sgn(0) produces 0 Sgn(-3) produces -1
Math and Number Functions Random Number Function Rnd() Generates a random number: 0 ≤ x < 1 Examples: X = Rnd() produces 0.1245... X = Rnd() produces 0.7942... X = Rnd() produces 0.9997... To generate a random number: a ≤ x ≤ b X = Int(Rnd() * (b – a + 1)) + a
Random Number Generators
Math and Number Functions Obtaining π (pi) and e π (pi) Pi = 4 * Atn(1) e e = Exp(1) Converting between Degrees and Radians 180º ↔ πr Degrees to Radians Radians to Degrees
Programming Assignment 5 Lab Assignment Write a function DegToRad that converts an angle in degrees to radians. Use this function in a program that allows the user to input an angle (measured in degrees) in a textbox, and then computes the sine, cosine, tangent, cosecant, secant, and cotangent. Display these values in six textboxes with appropriate labels. Round all answers to four (4) decimal places Programming Assignment 5 Due Monday, November 10 / Tuesday, November 11 Page 241 #6.18 Page 241 #6.19