Math Expressions

Read time: 15 minutes (3879 words)

We need to review what we learned about how computers do math, to make sure you have the ideas down right. We have discussed this in our last lecture. Make sure you see what is happening.

In your last homework, you did some research into terms we use to describe how computers store data. Each of those containers stores data encoded according to the rules set up for the kind of data we will store. When we do math, the computer needs to check the data (and its encoding) and convert things so the math makes sense.

This helps explain how computer math works, and it is not quite the same as human math. We need to practice with this a bit, even before we start using a real programming language.

Expressions

You should be familiar with these things. You take a bunch of “terms” and place math “operators” between them to explain what kind of math you want. Here are some examples:

  • 5 + 2

  • 3.0 / 2

  • 2 + 2 + 2 / 3

The question we need to explore is what do all those “expressions” “evaluate” to? Each one needs to be run through our computer’s calculator to see what final number we get.

The huge new thing we need to understand is that we are using two completely different kinds of numbers here:

  • Integer numbers - which cannot have a fractional part

  • Floating point numbers - which do (well, might) have a fractional part

These two kinds of numbers do not mix, and cannot be combined together in a math operation without converting things to the same kind of number.

Da Rulz

  1. Integer only expressions never produce floating point results!

Warning

Some of you will blow questions on an exam by not learning this. Whenever you have two things on either side of any arithmetic operator, that rule applies. If both numbers are integers, you will NEVER generate a fractional result. It will not be rounded either, just chopped off!

Here is a classic example:

  • What is 5 / 2?

Most of you will say 2.5. WRONG! The answer is 2!

Why?

There is no fraction in the result, because both numbers are integers. Those are displayed with no decimal point. They are called “whole numbers”. This is the math you learned in elementary school:

How many times does 2 fit into 5?

The answer is 2. And you have one left over!

Note

The “remainder after division is actually used in computing. The operator that gives you that remainder is called the “modulus” or “mod” operator. We will explore that later.

More Rulz

  1. Different types of numbers do not mix!

What is 5.0 / 2?

Here, we have a floating point number divided by an integer. What should the computer do? The answer is to convert the integer to a floating point number, then do the math. This time we get 2.5, like you thought you would earlier!

Operator Precedence

There is one aspect of computer math that takes some practice to get right.

Every operator has a “precedence” level, which determines which operator to apply first. In normal, four operator math, the multiplication and division operators have a higher precedence than addition and subtraction. Here are the levels:

  • multiply and divide are equal

  • add and subtract are equal

Here is how we process a more complex expression:

  1. Scan the expression left to right.

  2. Find the highest precedence operator in the expression (left-most if several are equal)

  3. “reduce” the expression by applying that operator to the two numbers on either side.

  4. Rescan left to right and repeat the process.

Here is an example:

  • 5 / 3 + 2 * 3 - 5 / 2

The steps in reducing this to one number follow:

  • 1 + 2 * 3 - 5 / 2

  • 1 + 6 - 5 / 2

  • 1 + 6 - 2

  • 7 - 2

  • 5

By the way. If any of those numbers was a floating point number, the whole thing becomes a floating point problem and we get a fractional result! Phew!

One More Rule!

Parentheses! You remember these things. They let you group a part of an expression. We can use those to force things to be processed the way we want, not the way operator precedence ends up doing things!

What would this produce?

  • 5 / ( 3 + 2 ) * ( 3 - 5 ) / 2

Same numbers, same operators, but a completely different result.