Mathematical Data Operators

Similar documents
2.1 Basics of Functions and Their Graphs

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Chapter 1 Operations With Numbers

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

EC121 Mathematical Techniques A Revision Notes

Expressions and Casting

Learning the Language - V

Solving Equations with Inverse Operations

Variables, expressions and statements

2.Simplification & Approximation

6.1 Evaluate Roots and Rational Exponents

WJEC MATHEMATICS INTERMEDIATE NUMBER STANDARD FORM

Modular Arithmetic. is just the set of remainders we can get when we divide integers by n

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Skill 1: Multiplying Polynomials

Lab 1: Cipher Fundamentals

Exponents. Although exponents can be negative as well as positive numbers, this chapter will only address the use of positive exponents.

Rational and Irrational Numbers

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

Creating a new data type

T H E I N T E R A C T I V E S H E L L

CMSC201 Computer Science I for Majors

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS 101 Randomness. Lecture 21

Introduction to Programming in Turing. Input, Output, and Variables

Lab 1: Cipher Fundamentals

Stage 11 Array Practice With. Zip Code Encoding

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Lecture 3. Input, Output and Data Types

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

Fundamentals: Expressions and Assignment

Manipulate expressions containing surds and rationalise denominators (A*) Able to simplify surds (A)

Square Roots: Introduction & Simplification

Topic 2: Introduction to Programming

SCRATCH MODULE 3: NUMBER CONVERSIONS

Topic 2: Decimals. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

Arithmetic Operations

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Topic 3: Fractions. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

MITOCW watch?v=kvtlwgctwn4

Graphics calculator instructions

printf( Please enter another number: ); scanf( %d, &num2);

Divisibility Rules and Their Explanations

Example: Which of the following expressions must be an even integer if x is an integer? a. x + 5

Bits, Words, and Integers

Radicals and Fractional Exponents

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

Exponents. Common Powers

Properties and Definitions

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Algebra II Radical Equations

Memory Addressing, Binary, and Hexadecimal Review

BITWISE OPERATORS. There are a number of ways to manipulate binary values. Just as you can with

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Get to Know Your Calculator!

SECTION 3. ROUNDING, ESTIMATING, AND USING A CALCULATOR

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VBScript: Math Functions

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

The counting numbers or natural numbers are the same as the whole numbers, except they do not include zero.,

Python Programming: An Introduction to Computer Science

Math 3 Coordinate Geometry Part 2 Graphing Solutions

Modular Arithmetic. Marizza Bailey. December 14, 2015

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Fundamentals of Programming. Week 1 - Lecture 3: Loops

MA 1128: Lecture 02 1/22/2018

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Lesson 6-2: Function Operations

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole

1/11/2010 Topic 2: Introduction to Programming 1 1

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Primality Testing! 1

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

CITS 1401 Problem Solving & Programming

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

(Refer Slide Time: 02:59)

Name: Date: Review Packet: Unit 1 The Number System

Combinatorics Prof. Dr. L. Sunil Chandran Department of Computer Science and Automation Indian Institute of Science, Bangalore

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Math Glossary Numbers and Arithmetic

Congruence Arithmetic

Full file at

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

Section A Arithmetic ( 5) Exercise A

Exponential Notation

The Absolute Value Symbol

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

PROBLEM OF ERRORS IN CALCULATION : A NEW CONCEPT OF DECIMAL

Transcription:

Mathematical Data Operators Programming often requires numbers to be manipulated. This usually involves standard mathematical operators: addition, subtraction, multiplication and division. It can also involve some more complex operations. 1. Adding, subtracting and multiplying These are the easiest operators to use. The symbols used for adding (+) and subtracting (-) are exactly the same as in normal maths. For multiplication we use the symbol * - an x might mean something different.!exercise 1.1 Evaluate the pseudo-code mathematical expressions below, giving the value of answer in each case a) answer ß 7 + 4 b) answer ß 5 + 0 c) answer ß 7-4 d) answer ß 7 * 4 e) answer ß 7 * 1 f) answer ß 7 * 0!Exercise 1.2 If the following pseudo-code is run first, what is the value returned by each of the expressions below. Assume the values are always as initially assigned by the pseudo-code. varone ß 6 vartwo ß 3 varthree ß 2 a) varone + vartwo b) varthree + vartwo + vartwo c) varone - vartwo d) vartwo - varthree e) varthree * varone f) vartwo - varone - 1 -

