lect2_2 - C++ Basics - Part 2 1 More C++ Basics...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ Basics - Part 2 1 More C++ Basics Floating-point types As mentioned before, the double data type provides 15 digits of precision. For even greater precision, upto 19 digits, the data type long double can be used. The float data type provides the smallest precision (6 digits), but might be useful in a program because a variable of type float takes the least amount of memory bytes (4) compared to the other two floating point types. The following table shows the storage sizes, exponent ranges, and digits of precision of the three C++ floating-point data types. Type Size(bytes) Exponent range Precision (digits)------------------------------------------------ float 4-32 to 38 6 double 8-308 to 308 15 long double 10-4932 to 4932 19 Numerical operators and expressions In C++ the symbols used for addition and subtraction are the usual symbols + and- . The symbol for multiplication is * . There are two division operators, whose symbols are / and % . • When the / operator is used to perform division, the data types of the operands can affect the result of the computation. This is explained in the following two cases. • If both operands are integer types, the operator / returns the integer quotient, throwing away the remainder. Therefore 5 / 2 = 2 4 / 5 = 0 • If one of the two operands to the division operator / is of type double , then the operator / does the normal division and the result is of type double . Therefore 5 / 2. = 2.5 2. / 5 = 0.4 • The operator % is called the remainder (or modulus ) operator. When using this operator, both operands must be integer types. This operator returns the remainder (or modulus) of the first operand divided by the second. Therefore, 9 % 2 = 1 8 % 2 = 0 C++ Basics - Part 2 2 Rules of precedence Consider the algebraic expression: 2 + 3 * 4. We know that the value is 14 but not 20, because we must evaluate the expression as 2 + (3 * 4). The reason for first evaluating 3 * 4 is that (as in algebra) multiplication has higher precedence than addition, so multiplication is performed first. Precedence of numerical operators Precedence Numerical operator---------------------------- Higher *, /, % Lower +, - Operations with the same precedence level • If an expression with no parentheses contains two operators of the same precedence, the computer performs those operations in order from left to right. • Operations within parentheses are performed first. We use parentheses when we need to override the normal precedence order. Therefore, if we want the computer to multiply a+b with c , we have to write ( a + b ) * c Likewise, if we want a+b to be divided by c+d , then we have to write ( a + b ) / (c + d) Note that if we write a + b / c + d then it would get evaluated as a + ( b / c ) + d Look at the following expressions and their correct values. We evaluated the expres- sions using the rules of precedence. C++ Basics - Part 2...
View Full Document

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 10

lect2_2 - C++ Basics - Part 2 1 More C++ Basics...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online