In fact, computers don’t really understand mathematics? How is that calculated?

Fundamentally speaking, computers do not really understand mathematics, nor do they understand the meaning of calculations. Even if a computer calculates 1 + 1 to get 2, it is not actually processing numbers, but manipulating the current according to specific rules.

In order for these rules to produce something useful to us, we need to correlate the electrical signals inside the computer with the numbers and symbols that humans like to use.

4.2.1 Integer

This is a shocking (electric shock) thought!

The obvious way to associate electrical signals with numbers is to specify a direct correspondence between voltage (or current) and numbers. For example, we can let 0V correspond to the number 0, 1V correspond to 1, 10V correspond to 10, and so on. There was a time when this was done in so-called analog computers. But this method has some problems, not just a million-volt computer!

Whose idea is this? Makes people's eyes bright.

Here is another method. Imagine that we use light bulbs to represent numbers. If the bulb is off, the number is 0; if the bulb is on, the number is 1. This is good, but it can only represent two numbers.

Well, let's upgrade to a triple light bulb. The three-light bulb actually has 4 switch positions: off and 3 increased brightness levels. Inside, the three-light bulb has two filaments (see Figure 4.1), one dark and one bright. For example, one filament may be 50W and another filament may be 100W. By not choosing one, choosing one, choosing the other, choosing two, we can get filaments of 0W, 50W, 100W, and 150W. We can use that light bulb to represent the numbers 0, 50, 100, and 150, or we can determine the 4 levels to represent the numbers 0, 1, 2, and 3.

In fact, computers don’t really understand mathematics?  How is that calculated?

 

Figure 4.1 Three light bulb

Internally, computers use the same idea to represent integers. Instead of using 50, 100, and 150 as in the lighting example above, use numbers combined with powers of 2. Imagine we have a light bulb with

In fact, computers don’t really understand mathematics?  How is that calculated?

 

W、

In fact, computers don’t really understand mathematics?  How is that calculated?

 

W and

In fact, computers don’t really understand mathematics?  How is that calculated?

 

W filament. Then, we can get the number 0 without turning on any filament, just turn on

In fact, computers don’t really understand mathematics?  How is that calculated?

 

The filament of W can get the number 1. By turning on

In fact, computers don’t really understand mathematics?  How is that calculated?

 

W filament can get 2, and so on, until all 3 filaments are turned on, we get

In fact, computers don’t really understand mathematics?  How is that calculated?

Now imagine that we have the following 4 consecutive powers of 2:

In fact, computers don’t really understand mathematics?  How is that calculated?

In fact, computers don’t really understand mathematics?  How is that calculated?

In fact, computers don’t really understand mathematics?  How is that calculated?

 

with

In fact, computers don’t really understand mathematics?  How is that calculated?

 

. Please take a moment to write out the numbers 0, 1, 2, etc., using some of these powers of 2, until the maximum possible value. Stop reading, we will wait for you to try to complete this task.

If all goes well, you will find that you can use 0 or more of these powers of 2 to get all integers from 0 to 15. For example, 13 can be expressed as

In fact, computers don’t really understand mathematics?  How is that calculated?

There are 10 kinds of people in this world: people who understand binary and people who don't understand binary.

Written in another way is:

In fact, computers don’t really understand mathematics?  How is that calculated?

Please note that we wrote the larger power of 2 on the left and the smaller power of 2 on the right. As we will see soon, this convention is very useful. The 0 and 1 in the above formula (that is, the "coefficients" of the power of 2) indicate whether a specific power of 2 is used. These 0 and 1 coefficients are called "bits" and represent binary numbers. The binary representation uses two values-here, the two values ​​are 0 and 1.

It is convenient to use bit sequences to represent numbers without explicitly expressing powers of 2. For example, we will use the bit sequence 1101 to represent the number 13, because this is the order in which the bits appear in the above equation. Similarly, 0011 represents the number 3. We usually ignore the leading (ie, leftmost) 0, so we can write it as 11.

The representation we use here is called "base 2" because it is based on powers of 2. of course! You can also use base-10 numbers. In the base 10 numbers, the numbers are formed by powers of 10, and we not only use 0 and 1 as coefficients, but 0-9. For example, the sequence 603 actually means:

In fact, computers don’t really understand mathematics?  How is that calculated?

In "Star Wars", the Hutt has 8 fingers, so the base is also 8.

Other bases are also useful. For example, the Native American tribes in Northern California use 8 as the base by the Yuki. When using 8 as the base, we use the power of 8, and the coefficient used is 0-7. So, for example, the base 8 sequence 207 represents:

In fact, computers don’t really understand mathematics?  How is that calculated?

 

This is 135 (base 10). It is believed that the base people use 8 as the base because they use 8 "slots" between the fingers for counting.

Please note that if we choose a base b (where b is an integer greater than or equal to 2), the number used as the coefficient is 0 ~ b −1. why? It is not difficult to prove mathematically that when we use this convention, we can use d numbers to represent 0~

In fact, computers don’t really understand mathematics?  How is that calculated?

-1 positive integer. In addition, each integer in this range has a unique representation, which is convenient because it avoids the trouble of having multiple representations for the same number. For example, just like 42 has no other representation in base 10, the number 1101 in base 2 (we just saw it is 13 in base 10) has no other representation.

Many older or smaller computers use 32 bits to represent base 2 numbers. We wisely call them "32-bit computers". Therefore, we can uniquely represent 0~

In fact, computers don’t really understand mathematics?  How is that calculated?

-1 (ie 4,294,967,295) all positive integers. Powerful modern computers use 64 bits to represent each number, which can represent the largest

In fact, computers don’t really understand mathematics?  How is that calculated?

The integer is about 18 trillion.

4.2.2 Arithmetic

Arithmetic with base 2, base 8 or base 42 is similar to base 10 arithmetic. For example, let us consider addition. When the base is 10, we start from the rightmost column. We add these numbers here and "carry" to the next column when needed. For example, when performing the following calculations:

We will happily do the addition!

In fact, computers don’t really understand mathematics?  How is that calculated?

5 + 7 = 12, so we write down 2 in the rightmost column, and carry one. That 1 represents 10, so it is propagated (that is, "carry") to the position of 10 in the next column.

We will try not to get excited about these examples, but you should try to add some base 2 numbers to make sure you understand it.

The addition with base 2 is almost the same. Let's calculate 111 (you should remember that base 10 is 7) plus 110 (base 10 is 6). We start with the rightmost (or "least significant") column and add 1 to 0 to get 1. Now we move to the next column, which is

In fact, computers don’t really understand mathematics?  How is that calculated?

Bit, which means the position of 2. Each of the two numbers is 1 at this position. 1 + 1 = 2, but we only use 0 and 1 when the base is 2, so when the base is 2, we get 1 + 1 = 10. This is similar to calculating 7 + 3 when the base is 10: We write 0 instead of 10, and carry 1 to the next column. Similarly, when the base is 2, for 1 + 1, we write 0 and carry 1 to the next column.

Do you know why this works? There is 2 in the 2 position, which is equivalent to 1 in the 4 position. Generally speaking, it corresponds to

In fact, computers don’t really understand mathematics?  How is that calculated?

There is a 2 in the column, which is equivalent to corresponding to

In fact, computers don’t really understand mathematics?  How is that calculated?

There is a 1 in the next column because

In fact, computers don’t really understand mathematics?  How is that calculated?

Key point: In your favorite base, addition, subtraction, multiplication, and division are similar to those in base 10!

4.2.3 Negative thinking

We have successfully represented numbers with base 2 and performed arithmetic operations on them. But all our numbers are positive. How about expressing as a negative number? What about the score?

Let's start with negative numbers. A fairly obvious method is to reserve 1 bit to indicate whether the number is positive or negative. For example, in a 32-bit computer, we might use the leftmost bit for this purpose. Setting this bit to 0 may mean that the number represented by the remaining 31 bits is positive, and if the leftmost bit is 1, the remaining number will be considered negative. This is called "symbol-size" representation. The price we pay is that we lose half of the range (because in our example, since there are now only 31 bits to represent the size of the number). Although we don't want to be too negative here, the bigger problem is that it is very tricky to construct computer circuits to manipulate the symbol amplitude numbers. Instead, we use the so-called "two's complement" system.

The idea behind two's complement is this: if the sum of the representation of the number plus the representation of its negative number is 0, it will be very convenient. For example, since 3 plus −3 is 0, if the binary representation of 3 plus the binary representation of −3 sums to 0, it is fine. We already know that the binary representation of 3 is 11. Suppose we have an 8-bit computer (instead of 32-bit or 64-bit), just to make this example easier. Then, including the leading 0, 3 will be represented as 00000011. Now how do we represent −3 so that we add it to 00000011 and represent it as 0, which is 00000000?

