Topic 15 - More About Numbers

Logo can handle numbers as well as text, in fact you have been using this feature since the start of your explorations. The inventor of Logo, Seymour Papert, wanted to create a laboratory or 'micro world' in which children (primarily) could experiment with numbers and see the results in the form of the turtle's trail.

You can use Logo as a simple calculator, for example:

PRINT 2 + 2
PRINT SQRT 4
PRINT SQRT 2
PRINT 2 + 3 * 2

Or you can use it as a 'prefix' calculator:

PRINT + 2 2
PRINT * + 2 3 2 

The first of these examples is easy to understand as we can easily imagine the '+' moving in between the 2s. The second example, however, is a little more tricky. By convention we place the arithmetic operator (+, -, *, /) in between the operands but it is possible to place the operators before or after the two operands.

The three styles of arithmetic are known as:

If you look back at the example 2 + 3 * 2 you will see that there are two possible answers, 8 and 10, depending on whether you perform the multiplication before or after the addition. If you want the addition performed first you must use brackets around the addition: (2 + 3) * 2. With infix and postfix no brackets are required but you must get the order of the operators just right as their operands are paired from left to right:

+ * 2 3 2   gives 8

* + 2 3 2   gives 10

and no brackets!

Logo provides words for mathematical operations as well as symbols so the example above could have been written as:

SUM PRODUCT 2 3 2
PRODUCT SUM 2 3 2

The other operators are DIFFERENCE and DIVISION for general subtraction and division, and QUOTIENT and REMAINDER for integer division. For example:

PRINT DIFFERENCE 5 2 gives 3
PRINT DIVISION 5 2 gives 2.5
PRINT QUOTIENT 5 2 gives 2 (there are 2 2s in 5)
PRINT REMAINDER 5 2 gives 1 (the remainder from the 2 2s in 5)

Prefix and postfix may seem unnecessarily difficult and obscure - why learn two more ways of doing what you can do already? - but it actually comes in very useful in advanced computer science.

Logo allows the user to set the number of significant digits shown in the results of calculations, and also to switch to scientific notation if required:

SETPRECISION 15
PRINT 50 / 7 gives 7.142857142857143
PRINT PI gives 3.141592653589793
PRINT 123.456e2 gives 12345.6 SETEXPONENTIALNOTATION
PRINT PI * PI * PI gives 3.100627668029982e+001
PRINT 0.5 * 0.1 gives 5.000000000000000e-002

Return to Logo Home Page