The basic operators are used in exactly the same way when programming with Python: valueone = 4 valuetwo = 2 answer = valueone + valuetwo print(answer) :Programming exercise 1.1 Write a program which multiplies two numbers together. a) What happens when you make one of the numbers negative? b) What happens when you make both of the numbers negative? c) What happens when you make one of the numbers 0? 2 Division Division is a little more complex because decimal numbers can easily get involved. The operator for division is / answer ß 8 / 2 This expression evaluates to 4 as 8 divided by 2 is 4.!Exercise 2.1 Give the values for answer in each case: a) answer ß 8 / 4 b) answer ß 6 / 2 c) answer ß 7 / 1 d) answer ß 3 / 2 Don't forget that you cannot divide by 0. Any attempt to do something such as: answer ß 8 / 0 will end with an error. - 2 -

:Programing exercise 2.1 Try this program and check the output. # program to show division valueone = 5 valuetwo = 2 valuethree = 8 answerone = valuethree / valuetwo answertwo = valuethree / valueone print(answerone) print(answertwo) You should see that both answers are given as decimals. You would expect this for answertwo 8 / 5 is 1.6 (one and three fifths remainder). But answerone is written as 4.0 rather than simply as 4. This will always happen in Python. Every time you divide two numbers you'll get a decimal number, even if they divide perfectly. There are really good reasons for this and we'll come back to it at another time. :Programming exercise 2.2 # long decimal numbers answer = 8 / 3 The answer is 2 and two thirds. In maths we would usually write this as 2.666 recurring (or 2.667 if we're rounding values). Python deals with long decimal numbers by only going to a set number of decimal places. This can effect the accuracy of calculations as very long numbers will always be rounded. This is something to be aware of when dividing. :Programming exercise 2.3 Write a program which divides a number by 0. What error message do you get? - 3 -

3 The Exponential Operator Using the exponential operator raises a value to a power. This is what happens when you square or cube a number 4 exponential 2 is 4 squared. There is no pseudo-code version of the exponential operator. In Python the symbol ^ is used (shift + 6 on your keyboard). 2^2 is 2 squared - in other words, 2*2 :Programming exercise 3.1 # program to square and cube numbers varone = 2 vartwo = 4 print(varone ^ 2) print(vartwo ^ 3) print(vartwo ^ varone) Answers are provided for this programming exercise. 4 Integer Division Sometimes you want the whole number that results when you divide two numbers, ignoring any decimal values or remainders. This is known as Integer Division. The pseudo-code operator for integer division is DIV. answer ß 3 DIV 2 gives a value for answer of 1 The value returned when the DIV operator is applied is the whole number without any remainder. The expression 3/2 would return a value of 1.5. When DIV is used any remainder is discarded and the value 1 is returned. answer ß 8 DIV 3 gives a value for answer of 2. 8/3 is 2.667, so the remainder is discarded and the whole number returned. DIV always gives just the whole number. - 4 -

!Exercise 4.1 Give the values returned by each of the following: a) 7 DIV 4 b) 8 DIV 2 c) 9 DIV 4 d) 15 DIV 2 e) 7 DIV 7 f) 3 DIV 8 Integer division can seem confusing to begin with. You need to remember to just get rid of any remainder and always round down to the lower whole number. In Python, integer division is handled by using the symbol // :Programming exercise 4.1 Try this program # integer division program varone = 7 vartwo = 3 varthree = 2 print(varone / vartwo) # dividing normally print(varone // vartwo) # integer division print(varone / varthree) # dividing normally print(varone // varthree) # integer division print(varthree // varone) # integer division Answers are provided for this programming exercise. - 5 -

