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

PF-Operators 5

The document discusses arithmetic operators and expressions in C++. It defines binary arithmetic operators like addition, subtraction, multiplication, and division. It also covers integer division, the remainder operator, and upcasting integers to floating-point. The document explains the order of operations and unary operators like negation. It provides examples of arithmetic expressions and notes they will evaluate to integers or floating-point values depending on the operands. Simulations using mathematical models and arithmetic are also briefly mentioned.

Uploaded by

muhammad shoaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

PF-Operators 5

The document discusses arithmetic operators and expressions in C++. It defines binary arithmetic operators like addition, subtraction, multiplication, and division. It also covers integer division, the remainder operator, and upcasting integers to floating-point. The document explains the order of operations and unary operators like negation. It provides examples of arithmetic expressions and notes they will evaluate to integers or floating-point values depending on the operands. Simulations using mathematical models and arithmetic are also briefly mentioned.

Uploaded by

muhammad shoaib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PROGRAMMING

FUNDAMENTALS
Dr. Sheikh Faisal Rashid
shfaisal@gmail.com,uet.edu.pk
Department of Computer Science and
Engineering UET Lahore
ARITHMETIC
OPERATORS
• Define binary arithmetic operators
• Addition, subtraction, multiplication and division
• Integer division
• Remainder/modulus operator
• Upcasting integers to floating-point
• Order of operations
• Upcasting and order of operations
• Unary arithmetic operators
• Negation and “+”
Arithmetic operations
• Most engineering computations involve simulations of the
real world, requiring the application of mathematics and
modelling
• The A380 double-decker jumbo jet was simulated entirely in
software prior to being built for the first time…
• Processors and circuits are simulated using mathematical models
Arithmetic operations
• A binary arithmetic operator takes two numerical
operands and returns the result of the operation
• The operands may be integers or floating-point
• We are restricted to the non-alphanumeric characters on the
keyboard
• The available binary arithmetic operators are
Operation Operator Integer Floating-
s point
Addition + 3 + 5 3.2 + 7.3
Subtraction – 7 - 6 9.5 - 4.1
Multiplication × 8*9 1.5*2.7
Division ÷ 1/2 4.5/9.6
Note: For clarity, it is usual to place spaces around + and -
but no spaces around * or /
Arithmetic operations
• Juxtaposition is never acceptable to represent multiplication

2 x 2  3 xy  4 y 2 2*x*x - 3*x*y + 4*y*y

• If you entered 2xx - 3xy + 4yy, this would result in the


compiler signalling an error

• 2xx is neither a valid integer, floating-point number or identifier

• Exponentiation is not represented as an operator


• Exponentiation is a very expensive and difficult operation
• The C++ language leaves libraries to implement this as a function
Integer division
• In C++, the result of an arithmetic operation on integers
must produce an integer
• This is a problem for division

std::cout << (1/2) << std::endl; // outputs 0


std::cout << (7/3) << std::endl; // outputs 2
std::cout << (-11/4) << std::endl; // outputs -2
std::cout << (-175/-13) << std::endl; // outputs 13
• The result is the quotient discarding any remainder

175 6 534 3
 13   35 
13 13 15 5
Integer remainder
• To find the remainder of a division, use the modulus
operator
• Also called the remainder operator

std::cout << (1 % 2) << std::endl; // outputs 1


std::cout << (7 % 3) << std::endl; // outputs 1
std::cout << (-11 % 4) << std::endl; // outputs -3
std::cout << (-175 % -13) << std::endl; // outputs 6

• For any integers m and n, it is always true that


(n/m)*m + (n % m) equals n
Integer remainder
• Let’s take a closer look at:
(n/m)*m + (n % m)

• Don’t we know from mathematics that as long as m ≠ 0,


n
m  n ?
m

• C++ evaluates one expression at a time


