Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about Numerical Computation

Similar presentations


Presentation on theme: "More about Numerical Computation"— Presentation transcript:

1 More about Numerical Computation
Assumptions: Graduate level Operating Systems Making Choices about operation systems Why a micro-century? …just about enough time for one concept CIS 1057 Computer Programming C Fall 2013 (Acknowledgement: Many slides based on/borrowed from Professor Hugh C. Lauer. Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CIS 1057 Fall 2013 More about Numerical Computation in C

2 More Numerical Operators
Relational operators <, <=, >, >=, ==, != Return 0 if false, 1 if true Let int a = 3; Then a < 3 returns 0 a >= 3 returns 1 a == 3 returns 1 a != 3 returns 0 CIS 1057 Fall 2013 More about Numerical Computation in C

3 More Numerical Operators
Relational operators are not special in C. They are just like any other operators in expressions Relational operators <, <=, >, >=, ==, != Return 0 if false, 1 if true Let int a = 3; Then a < 3 returns 0 a >= 3 returns 1 a == 3 returns 1 a != 3 returns 0 CIS 1057 Fall 2013 More about Numerical Computation in C

4 Precedence of Relational Operators
Comparisons: lower than arithmetic operators Equality-inequality: lower than comparisons See Table 2-1, p.53 Examples i < lim - 1 means i < (lim – 1) X + (y >= 3) returns the value X (when y < 3) and the value X+1 (when y >= 3) CIS 1057 Fall 2013 More about Numerical Computation in C

5 Increment & Decrement Operators
++x, --x, x++, x-- Increments or decrements x by 1 ++x – increments x, returns new value of x x++ – increments x, returns old value of x Used in many situations, especially for loops --x – decrements x, returns new value of x x-- – decrements x, returns old value of x High precedence than *, /, % Associates right to left CIS 1057 Fall 2013 More about Numerical Computation in C

6 More about Numerical Computation in C
Bitwise Operations & – bitwise AND | – bitwise OR (inclusive) ^ – bitwise exclusive OR << – left shift Same as multiplying by 2 (i.e., fills low-order bits with zeros) >> – right shift Machine dependent fill on left, depends upon sign bit ~ – one’s complement May only be applied to integral types i.e., int, short, long, char signed or unsigned CIS 1057 Fall 2013 More about Numerical Computation in C

7 Conditional Expressions
expr1 ? expr2 : expr3 Evaluate expr1. If result is true, evaluate and return expr2 Otherwise evaluate and return expr3 Example, z = (a < b) ? a : b Assigns z the value of a if a < b, or b otherwise See tricky code at end of §2.11 For use in Programming Assignment #2 CIS 1057 Fall 2013 More about Numerical Computation in C

8 Assignment Operator (yet again)
location += expression means Add expression to the value at location and assign the result back into the same location Similarly for -=, *=, /=, %=, ^=. |=, <<=, >>= E.g., x *= a is the same as x = x * a y /= b is the same as y = y * b z <<= 3 is the same as z = z << 3 Exceptions will become apparent later in the course CIS 1057 Fall 2013 More about Numerical Computation in C

9 More about Numerical Computation in C
Type Conversion May be automatic or explicit Automatic, for signed operands:– If either is long double, convert other to long double Else if either is double, convert other to double Else if either is float, convert other to float Otherwise, convert char and short to int and then if either is long, convert other to long I.e., “promote” numerical types from lower to higher CIS 1057 Fall 2013 More about Numerical Computation in C

10 Type Conversion (continued)
Automatic type conversion of unsigned integer values is Tricky and Machine dependent CIS 1057 Fall 2013 More about Numerical Computation in C

11 Explicit Type Conversion
Definition – cast A unary operator applied to an expression to explicitly force the value to a particular type Represented as (type) expression High precedence, equal to unary operators Associates right-to-left Example (int) sqrt(2*pi) Converts the square root of 2π to an integer and truncates the fractional part (i.e., no rounding) CIS 1057 Fall 2013 More about Numerical Computation in C

12 More about Numerical Computation in C
Questions? Read or review Chapter 2 CIS 1057 Fall 2013 More about Numerical Computation in C


Download ppt "More about Numerical Computation"

Similar presentations


Ads by Google