5 The Modulus Operator Sometimes you want just the remainder from division. This is handled by using the modulus operator. In pseudo-code this uses the operator MOD. answer ß 7 MOD 4 7/4 would be 1.75, or 1 remainder 3. MOD returns just the remainder value, so 7 MOD 4 evaluates to 3 This will probably seem strange to begin with answer ß 3 MOD 2 5/2 = 2.5 - or 2 remainder 1. So, 3 MOD 2 evaluates to 1 The trick to using MOD is to get out of the habit of using decimals and think about remainders instead. When you first learned to divide numbers that s what you did. It s only later that people have made things more complicated by using decimals.!exercise 5.1 Evaluate the following expressions. a) 7 MOD 2 b) 8 MOD 3 c) 9 MOD 4 d) 5 MOD 3 e) 6 MOD 2 2 MOD 5 is interesting 2 / 5 = 0.4. This means that 2 goes into 5 0 times with ⅖ remainder. So the remainder is 2. So 2 MOD 5 evaluates to 2. Whenever the number to the right of MOD is greater than the number to the left, the answer is the number on the left. 2 MOD 9 = 2 5 MOD 7 = 5 324 MOD 398 = 324-6 -

!Exercise 5.2 Evaluate each of a) 5 MOD 6 b) 9 MOD 9 The Modulus operator allows you to check if a number is perfectly dividible by another number. If this is the case then the answer to valone MOD valtwo is 0. This is surprisingly helpful when you get into more complex programming problems. In Python the operator % is used to signify MOD. :Programming exercise 5.1 # program using modulus division varone = 7 vartwo = 3 varthree = 2 print(varone % vartwo) print(varone % varthree) print(varthree % varone) Answers are provided for this programming exercise. 6 User Inputted Numbers All numbers entered by users in pseudo-code or in Python are initially stored as strings. This causes a problem when we want to do maths with a value as computers will think of "42" as a sequence of keyboard characters as a string. If we try and add "72" to it the computer will just concatenate the values. This means that "42" + "72" evaluates as "4272" as a string with the two numbers joined together. To deal with this we need to convert strings into numbers before we do anything mathematical with them. This can be done in both pseudo-code and in Python. - 7 -

We will assume that all numbers are going to be entered as whole numbers as integers. The pseudo-code to do this is: valueone ß USERINPUT valueone ß STRING_TO_INT(valueOne) In Python we can include the conversion on the same line as the data input: valueone = int(input("enter a number: ")) The int() command converts the string entered at input into an integer. :Programming exercise 6.1 Write a program which asks the user to enter two numbers. The program should then multiply the two numbers together and report the answer. Solutions to exercises Exercise 1.1 a) 11 b) 5 c) 3 d) 28 e) 7 f) 0 don't forget that if you multiply by 0 you get 0 Exercise 1.2 a) 9 b) 8 c) 3 d) 1 e) 12 f) -3 minus numbers will work just fine Exercise 2.1 a) 2 b) 3 c) 7 d) 1.5 Programming exercise 3.1 varone ^ 2 is 2 ^ 2 or 2 squared (2 * 2) = 4 vartwo ^ 3 is 4 ^ 3 or 4 cubed (4 * 4 * 4) = 16 * 4 = 64 vartwo ^ varone is 4 ^ 2 or 4 squared (4 * 4) = 16 Exercise 4.1 a) 1 b) 4 c) 2 d) 7 e) 1 f) 0 3/8 is less than 1, so the value is rounded down to 0 Programming exercise 4.1 varone / vartwo = 2.333 varone // vartwo = 2 drop the remainder varone / varthree = 3.5 varone // varthree = 3 drop the remainder varthree // varone = 0 drop the remainder Exercise 5.1 a) 1 the remainder is 1 b) 2 the remainder is 2 c) 1 the remainder is 1 d) 2 the remainder is 2 e) 0 the remainder is 0 as 2 goes perfectly into 6 Exercise 6.2 a) 5 the value to the right is larger, so use the value to the left b) 0 9 goes into 9 once with no remainder Programming exercise 5.1 varone % vartwo = 1 the remainder is 1 varone % varthree = 1 the remainder is 1 varthree % varone = 2 varthree is smaller than varone - 8 -