• It doesn’t look ahead, it doesn’t try to optimize, it simply performs
the operation you specified
• If the compiler sees (7/3)*3,
• It first will have (7/3) calculated, which evaluates to 2
• It then proceeds to calculate 2*3 which is 6
Upcasting
• Suppose the compiler sees:
3.2/2
• Does it use floating-point division, or integer division?
• The only way for this to make sense is for the compiler to interpret the
2 as a floating-point number
• We will see later that this requires a completely different internal
representation
00000000000000000000000000000010
0100000000000000000000000000000000000000000000000000000000000000
• If you think you see a pattern, you’re wrong; this is 3:
00000000000000000000000000000011
0100000000001000000000000000000000000000000000000000000000000000
and this is 1:
00000000000000000000000000000001
0011111111110000000000000000000000000000000000000000000000000000
• This process is called upcasting
• Literals are upcast by the compiler
Order of operations
• The compiler uses the same rules you learned from
secondary school:
• Multiplication and division before addition and subtraction
• In all cases, going from left to right

• Try the following:


std::cout << (1 + 2 + 3 + 4 * 5 * 6 + 7 + 8 + 9);
std::cout << (1 * 2 * 3 + 4 * 5 * 6 + 7 + 8 + 9);
std::cout << (1 * 2 * 3 * 4 * 5 + 6 + 7 + 8 + 9);
std::cout << (1 * 2 * 3 * 4 * 5 - 6 * 7 + 8 * 9);
std::cout << (1 / 2 + 3 * 4 + 5 * 6 * 7 – 8 * 9);
std::cout << (1 + 2 * 3 * 4 / 5 * 6 * 7 * 8 / 9);
std::cout << (1 * 2 + 3 + 4 * 5 * 6 / 7 * 8 + 9);
Order of operations
• Parentheses can be used to enforce the order in which
operations are performed

• Common mistakes include


k/m*n when they mean k/(m*n) or k/m/n
k/m+n when they mean k/(m + n)
k+m/n when they mean (k + m)/n
Order of operations and upcasting
• Again, C++ is very exact when upcasting occurs:
• Only when one operand is a floating-point number and the other is
an integer is the integer upcast to a floating-point number

• What is the output of each of the following? Why?


std::cout << (10.0 + (1 / 2) * 3.0);
std::cout << (10.0 + 1 / 2 * 3.0);
std::cout << (10.0 + 1 / (2 / 3.0));
Unary operators
• There are two unary operators for arithmetic:
• Unary negation operator changes the sign of what follows:
std::cout << -(1 + 2 + 3);
std::cout << -(2*3*4);
std::cout << -(1 + 2*3);

• Unary neutral operator leaves the sign unchanged:


std::cout << +(1 + 2 - 5);
std::cout << +(-2*3*4);
std::cout << +(1 - 2*3);
Arithmetic expression
• Up to this point, we may say an arithmetic expression is either:
1. A numeric value (integer or floating point):
…, -3, -2, -1, 0, 1, 2, 3, …
2. The chaining of two or more arithmetic expressions with addition or
multiplication:
(arith-1) + (arith-2) + (arith-3)
(arith-1) * (arith-2) * (arith-3)
3. The difference or ratio of two arithmetic expressions:
(arith-1) - (arith-2)
(arith-1) / (arith-2)
4. The negation of or applying the unary + operator to an arithmetic
expression:
-(arithmetic-expression)
+(arithmetic-expression)
Arithmetic expression
• We can now make the following statements:
• An integer arithmetic expression will always evaluate to an integer

• We can make an identical description of floating-point


arithmetic expressions
Summary
• Following this presentation, you now:
• Understand the binary arithmetic operators in C++
• Addition, subtraction, multiplication and division
• The effect of integer division and the remainder operator
• Upcasting integers to floating-point
• Understand the order of operations and upcasting
• The two unary arithmetic operators
References
[1] Thomas McConkey, a simulation of a 6 GHz microwave signal
transmitting through a coaxial pogo pin onto a micro-coplanar
waveguide transmission line of a thin film superconducting
aluminium (i.e., a quantum socket). Developed with the Ansys
software HFSS.
[2] Wikipedia,
https://en.wikipedia.org/wiki/Operators_in_C_and_C++#Arithmetic_operators
[3] cplusplus.com tutorial,
http://www.cplusplus.com/doc/tutorial/operators/
[4] C++ reference,
https://en.cppreference.com/w/cpp/language/operator_arithmetic

You might also like