Natural Science - Year II

#### Course Materials are always under revision! Weblecture content may change anytime prior to two weeks before scheduled chat session for content.

Science Weblecture for Unit 66

 This Unit's Homework Page History Lecture Science Lecture Lab Parents' Notes

# Science Lecture for Unit 66: Information Science and Computers

### For Class

• Topic area: Information Science
• Terms and concepts to know: computer, program, algorithm, URL, Internet, World-Wide Web
• See historical period(s): Information Science

## Information Science

In order for a computer to perform any activity, we need to have a way to talk to it. We need to know how computers actually store information (binary code), how chips talk to computers (assembly code), and how computer languages talk to assembly code ( "a = 2a;").

#### Binary Code and Chips

Computers use chips or hard-coded, built-in instruction sets that allow them to perform operations on values stored in binary code in specific locations in memory (called registers).

Machine code is binary code, represented by a string of 1s and 0s to make up numbers. In base ten, we use the rightmost value in a number to represent the number of units (1s) we have, the next value to the left represents the number of 10s we have, the next number to the left represents the number of 100s...and so on. In base two (binary), we use the rightmost value in a number to represent the number of units (1s) we have, the next value to the left represents the number of 2s we have, the next number to the left represents the number of 4s...and so on. As a shorthand, computer engineers sometimes write values in hexadecimal, a number system based on 16.

 Base 10 Place Values 103 102 101 100 Value Base 10 Example 4 6 7 2 4 * 103 + 6 * 102 + 7 * 101 + 2 * 100 4 thousands + 6 hundreds + 7 tens + 2 ones 4672Highest possible value: 9999 Base 2 Place Values 23 22 21 20 Value Base 2 Example 1 0 1 1 1 * 23 + 0 * 22 + 1 * 2 1 + 1 * 20 1 eights + 0 fours + 1 two + 1 one11Highest possible value: 15 Hexadecimal Values 161 160 Value Hexadecimal 7 F 7 * 16 1 + [F=15] * 160 7 sixteens + 15 ones112+ 15 = 127Highest possible value (in 2 digits): 255

For people, binary code is incredibly cumbersome, but machines which use electrical signals or magnetic fields that are either "on" or "off" can easily use binary representation of numbers.

Computer chips, such as the 8080 chip, which was the original one used in personal computers, the Intel chip, and the RISC chip, map specific binary values to a particular instruction. In Z80 code, the Hex instruction "87" (8*16 + 7 = [base 10] 135 = [binary] 10000111 means "take the value in register A, add it to itself, and store the result in register A".

This brings us the interesting problem of "assignment", which is a basic part of programming. The expression

a = 2a

it makes no sense algebraically. It's a logical contradiction for any value for a other than 0. But on a Z80 programming, the BASIC language instruction "a = 2a" would be interpreted as bit instruction 87 -- take the value in location a, add it to itself, and stick the result back in location a.

Take a look at the instruction set for the 8080/Z80 chip at the Nemesis site. This is a collection of information on 8080 assembly language, which is still used on older computers.

• What does instruction "3D" do?
• How many registers do you think there are?

#### Algorithms and Programs

Consider the following problem: You don't have a computer or calculator handy, but you need to know the square root of 734. There's way to find square roots that was used in the dark ages before calculators:

 We first make a guess -- any number will do. Let's make an educated guess of 25, since the square root has to be at least 25 (252 = 625) Divide our square by our guess. 734/25 = 29.36 Now we take the average of the difference between our first guess (25) and this result (29.36) (29.36 + 25)/2 = 27.18 We use this for our new "guess" 734/27.18 = 27.005 Now we take the average of the difference between our this guess (27.18) and this result (27.005) (27.18 + 27.005)/2 = 27.09 We use this for our new "guess" 734/27.09 = 27.09

We just got the same answer, so at two decimal places, the square root of 734 is 27.09. If we square 27.09, we get 733.86, which is 0.14/734 or 0.00018. That means our estimate is off by less than 0.02%. It isn't mathematically exact. If we had kept better track of the values to more decimal places we could keep going and get a more accurate value for √(734).

What we do have is an algorithm or method.

1. Make a guess equal to the square root of the larges perfect square less than our square.
2. Divide the square by the guess and record the result.
3. Take the average of the guess and the result as the next guess. Repeat steps 2 and 3 until you reach a result that is accurate enough for your purposes.

Here we want the difference between our guess and our result to be accurate when the values are rounded to 2 decimal places. So our check is to subtract the new guess from the result and see if the remainder is small enough.

Now we can try to write this as a set of instructions to a program. We need two starting values, our square and our guess.

• Let's name a location "square" and copy the value of our square to it: square = 734
• Let's name a location "guess" and copy the value of our first guess to it: guess = 25
• Now we divide the square by the guess and copy it to location "result": result = square/guess
• Now we assign "guest" a new value, which is the average of guess and result: guess = (guess + result)/2
• We have to figure out whether this value is "close enough" to the right answer. If (guess - result) < 0.01, accept the answer and quit.

In a language like Perl (the duct tape that holds the Internet together), the code will look like this:

$square = 734;$guess = 25;
$result = 0; while ($guess - $result > 0.01) {$result = $square/$guess;
$guess = ($guess + $result)/2; } print$result;

The \$ (dollar sign) tells the Perl interpreter that the string is the name of a variable, and not a keyword in the language, like "while" in the loop. The brackets mark the beginning and end of statements in the loop. The semi-colon tells the interpreter when the statement ends; long statements or lists of values may cross multiple lines of the text file containing the code.

### Study/Discussion Questions:

• What are the advantages of having a "repeatable process", like the ones in a computer program?
• What are the limits of applying computer program algorithms to real situations?

### On your own

• Programming is just like any other kind of writing: there are good ways to do it and bad ways. Here's a short article on "style" that will give you some examples of how programs are written.