28.05.2017 Views

APC_Australia_Issue_442_June_2017

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

howto » coding masterclass<br />

Doing maths with Python<br />

Not great with numbers? Don’t worry, Python is. Darren Yates delves<br />

into Python’s module of mathematical functions to begin solving<br />

almost any problem.<br />

There’s a lot of talk about<br />

STEM (Science, Technology,<br />

Engineering, Mathematics)<br />

education in schools at the<br />

moment. A recent Federal<br />

Government report found that school<br />

enrolments in these subjects are at<br />

20-year lows (tinyurl.com/lxduqto).<br />

Given how crucial these areas are to<br />

our future, the report is alarming.<br />

If we had to pick which of these<br />

areas was most important, we think<br />

the choice is easy — mathematics.<br />

Why? Without mathematics, the other<br />

three areas couldn’t exist (tinyurl.com/<br />

lywfnro). But while maths isn’t<br />

everyone’s cuppa, it’s something<br />

computers excel at and Python is<br />

loaded with functions and modules<br />

designed to help solve practical<br />

maths problems.<br />

THE ‘BIG FOUR’<br />

Starting at the very beginning,<br />

the four basic maths functions<br />

or ‘operators’ are addition (+),<br />

subtraction (-), multiplication (*)<br />

and division (/). You can try these out<br />

in the Python Shell:<br />

print(2+5)<br />

print(18/3)<br />

print(15.2-6.4)<br />

print(5.4*3)<br />

ARITHMETIC PRECEDENCE<br />

You don’t have to be on LinkedIn,<br />

Twitter or Facebook for long before<br />

you get the inevitable maths<br />

brainteaser quiz that goes something<br />

like this:<br />

4 + 10 / 2 – 2 x 3 = ?<br />

Getting the right answer relies on<br />

knowing what’s called ‘arithmetic<br />

precedence’ or ‘order of operations’<br />

— either way, it’s the order in which<br />

the operators must be calculated.<br />

That basic order is division (/) and<br />

multiplication (x), followed by<br />

addition (+) and subtraction (-).<br />

You can create a Direct<br />

Digital Synthesis table<br />

with Python code.<br />

Get Python<br />

Grab the latest version of Python<br />

for your operating system<br />

(Windows, Linux, Mac OS X) from<br />

www.python.org/downloads<br />

We’re using the latest version 3.x, so<br />

choose that rather than version 2.x.<br />

You may even remember ‘BODMAS’<br />

from school — Brackets, Orders, then<br />

Division, Multiplication, Addition,<br />

Subtraction as before.<br />

In this example, we start with 4,<br />

but instead of adding ‘10’, we have to<br />

calculate the division operator first,<br />

so 10/2 = 5. Add the ‘5’ to the ‘4’ and you<br />

get 9. Continuing on, we can’t subtract<br />

the ‘2’ as the following multiplication<br />

operator takes precedence, so we get<br />

2 x 3 = 6. Now we subtract the ‘6’ from<br />

‘9’ and get 3. Try this out in Python<br />

Shell:<br />

print (4 + 10 / 2 – 2 * 3)<br />

And you should end up with ‘3.0’.<br />

COMPUTERS CAN’T ADD UP?<br />

We give computers a lot of credit<br />

for working out all sorts of problems,<br />

but there are limitations — and those<br />

limitations are easier to see than you<br />

might expect. We store numbers in<br />

variables of a particular datatype —<br />

whole numbers or ‘integers’ are stored<br />

in ‘integer’ variables, decimal or ‘real’<br />

numbers are stored in ‘floating-point’<br />

variables.<br />

Fire up Python Shell again and try<br />

out the following:<br />

106 www.apcmag.com<br />

Calculate the 3dB<br />

cut-off frequency of an<br />

RC filter using the Math<br />

module.<br />

print(0.1 + 0.1 + 0.1 + 0.1 +<br />

0.1 + 0.1 + 0.1 + 0.1 + 0.1 +<br />

0.1)<br />

The answer you’d expect is ‘1.0’.<br />

The answer you’ll get is<br />

‘0.9999999999999999’, which<br />

obviously isn’t the same thing. Now<br />

instead of assuming computers are<br />

stupid and can’t add up, the reason for<br />

this has to do with the way computers<br />

store real numbers. The basic problem<br />

is that, while we talk and think in<br />

decimal numbers (factors of 10 or

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!