Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numerical Functions & Tricks

Similar presentations


Presentation on theme: "Numerical Functions & Tricks"— Presentation transcript:

1 Numerical Functions & Tricks
In today’s lesson we will look at: why we might want to use mathematical techniques some useful functions other mathematical ideas

2 Why Manipulate Numbers?
Computers are better at handling whole numbers (integers), and you can easily get rounding errors when working with long decimal numbers. You might need to write a program that performs a calculation – e.g. for a bank or insurance company You can use mathematical techniques for more interesting things, such as motion, or for cycling through a limited number of colours (like on the Spots theme for the Ill Health Team web-site)

3 Useful Operations Modulo arithmetic: performs a division and gives you the remainder – e.g. 10 % 3 returns 1 Modulo arithmetic is useful if you want to cycle through a fixed number of options, e.g. colour = (“Red”, “White”, “Blue”) for n in range(20): print(str(n) + “ – “ + colour[n%3])

4 Useful Operations Powers/Indices: to raise a number to a power, we use the ** symbols For example, 4**3 would calculate 43 There is no native square root function, but if you remember your indices work from Maths, you can use negative numbers and fractions, e.g. 25**0.5 would give you the square root.

5 Useful Functions abs(): gives you the absolute value of a number - i.e. without the sign. For example, abs(-1) would return 1 float(): gives you the numerical value of a string, e.g. float(“2.5”) would return the value 2.5 as a float. int(): gives you the integer value of a string, e.g. int(“3”) would return the value 3 as an int. str(): turns a number into a string, e.g. str(10) would would return a str containing “10”. eval() evaluates the expression in a string, e.g. eval(“2+3”) would return 5 (as an int)

6 Rounding round() rounds a number in the same way that you would in Maths You can round to a specified number of decimal places – e.g. round(1.2345,0) would give 1 round(1.2345,1) would give 1.2 etc.

7 Tricks & Tips You can change the sign of a number by multiplying by -1
You can use this idea to make things bounce – e.g. multiply their speed by -1 so that they go the other way, Computers are much better at working with whole numbers, so for things like money it is better to work in pence, and just divide by when you want to display the answer

8 Rounding Errors When performing floating point operations on computers, you quite often get strange results: This isn’t a bug in JavaScript – Python will do the same – it’s the way the computer handles floating point numbers. It’s better, therefore, to avoid floats where possible – e.g. if you are writing a program to handle money, do all of your calculations in pence rather than pounds.

9 Further Reading Boolean Operators – AND, OR, NOT, EOR
Binary – representing numbers using only 0s and 1s Hexadecimal – used to describe colours Bitwise Boolean Operators: these take the Boolean Logical Operators and apply them to individual bits in the binary representation of numbers – this is how the binary flags page works.


Download ppt "Numerical Functions & Tricks"

Similar presentations


Ads by Google