Note that if we "flip" all the bits in the representation of 3, we get 11111100. In addition, 00000011 + 11111100 = 11111111. If we add 1 bit to this, we get 11111111 + 00000001. When we add with the carry, we get 100000000, that is, 1 followed by 8 zeros. If the computer uses only 8 bits to represent each number, it will not record the leftmost (9th) bit! In this case, only the lower 8 bits 000000000, which is 0, are saved. Therefore, to represent −3, we can simply take the representation of 3, flip these bits, and then add 1 to it. Please try it out and make sure you understand how it works.

In short, to represent a negative number in a twos complement system, you need to flip the bits of the positive number first, and then add 1.

4.2.4 Score: Stitched together

How to express the score? One method (usually used in video and music players) is to establish a convention to measure everything in a convenient fraction (just like our three-light bulb works with 50W). For example, we may decide that all content is in units of 0.01, so the number 100111010 does not represent 314, but 3.14.

The problem with this approach is that scientific calculations often require numbers with higher precision and a larger range of numbers than the strategy provides. For example, the value often used by chemists is about

In fact, computers don’t really understand mathematics?  How is that calculated?

Or higher (Avogadro constant is approximately

In fact, computers don’t really understand mathematics?  How is that calculated?

), while the values ​​used by nuclear physicists may be as small as

In fact, computers don’t really understand mathematics?  How is that calculated?

, Even smaller.

This problem can be solved in the following way: Suppose we operate with base 10 and only 8 digits represent our number. We may use the first 6 digits to represent a number and agree to have an implicit 0 and a decimal point before the first number. For example, the 6 numbers 123456 will represent the number 0.123456. Then you can use the last two digits to represent the exponent of the power of 10, so 12345678 will represent

In fact, computers don’t really understand mathematics?  How is that calculated?

. Computers use a similar idea to represent decimals, but use base 2 instead of base 10.

4.2.5 Letters and strings

As you know, computers can not only manipulate numbers, they can also process symbols, words, and documents. Now that we have a way to represent numbers as multiple bits, we can use these numbers to represent other symbols.

It is quite easy to represent letters with numbers. We only need to reach an agreement on the encoding, that is, "agreement." For example, we might decide that 1 represents "A", 2 represents "B", and so on. Or we can use 42 for "A" and 97 for "B". As long as we work entirely in our own computer system, it doesn't matter.

Although I find it is usually easier to reach an agreement with myself.

However, if we want to send a document to a friend, it helps to reach an agreement with more people than with ourselves. A long time ago, the American National Standards Institute (ANSI) issued such a protocol, called ASCII (pronounced "ask-ee", which stands for American Standard Code for Information Interchange). It defines the encoding of uppercase and lowercase letters, numbers, and a set of selected special characters, all of which happen to be symbols printed on a standard American keyboard, which is no coincidence.

You can find the ASCII encoding standard on the Internet. In addition, you can use the Python function ord to find the numeric representation of any symbol. E.g:

>>> ord('*') 
42
>>>  ord('9') 
57

In ASCII, the number 42 represents an asterisk (*).

Why is the ordinal value of '9' reported as 57? Remember, the quoted '9' is just a character, like an asterisk, letter or punctuation mark. In the ASCII convention, it appears as character 57. By the way, the opposite of ord is chr. Typing chr(42) will return an asterisk'*', and chr(57) will return the character '9'.

ord stands for "ordinal" (ordinal number). You can think of it as asking for the "serial number" of the symbol

