### CS12 : Why are we doing maths? Computer are really good at maths. They do maths really quickly. Most of the maths that computers do is ordinary maths like we do. See?

We are learning ...
• How to perform mathematical operations using computers / programming
So that we can ...
• Use familiar arithmetic operators - addition, subtraction, multiplication
• Apply the two types of division - real / float division and integer division
• Use exponentiation
• Use incrementation and decrementation operators
• Apply precedence rules and use brackets to alter them
• Use rounding and truncation in appropriate circumstances
• Use the DIV and MOD operations and describe their application in programming

 Activity 1 Mathematical operators (20)

OK - I know it may seem simple, but you need to know them.  Some of these are Python specific.  Task 1.1 Practicing some maths IDLE Programming Environment Try out the operations above and then practice creating some of your own examples. OUTCOME : Documentary evidence of the examples of mathematical operators you have created. Activity 2 Incrementation, decrementation and other assignments (30)

A standard assignment operator (=) will perform only assignment, not maths.  However, there are a number of equivalent assignment operators which perform shorthand mathematical functions.

Of these, undoubtedly the most important are the Add AND (incrementation) and the Subtract AND (decrementation) assignment operators. Incrementation means 'adding to'. Decrementation means 'subtracting from'. Incrementation and decrementation operations are often carried out in programming during loops to alter the value of a counter variable.

Remember that you can only increment or decrement a variable that is already declared! (In fact, this is true of any of the assignment operators). Task 2.1 Up and down IDLE Programming Environment Brain First, try the following examples at the console.  Try changing the incrementation / decrementation value and seeing what happens. ```    >>> counter = 0     >>> print(counter)     0     >>> counter += 5 # increment by 5     >>> print(counter)     5     >>> counter -= 3 # decrement by 3     >>> print(counter)     2``` Next, download ChipsDown.py from the resources for the lesson and play the game with another student.  Your challenge is to create a new script based on this one called ChipsUp.py which works in the opposite way - where the players have to add chips to the pile. The winner would be the one who adds the 21st chip. OUTCOME : Provide evidence of your completed ChipsUp.py script and also proof of the output of the script. Activity 3 Precedence (15)

Some mathematical operators are more 'important' than others and take precedence over others. You will be familiar with this from your work in Maths. In Python, the precedence rules are ... Notice that there are certain operators which have the same precedence as each other and are evaluated from left to right in this list. As always, however, it is the brackets that have the ultimate precedence and are able to change the precedence of any expression they surround.

Consider the following evaluation tree ... Task 3.1 Mathematical challenge Nothing Consider the following mathematical expression ... ```    >>> 15 // 4 + 9 ** 2 / 12 - 9 % 4 * 2     7.75``` Using the precedence rules, construct an evaluation tree to explain why this evaluates to `7.75`. OUTCOME : Step by step mathematical proof of operator precedence using an evaluation tree. Activity 4 Rounding and truncation (25)

Rounding

Like rounding in Maths, Python allows you to round digits to certain decimal places using the `round()` function.
```    >>> round(3.1415927,3)
3.142```
Truncation

Truncation is where you shorten a value by cutting off after a certain number of digits. This is the equivalent to rounding towards zero. There are two ways of performing truncation in Python.

Method 1 : String formatting
```    >>> d = 3.1415927
>>> p = 3
>>> t = float('{:.{precision}s}'.format(str(d),precision=str(d).find('.')+1+p))
>>> print(t)
3.141```
Method 2 : The multiply and divide method
```    >>> d = 3.1415927
>>> p = 3
>>> t = float(int((d)*(10**p))/(10**p))
>>> print(t)
3.141```
Neither are ideal but they work! There isn't currently a built in method in Python to perform truncation. In practical terms, truncation can be used in quantisation like sound sampling. Task 4.1 Truncate IDLE Programming Environment Try out the provided and your own examples for round and truncate methods.  Can you explain how the two truncation methods work? OUTCOME : Evidence that you have run the rounding and truncation scripts and also that you have explained how the truncation methods work in your own word. Extension Activities

There is one very common use for the modulo (%) operator.  Find out what it is and try to give an example of it in use. HINT : Search for 'Use of modulo operator' in Google.

 What's next? Before you hand your book in for checking, make sure you have completed all the work required and that your book is tidy and organised. Your book will be checked to make sure it is complete and you will be given a spicy grade for effort.

END OF TOPIC ASSESSMENT