Lecture031 Math stuff

# Lecture031 Math stuff - Arithmetic Expressions With...

This preview shows pages 1–5. Sign up to view the full content.

Lecture 5 -- 1 Computer Science I - Martin Hardwick Arithmetic Expressions With Integers Operators: result is always an integer Symbol Name Example Value (x = 10, y=3)  + addition     x + y 13  – subtraction x – y   7  * multiplication x * y 30   / quotient    x / y   3  % remainder x % y   1  – unary minus    –x -10  + unary plus    +x  10 You can string the operators together to build longer expressions. use parentheses to specify order of operations precedence (after sub-expressions in parentheses) – first: unary plus and minus from right to left – second:  *, / and % from left to right – third:  + and – from left to right

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 5 -- 2 Computer Science I - Martin Hardwick Arithmetic Expression Examples 12 + 2 * 5 - 8 / 3 + 6 10 2 22 20 26 -2 * 3 * (4 + 5) % 10 + -3 -3 -2 9 -6 -54 -4 -7
Lecture 5 -- 3 Computer Science I - Martin Hardwick Arithmetic Exprs With Doubles Arithmetic expressions with real numbers (numbers with decimal points) work the same way as with integers, with a few exceptions: there is no remainder operator (“%”) the “/” operator means “divide”, computing the answer  to many decimal places the result is a real value rather than an integer value Important: Real values are approximate and may contain errors in the last few digits. about 7 digits of accuracy for type float (one word) about 14 digits of accuracy for type double (two  words) so programmers today nearly always use doubles Real values are often represented using scientific notation. Example:     1.857409 x 10 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 5 -- 4 Computer Science I - Martin Hardwick Mixed Mode Arithmetic Expressions Arithmetic expressions both integers and doubles can get tricky. if both operands are integers, integer arithmetic is used
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/30/2012 for the course CSCI 101 taught by Professor Hardwick during the Fall '08 term at Rensselaer Polytechnic Institute.

### Page1 / 14

Lecture031 Math stuff - Arithmetic Expressions With...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online