Download IMS 3253: Math - University of Central Florida

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
IMS 3253: Math
Topics
• Five Fundamental Math Operations
• Precedence of Math Operations
– Using Parentheses to override precedence
• Writing and Interpreting VB Calculation
Statements
• Mathematical Functions
“A computer lets you make more mistakes faster than any
invention in human history—with the possible exceptions of
handguns and tequila”
Mitch Ratcliffe
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
1
IMS 3253: Math
Numeric Operations in Computer Code
• Computers were invented to process large
quantities of numeric data
• Computers perform arithmetic operations on
numeric values incredibly rapidly
• All computer operations can be performed using
the basic operations of addition, subtraction,
multiplication, and division.
• Most computer languages also provide for
exponentiation
• Most also provide built in functions for more
complex operations
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
2
IMS 3253: Math
Objectives
• Understand the five fundamental arithmetic
operations in VB
• Understand how to write and interpret
mathematical calculations in VB
• Understand the precedence of mathematical
operations in computers and how parenthetical
grouping affects calculation order
• Understand the use of predefined mathematical
functions available in VB
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
3
IMS 3253: Math
Five Fundamental Arithmetic Operations
• VB supports five built-in arithmetic operations:
Operation
–
–
–
–
–
Conventional Notation
Addition
Subtraction
Multiplication
Division
Exponentiation
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
3+4
3-4
3x4
3 
34
Computer Symbol
3+4
3-4
3*4
3/4
3^4
4
IMS 3253: Math
The Assignment Operation
• Many times the results of a calculation or other
operation are assigned to a variable, property, or
other value holder
intSum = intFirstValue + intSecondValue
intSum = Val(txtFirst.Text) + Val(txtSecond.Text)
MainMenu.Top = (Screen.Height - MainMenu.Height) / 3
• The calculation on the right of the equal sign is
performed first and assigned to the location on
the left
• The Equal Sign is an assignment operator
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
5
IMS 3253: Math
Using Numeric Calculations
• Legal statements
• Illegal statements
x = 4
y = 2
z = x + 5
t = x * 2
txtPrice.Text = x + y
Const Pi = 3.14
dblDiameter = Pi *
sglRadius
dSqrt = x ^ .5
4
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
x + y
z% = x% + txtInput.Text
sglPrice = $1,456.98
6
IMS 3253: Math
Precedence of Numeric Calculations
• Q: What is the result of 3 + 5 * 2?
A:
• Numeric operations have a defined hierarchy or
precedence of calculation
1. Exponentiation
2. Multiplication and division
3. Addition and subtraction
4. Left to right within categories
– Because multiplication precedes addition 5 is
multiplied by two giving 10 first after which the
three is added to give 13
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
7
IMS 3253: Math
Precedence Exercises
What are the results of the following calculations if x = 12 and y = 4?
x + y / 2
y ^ 3 / 4
x + 2 * y - 2 * x
8 + x * y / 3
-8 + y ^ -2
16 ^ (1 / 4)
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
8
IMS 3253: Math
Using Parentheses to Group Calculations
• Operations grouped in parentheses are
performed first, regardless of the natural order of
calculation
3 + 5 * 2 = 13
(3 + 5) * 2 = 16
• Parentheses may be nested
((3 + 5) * 8) / (2 * 4) = ?
Inner parentheses are
evaluated first
• Parentheses may be used to visually format
mathematical expressions even if no change to
the calculation order is needed
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
9
IMS 3253: Math
More Precedence Exercises
Write computer expressions for each mathematical expression
ab
z
d
bc
z  x3  y  2
 b  b 2  4 ac
x
2a
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
10
IMS 3253: Math
Numeric Functions
• VB (and most other programming languages)
provide predefined numeric calculations in the
form of functions
• Functions often operate on a value and return
some transformation of the value
• E.g. in
x = 25
y = Sqrt(x)
Sqr( ) is a predefined function which returns the
square root of its argument (x in this case)
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
11
IMS 3253: Math
Functions and Expressions as Arguments
• Functions may be nested
E.g., in Math.Sqr(Math.Abs(x))
the absolute value of x is determined first and then
becomes the argument of the square root function
• A calculation may be performed inside the
argument of a function
E.g., in Math.Sqr(x ^ 2)
the value of x is first squared and then the square
root of the result is taken
• Functions are always calculated from the inside
out
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
12
IMS 3253: Math
Empty Variables in Mathematical Operations
• Variables which have never been assigned a
value are said to be ‘empty’
• If a mathematical operation is performed on an
empty variable the variable behaves as if its value
is zero
• Be very careful as division by zero results in a
runtime error which will crash the program.
• NB: Variables loaded from databases may
sometimes contain a null value which is different
from empty.
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
13
IMS 3253: Math
Converting Data Types
Introduces
inefficiency
• VB will automatically convert many numeric data
types before performing calculations
– Single precision values can be multiplied by
integers, assigned to currency variables etc.
• Some conversions cannot take place
– String values cannot be assigned to explicitly
declared numeric variables (non variants)
– Commonly arises when using text boxes, list
boxes, grid controls, etc.
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
14
IMS 3253: Math
The Val( ) Function
• Val( ) returns the numeric value of its argument
– Argument is typically a string but may be a number
– Val converts strings to numbers
• A common use of Val is to convert values which
must be treated as text into numbers Some properties always
contain characters
intQty = Val(txtQty.Text)
intCourse = Val(cboCourseNumber.Text)
sglPrice = Val(InputForm.Tag)
• Or use the Convert method
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
15
IMS 3253: Math
The Val ( ) Function: Examples
• Val (“123.54”)
123.54
• Val (““)
0
• Val(“123A45”)
123
• Val(“A123”)
0
• Val(“ 123 456”)
123456
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
16
IMS 3253: Math
Methods of the Math Class
Name
Description
Abs
Overloaded. Returns the absolute value of a specified number.
Acos
Returns the angle whose cosine is the specified number.
Asin
Returns the angle whose sine is the specified number.
Atan
Returns the angle whose tangent is the specified number.
Atan2
Returns the angle whose tangent is the quotient of two specified
numbers.
BigMul
Produces the full product of two 32-bit numbers.
Ceiling
Overloaded. Returns the smallest integer greater than or equal to
the specified number.
Cos
Returns the cosine of the specified angle.
Cosh
Returns the hyperbolic cosine of the specified angle.
DivRem
Overloaded. Calculates the quotient of two numbers and also
returns the remainder in an output parameter.
Exp
Returns e raised to the specified power.
Floor
Overloaded. Returns the largest integer less than or equal to the
specified number.
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
17
IMS 3253: Math
Methods of the Math Class (cont.)
IEEERemainder
Returns the remainder resulting from the division of a specified
number by another specified number.
Log
Overloaded. Returns the logarithm of a specified number.
Log10
Returns the base 10 logarithm of a specified number.
Max
Overloaded. Returns the larger of two specified numbers.
Min
Overloaded. Returns the smaller of two numbers.
Pow
Returns a specified number raised to the specified power.
Round
Overloaded. Rounds a value to the nearest integer or specified
number of decimal places.
Sign
Overloaded. Returns a value indicating the sign of a number.
Sin
Returns the sine of the specified angle.
Sinh
Returns the hyperbolic sine of the specified angle.
Sqrt
Returns the square root of a specified number.
Tan
Returns the tangent of the specified angle.
Tanh
Returns the hyperbolic tangent of the specified angle.
Truncate
Overloaded. Calculates the integral part of a number.
Dr. Lawrence West, MIS Dept., University of Central Florida
[email protected]
18