Math Is Fum (Yes, Fum!)

On an episode of the show "SpongeBob SquarePants", SpongeBob's friend Patrick was a marketing consultant for The Chum Bucket, a restaurant owned by Plankton. Patrick is, let's say, simple-minded; he came up with the slogan "Chum is Fum", misspelling "fun". But the fish in Bikini Bottom loved the slogan and the catchy play on words, and Plankton enjoyed the temporary success of Patrick's accidental marketing know-how.

In an attempt to get the kids to read about math, I have decided to call this series of blog postings “Math is Fum”. We’ll see if that works.

Decimal First
We normally do math in Base-10, or decimal, meaning we generally organize our numbers using powers of ten when we write numbers and do math.

A standard way to break up a large number is to separate the values based on their position from right to left – the ones place, the tens place, the hundreds place, and so on. For example, 123 = 100 + 20 + 3.

Another way to say it, using powers of ten, is

123 = (1 x 10^2) + (2 x 10^1) + (3 x 10^0)

Two notes:
1) The ‘^’ symbol means to raise 10 to that power. For example, 10^2 means 10 squared, or 10 to the second power.
2) 10^0 = 1. Ten raised to the zeroth power equals 1. In fact, any number raised to the zeroth power = 1, by definition.

Another example:

18,903 = (1 x 10^4) + (8 x 10^3) + (9 x 10^2) + (0 x 10^2) + (3 x 10^0)

Math gurus will point out the parentheses in this example are unnecessary due to operator precedence rules, but they’re just showing off. It’s easier for people to read it with the parentheses.

At their core, computers don’t know anything about decimal math. Mathematical calculations on a computer are done in binary math, and converted to decimal numerals for display or storage.

So let’s look at how binary numbers work.

Binary Is Your Friend
In decimal math, there are 10 digits that are used: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

In base-2, or binary, math, there are two digits: 0 and 1.

Here is the binary representation of the number eleven:

1011

How can only two digits (0 and 1) represent a number like eleven? We can write the binary number 1011 like this:

1011 = (1 x 2^3) + (0 x 2^2) + (1 x 2^1) + (1 x 2^0)

While we have the ones place, the tens place, and the hundreds place in decimal math, binary has the ones place, the twos place, the fours place, and so on instead. It takes a lot more space in binary to write a large number than it does to write a large number in decimal.

Exercises (Yes, you need to do these.)
Exercise A: Convert the following binary number to decimal: 1001101
Exercise B: Write the following decimal number in binary: 24

Octal, or Base-8
Let’s move on to something different. You should be able to follow this if you understand the above discussion of binary numbers.

In computers we sometimes work with numbers expressed in octal, or base-8. In octal we use (you guessed it) 8 digits: 0, 1, 2, 3, 4, 5, 6, 7.

Exercise C: What is the decimal value of the octal number 447?

Hint: Write it out using powers of 8 as in the examples above for binary and decimal.

Hexadecimal Numbers

Programmers also occasionally work with numbers that are expressed in base-16 (also known as hexadecimal). There is a problem with this, though; there are only ten digits available to use, but we need 16 “digits” to express a hexadecimal number.

To get around this, we use letters to denote the remaining 6 digits. Counting to 15 in hexadecimal looks like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. So F means “15” as far as we’re concerned. A is the tenth hexadecimal “digit”.

Here is an example of a hexadecimal number:

A2FA = 10 x 16^3 + 2 x 16^2 + 15 x 16^1 + 10 x 16^0

In base-10, that’s 41,722. (is that what you got?)

Software developers typically denote a hexadecimal value with the text “0x” (zero x), as in “0xAAA” so that people who read their writing will not confuse the letters “AAA” (insurance company) with the numeric hexadecimal value of AAA (2,730).

Computers Don’t Do Decimal
One problem with computers using binary numbers is that it can be difficult to exactly convert a decimal number to a binary number, then back again. Anyone who has used Google to translate an English phrase to Japanese and back to English will understand.

For example, let’s say you’re in an Excel spreadsheet and you type in this number:

12.33998844

We humans recognize that number in its decimal format. But the computer stores this number in a binary format. To do that, it converts the value into binary. (I won’t go into the details of that, but computers have ways of breaking numbers like this into parts and storing them.)

A problem occurs when this number needs to be converted back to decimal. Because the value stored in the binary format cannot always be exactly translated back into decimal, the computer may end up telling Excel that the value that was stored was

12.33998843 (note the last digit is not the same as above.)

This is a common problem when working with numbers with lots of numbers after the decimal point. Programs like Excel know how to deal with these things, and payroll programs know how to round up or down to pennies to get the correct value each time (at least they should.) Even with modern programming languages, it is still possible to get into trouble with high-precision numbers if you’re not careful. That’s why I only work with nice, whole numbers. It’s in my contract.

Hope you enjoyed this edition of Math Is Fum! Let me know in the comments if you need the answers to the questions. You all should be able to figure them out, though.

No comments:

Post a Comment