Each character in ASCII can be represented by 8 bits, usually called a byte (Byte). Unfortunately, only 8-bit ASCII can only represent 256 different symbols. (You may find it interesting to pause here and write a short program that counts from 0 to 255, and for each of these numbers, it outputs the ASCII symbol corresponding to that number. You will It is found that some symbols are output very strange or even invisible. (Look up on the Internet for more reasons.)

It seems that 256 symbols are a lot, but it does not provide accented characters used in languages ​​such as French, let alone Cyrillic, Sanskrit or thousands of Chinese and Japanese symbols.

Sometimes the 4 bits are called "nybble"; we are not responsible for this poor, nerdy pun.

In order to solve this oversight, the International Organization for Standardization (ISO—pronounced "eye-so" instead of reading a single letter) finally designed a system called "Unicode", which can represent each type of Know every character in the language and leave room for future growth. Because Unicode is a waste of space for English documents, ISO also defines several space-saving Unicode transformation formats (Unicode Transformation Format, UTF), the most popular of which is UTF-8. You may already use UTF-8 on your computer, but we won't go into details here.

There are even unofficial Klingon Unicode symbols!

Of course, the individual letters themselves are not very interesting. People usually like to string letters together, and we have seen that Python uses a data type called "string" to achieve this. This is easy to do with a sequence of numbers. For example, in ASCII, the sequence 99, 104, 111, 99, 111, 108, 97, 116, 101 is interpreted as "chocolate". The only missing detail is that when given a long list of numbers, you must know when to stop. A common convention is to include a "length field" at the very beginning of the sequence. This number tells us how many characters are in the string. (Python uses the length field, but hides it from us to prevent the string from appearing cluttered.)

4.2.6 Structured Information

Using the same concept, we can represent almost any information as a sequence of numbers. For example, a picture can be represented as a series of colored dots arranged in rows. Each color point (also called "picture element", or pixel) can be represented as 3 numbers, giving the red, green, and blue components at that pixel. Similarly, sound is a time series of sound pressure levels in the air. Movies are more complex time series of single pictures, usually 24 or 30 frames per second, accompanied by matching sound sequences.

That would be a little annoying!

This is the concept of abstraction again! Bits make up numbers, numbers make up pixels, pixels make up pictures, and pictures make up movies. A two-hour movie may require billions of bits, but no one who makes or watches the movie wants to think about all these bits!

Key term

abstraction: abstraction

AND, OR and NOT gates:AND、OR和NOT门

ASCII

assembly language: assembly language

base: base

binary: Binary

bit: bit

byte: byte

circuit: circuit

coefficients: coefficients

command line: command line

compiler: compiler

conditional jump instruction: conditional jump instruction

CPU

full adder: full adder

halt: stop

instruction register: instruction register

interpreter: interpreter

latch: latch

length field: length field

logic gates: logic gates

machine language: machine language

merge sort: merge sort

minterm: minimum term

minterm expansion principle: principle of minimum term expansion

modular design: modular design

no-operation instruction: no operation instruction

opcode: opcode

operation code: operation code

padding: padding

parameter passing: parameter passing

pixel: pixel

pop: pop up

program counter (PC): program counter

push: push

RAM (Random Access Memory): Random Access Memory

registers: registers

ripple-carry adder: ripple-carry adder

sign-magnitude representation: sign-magnitude representation

stack: stack

stack discipline: stack discipline

stack pointer: stack pointer

truth table: truth table

two's complement: two's complement

unconditional jump instruction: unconditional jump instruction

Unicode

UTF-8

variable jump target: variable jump target

von Neumann architecture: von Neumann architecture

word: word

This article is excerpted from "Introduction to Computer Science" (Python version).

In fact, computers don’t really understand mathematics?  How is that calculated?

The rest of the book follows the same idea. We use the Python language because of its simple syntax and a rich set of tools and software packages that enable novice programmers to write useful programs. In Chapter 2, our introduction to programming with Python is limited to a limited subset of the language syntax, which embodies the spirit of functional programming languages. In this way, readers have mastered recursion very early and realized that they can write interesting programs with very little code.

Chapter 3 takes functional programming a step further and introduces the concept of higher-order functions. Chapter 4 focuses on a question: "How does my computer do all this?" We studied the inner workings of computers, from digital logic to computer organization to programming in machine language.

Now that the "mystery" of computers has been unveiled, readers have also seen the physical representation of what is happening "behind the scenes", so we continue to explore more complex ideas in computing in Chapter 5, while discussing issues such as citations and Concepts such as degeneration, as well as structures, arrays, and dictionaries including loops. We use the computer physical model introduced in Chapter 4 to explain these concepts and structures. According to our experience, if readers have established the underlying physical model, it will be easier to understand these concepts. All of this is done in a scene familiar to readers. This is a recommendation program, just like the one used in online shopping.

New content! There is improvement! There are many "marginal" useful notes!

In Chapter 6, we discussed some key ideas in object-oriented programming and design. The goal here is not to train professional programmers, but to explain the basic principles of the object-oriented paradigm and let the reader understand some key concepts. Finally, in Chapter 7, we studied the "difficulty" of the problem-in terms of computational complexity and computability, we provided some elegant, but mathematically reasonable processing methods, and finally proved that it cannot be solved on a computer. Many calculation problems. We use Python as a model instead of using a formal computational model (like a Ling machine).

This book is intended to be used with a large number of resources we have developed for the course, which are available on the website https://www.cs.hmc.edu/csforall. These resources include a complete lecture PPT, a rich set of weekly homework, some accompanying software and documents, and published papers about the course.

We intentionally kept this book relatively short and tried to make it interesting and readable. This book accurately reflects the content of the course, not a daunting encyclopedia that cannot be completed in one semester. When we wrote this book, we believed that readers could read everything comfortably as the course progressed.

Guess you like

Origin blog.csdn.net/epubit17/article/details/108232496