Tuesday, March 28, 2017

Math vs Programming

MathTake a look at these two equations. I found the first one on Facebook. The second is my first attempt at rewriting it for the computer. What is the answer?

Well the first answer is 1. The second answer is 9. How is that possible? If you think about it there is a sort of implied parenthesis around 1/3. It’s pretty clear in the more traditional mathematical expression that one is to divide by one third and not 1 divided by three. imageA more accurate expression for the computer would have some parenthesis to make things obvious. That would give one the answer 1 that the traditional expression provides.

We see this sort of thing a lot – students assuming that equations in programming are exactly the same as the mathematical expressions their are used to using.  The most common issue is students thinking that the = sign allows copies from left to right as well as right to left. After all in mathematics the = sign means that both sides of the equation have the same value. That is not the same think as the = sign meaning copy the value on the right into the location on the left.

Some functional languages do things differently. The WeScheme IDE and DrRacket used by Bootstrap for example. That curriculum takes a much more algebra focused look at computer science. Or is that a more computer science focus approach to algebra? Either way it seems to be very effective. (Note that Bootstrap training is available though CSPdWeek and other venues.)

Those of us using other platforms though have a lot to contend with as we teach students how to build expressions that mean what students think they mean. Is it worth the effort? I tend to think so. But it is confusing at times.  Days like to today make me rethink things.


Doug said...

The problem in the original example is that you copied/transposed the first equation character by character. That's similar to a language translator translating an expression word for word. A sentence is more than a collection of words just like a mathematical expression is more than a collection of numbers and operators.

In terms of the "=", it's a matter of pedagogy. If the teacher teaches or allows students to use the expression "is equal to", they're looking for problems. I had CS teachers who used the proper terminology "is assigned" from the first expressions and corrected us if we ever used the term "equal". As you correctly note, that doesn't apply.

For people like us who are long timers in this field, these things seem like second nature. I would assume that you use the proper use in your classroom. I fear for those whose first bit of coding is drag and drop with a language based upon blocks and not necessarily what the blocks actually mean in terms of programming and not just their language. There will come a time when they have to graduate to a more traditional programming language. If they haven't been instructed properly, they'll have problems.

Anonymous said...

Even with proper pedagogy, this is problematic: not every kid can "see through" the syntax, especially english-language learners who may not have the cognitive bandwidth to consider the subtlety between "equals" and "is assigned". And don't forget that the difference between these concepts isn't immediately apparent! It's possible to use an imperative language to do many functional things, so kids who have a misconception won't realize it right away.

Of course, the equals sign is just one problem. You also get into trouble when Java tells you that 1/3 = 0 (was it an integer?), when 1/reallyBigNumber gives you an overflow error (should it have been a double?), or a side-effecting function produces different results depending on how many times it was called. And bear in mind, all of these are just problems for learning programming. Let's not forget that kids also have to learn math, so the risks of confusing these concepts are twofold.

None of this is to say imperative programming is bad. All choices have tradeoffs! But to Alfred's point, sometimes we choose a tool like Python or Javascript without being aware of those tradeoffs, and there be dragons.


Emma Charlotte said...

The fact is that Maths is a boring subject this is why a large number of students get help from assignment writing services to learn about different theories of it.