0% found this document useful (0 votes)
85 views

Chapter 2 (B) - Data Type and Operators

This document discusses various integer and arithmetic data types and operators in C++, including: - Integer types like short, int, long, and unsigned variants - Arithmetic operators like addition, subtraction, multiplication, division, and modulus - How division works differently depending on operand data types - The increment and decrement operators (++, --) - Composite assignment operators like +=, -=, *=, /=, %= and how they work - Examples of using various operators on integer variables
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Chapter 2 (B) - Data Type and Operators

This document discusses various integer and arithmetic data types and operators in C++, including: - Integer types like short, int, long, and unsigned variants - Arithmetic operators like addition, subtraction, multiplication, division, and modulus - How division works differently depending on operand data types - The increment and decrement operators (++, --) - Composite assignment operators like +=, -=, *=, /=, %= and how they work - Examples of using various operators on integer variables
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 2(b) - Data Types

Integer Types
and Operators
Fundamental type
Integral Types
Integer Types
short

int

long

unsigned short

unsigned int

unsigned long
1 2

Arithmetic Operators

 Computer were invented to perform


numerical calculations.
 C++ performs its numerical
calculations of the five ARITHMETIC
OPERATORS (addition, subtraction,
multiplication, and division)
type int can have values in the range -
2147483648 to 2147483647
+ - * / %
3 4

Division Operator
 The division operator returns the quotient.
 However, the value of the quotient depends
on whether at least one of the operands is a
floating point data type.
 For example, the value of 10 / 4 is 2.5.
 However, in C++, the value is 2
 When both operands are an integer or other
whole number data type, then the result is
an integer as well, and the remainder is not
part of the quotient
5 6

 This is true even if the result is assigned to a floating  However, the value of 10.0 / 4 is 2.5 in C++
point variable.
 The output of the following program is 10 / 4 = 2  When at least one of the operands is a
floating point data type, and 10.0 would be
#include <iostream>
using namespace std; interpreted as floating point, then the result
int main(void) is a floating point as well.
{
int firstOp = 10, secondOp = 4;
 The output of the following program is
float result = firstOp / secondOp; 10 / 4 = 2.5 because we changed the data
cout << firstOp << " / " << secondOp << " = " << type of firstOp from int to float:
result;
return 0;
} 7 8
The Modulus Operator
#include <iostream>  The modulus operator also involves division,
using namespace std; but returns only the remainder.
int main(void)  For example, the result of 7 % 2 is not the
{ quotient, 3, but the remainder, 1
float firstOp = 10, result;  The modulus operator works only with
int secondOp = 4; whole number operands.
result = firstOp / secondOp;  The result of an attempt to use it with a
cout << firstOp << " / " << secondOp << " = " <<
floating point operand is undefined.
result;  The result often is a compiler error, but this
return 0; is compiler dependent.
}
9 10

m + n= 54 + 20 = 74
m - n= 54 - 20 = 34
m * n= 54 X 20 = 1080

m / n= 54 / 20 = 2 (should be 2.7 but


integer will ignore decimal point)

2
20 54
m % n= 54 % 20 = 14 40
14
11 12

The Increment (++) And


Decrement Operators (--)
 ‘pre’ version
n=++m;
– Performs the operation (either adding 1 or
Increment m to 45 then
subtracting 1) on the object before the assigns that value to n
resulting value is used in its surrounding
context. n=m++;
assigns 44 to n then only
increase m to 45
 ‘post’ version
– Performs the operation (either adding 1 or
subtracting 1) on the object after the
object’s current value has been used.
13 14

Composite Assignment
Operators
 The standard assignment operator in  New programmers sometimes are
C++ is the equal sign = confused by statements such as total
= total + added
 C++ also include the following
composite assignment  In mathematics, a variable cannot
equal itself plus another number.
+= -= *= /= %=  However, in C++ programming, in
which the = operator is not used for
equality, but instead for assignment.
15 16
 Nevertheless, there also is another way to
express
total = total + added
total += added;
 To the compiler, it makes no difference
 However, many programmers prefer total
+= added, some because it looks more
elegant, others because it seems more
readable, and still others for the practical
reason that it requires less typing.
17 18

Examples:

int m = 6 ; m-=2
m=m-2
m+=4 m= 6-2
m=m+4 m=4
m= 6+4
 m = 10

19 20

n=22
After n+=9 (n=22+9=31), n=31
After n -=5 (n=31-5=26), n=26
After n*=2 (n=26*2=52), n=52

21 22

Floating-Point Types

 C++ support 3 real number type:


– float (4 bytes)
– double (8 bytes)
– long double (8, 10, 12, 16 bytes)

23 24
Type Conversion

 C++ converts integer types into


floating types when they are expected

25 26

Type Casting

 Converting from type integer to type


float can be done automatically
 However, converting from type float/
double to type integer is not automatic
 Therefore, it requires type casting

27 28

Numeric Overflow

 Computers are finite, so the range of


any type must be finite.
 But in mathematics there are infinitely
many integers.
 Consequently, computers are
manifestly prone to error when their
numeric values become too large

29 30

Round Off Error Hidden Round-off Error

 When computer do arithmetic on  This program implements the


rational numbers quadratic formula to solve equations
For Example:



1/3 is not exactly 0.333333
This is the round off error − b ± b 2 − 4ac
x=
 In some cases, these errors can cause 2a
serious problems
31 32
float d = b*b -4*a*c; d = b 2 − 4ac

float sqrtd = sqrt(d); sqrtd = b 2 − 4ac

− b ± b 2 − 4ac − b + b 2 − 4ac
x= float x1 = (-b + sqrtd) / (2*a); x1 =
2a 2a

float x2 = (-b - sqrtd) / (2*a);


− b − b 2 − 4ac
x2 =
2a
33 34

The E-Format for


Floating-Point Values
 Floating-point values may be specified in either:
– Fixed point (0.333333)
– Scientific (3.3e-6, in mathematical format 3.3x10-6)
 Floating point values with magnitude in the
range 0.001 to 999,999 will normally be printed
in fixed point format
 All others will be printed in scientific format
not zero

35 36

37

You might also like