Review CS 100M: Lecture 3 February 1 Variable Has a name and a value Arithmetic Expression A recipe involving variables, functions, +, -, / , * , ^ Assignment Statements < variable > = < arithmetic expression > Built-In Functions Trigonometric Log, exponential sine cosine tangent inverse sine inverse cosine inverse tangent sin cos tan asin acos atan Built-In Functions exp log log10 log2 exponential natural log base-10 log base-2 log THINK RADIANS, NOT DEGREES Built-In Functions Useful functions for doing certain computations with integers: floor ceiling round fix mod floor p = floor(x) p is assigned the largest integer less than or equal to the value of x floor(-3.5) has value -4 floor(3.5) has value 3 1 round ceiling q = round(x) r = ceiling(x) r is assigned the smallest integer greater than or equal to the value of x. ceiling(-3.5) has value -3 ceiling(3.5) has value 4 q is assigned the value of the closest integer to the value of x (In case of tie, round away from zero) round(-3.5) has value -4 round(2.4) has value 2 fix mod q = fix(x) q is assigned the value of the closest integer to x that is between x and zero, i.e., round towards the origin. round(3.7) fix(3.7) round(-3.7) fix(-3.7) has has has has value value value value If p and q are variables with whole number values, then r = mod(p,q) assigns to r the value of the remainder when we divide the value in p by the value in q. 4 3 -4 -3 p = 30; q = 7; r = mod(p,q) % r has value 2 “Synonyms” max, min, abs Suppose x is initialized. Then x y A B C D E = = = = = = = -3 2 max(x,y) min(x,y) abs(x) max(abs(x),abs(y)) min(abs(x),abs(y)) % % % % % A B C D E gets gets gets gets gets 2 -3 3 3 2 A = sin(x); y = x+1; B = sin(y); and A = sin(x); B = sin(x+1); assign the same values to A and B. 2 “Synonyms” The command Order of Operations When in doubt, use parentheses: fprintf(‘E = %20.15e’,abs(22/7 – pi)) produces the same output as x = 22/7; error = abs(x – pi); fprintf(‘E = %20.15e’,error) And Q. When is a real number x in the interval [L,R]? A. If x is greater than or equal to L and less than or equal to R. Or Q. When is a real number x not in the interval [L,R]? A. If x is less than L or greater than R. y = 1 / (a + b/c)*d + 1 is the same as y = (1 /(a + (b/c)))*d + 1 The And Operation: && if x >= L && x <= R fprintf(‘x is in [L,R]’) else fprintf(‘x is not in [L,R]’) end The Or Operation: || if x < L || x > R fprintf(‘x is not in [L,R]’) else fprintf(‘x is in [L,R]’) end 3 Boolean Expressions • They involve comparisons. • They have a value that can be thought of as either True or False. Examples: 2.Variable x has a positive integer value. Is it divisible by 3 and 5? Yes if the following is true: mod(x,3) == 0 && (mod(x,5) == 0 1.Variables a, b, and c have positive real values. Can we make a triangle with sides that have those values? Yes if the following is true: (a < b+c) || (b < a+c) || (c < a+b) 3.Variable y has a positive integer value. Does it name a non-leap year? Yes if the following is true: mod(y,4)~=0 || (mod(y,100)==0 && mod(y,400)~=0) Rule: Y is an “ordinary” year if it is not divisible by 4 or if it is a century year not divisible by 400. 4