Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
21
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

BASIC Integers

BASIC has somre extra rules around integers. To declare an integer variable in BASIC you can either use a plain unadorned name or you can signal to BASIC that it is an integer we wish to store(this will be slightly more efficient). We do this by ending the name with '%':

FOO = 8 REM FOO can hold any kind of number

BAR% = 9 REM BAR can only hold integers

One final gotcha with integer variables in BASIC:

i% =

7

* i%

PRINT

2

i% =

4.5

* i%

PRINT

2

Notice that the assignment of 4.5 to i% seemed to work but only the integer part was actually assigned. This is reminiscent of the way Python dealt with division of integers. All programming languages have their own little idiosyncracies like this!

Tcl Numbers

As mentioned earlier Tcl stores everuything internally as strings, however this doesn't really make any diffeence to the user because Tcl converts the values into numbers and back again under the covers, as it were. Thus all the restrictions on number sizes still apply.

Using numbers in Tcl is slightly more complex than in most languages since to do any calculations you have to signal to the interpreter that a calculation is needed. You do that with the expr command:

% put [expr 6 + 5] 11

Tcl ses the square brackets and evaluates that part first, as if it had been typed at the command line. In doing so it sees the expr command and does the calculation. The result is then put to the screen. If you try to put the sum directly Tcl will just print out "6 + 5":

% put 6 + 5 6 + 5

Real Numbers

These are fractions. They can represent very large numbers, much bigger than MAXINT, but with less precision. That is to say that 2 real numbers which should be identical may not seem to be when compared by the computer. This is because the computer only approximates some of the lowest details. Thus 4.0 could be represented by the computer as 3.9999999.... or 4.000000....01. These approximations are close enough for most purposes but occasionally they become important! If you get a funny result when using real numbers, bear this in mind.

Floating point numbers have the same operations as integers with the addition of the capability to truncate the number to an integer value.

24