 Terrapin Resources

# Math

Arithmetic computations.

Logo understands quite a lot of math commands, from simple addition and subtraction up to functions like the cosecant, or the square root. Random numbers are part of the math functions as well.

## POWER

Also: ^

Raises a number to the power of another number.

#### Syntax

``````number ^ number
POWER number number
(POWER number number number ...)
``````

#### Description

POWER raises the first number to the power of the second number. Its abbreviation is ^. When given multiple inputs, POWER and all its inputs must be enclosed in parentheses. The ^ symbol for POWER may be used as either a prefix or infix operator. See also EXPN.

#### Example

2 ^ 3 Result: 8 POWER 3 2 Result: 9 (POWER 2 3 4) Result: 4096

## DIFFERENCE

Also: -

Outputs the difference of two or more numbers.

#### Syntax

``````number1 - number2
DIFFERENCE number1 number2
(DIFFERENCE number1 number2 number3 ...)
``````

#### Description

DIFFERENCE reports the result of subtracting its inputs. DIFFERENCE expects two inputs, but will accept more if it and its inputs are enclosed within parentheses. DIFFERENCE is equivalent to the prefix or infix operator -.

#### Example

6 - 3 Result: 3 DIFFERENCE 6 3 Result: 3 (DIFFERENCE 6 3 2) Result: 1

## SUM

Also: +

Reports the sum of its inputs.

#### Syntax

``````number1 + number2
SUM number1 number2
(SUM number1 number2 number3 ...)
``````

#### Description

SUM reports the result of adding its inputs. SUM expects two inputs, but will accept more if it and its inputs are enclosed within parentheses. SUM is equivalent to the infix operator +.

#### Example

SUM 3 6 Result: 9 SUM 3.2 6.4 Result:9.6 (SUM 3.2 6.4 1) Result: 10.6

## PRODUCT

Also: *

Calculates the product of its inputs.

#### Syntax

``````number * number
PRODUCT number number
(PRODUCT number number number)
``````

#### Description

PRODUCT multiplies its inputs and reports the result. PRODUCT expects two inputs, but will accept more if it and all its inputs are enclosed in parentheses. PRODUCT is equivalent to *. When used as the * symbol, PRODUCT may be a prefix or an infix operator.

#### Example

PRODUCT 2 3 Result: 6 PRODUCT 4 -1.2 Result: -4.8 PRODUCT -.5 -1.5 Result: 0.75 (PRODUCT 2 3 4 5) Result: 120

## QUOTIENT

Also: /

Reports the quotient of its inputs.

#### Syntax

``````number / number
QUOTIENT number number
(QUOTIENT number number number ...)
``````

#### Description

QUOTIENT reports the result of dividing the first input by the second input. QUOTIENT expects two inputs, but will accept more if it and all its inputs are enclosed in parentheses. The symbol “/” can be used as a prefix or an infix operator. See also % and MODULO.

#### Example

10 / 5 Result: 2 QUOTIENT 10 2 Result: 5 QUOTIENT 10 3 Result: 3.33

## REMAINDER

Also: %

Outputs the remainder of two numbers.

#### Syntax

``````dividend % divisor
REMAINDER dividend divisor
``````

#### Description

REMAINDER reports the number that is the remainder of dividing the first input by the second. The symbol “%” can be used as a prefix or an infix operator. See also /.

REMAINDER reports the result of ```(dividend - (divisor * int(dividend /divisor)))```. The result is the same as for MODULO if the signs of both operand are the same, but are different from MODULO when the signs are different.

Until Logo version 4.0.4, REMAINDER was an alias for MODULO.

#### Example

REMAINDER 6 3 Result: 0 REMAINDER 159 2 Result: 1 689 % 468 Result: 221 REMAINDER -123 4 Result: -3

## .EQ

Also: =, EQUAL?, EQUALP

Tests its inputs for equality.

#### Syntax

``````object = object
= object object
``````

#### Description

The equality operator compares two objects. If they are equal, the output is TRUE; otherwise it is FALSE. This operator is the same as the = command. The operator “=” may be used either as a prefix or as an infix operator.

The value of the :EPSILON variable determines how the equality operator compares two numbers. See :EPSILON for details.

#### Example

4 = 5 Result: FALSE = 4 5 Result: FALSE .EQ 5 5 Result: TRUE EQUAL?   Result: TRUE EQUAL? 6  Result: FALSE

## .NE

Also: <>, !=, NOT.EQUAL?, NOT.EQUALP, NOTEQUAL?, NOTEQUALP

Tests its inputs for inequality.

#### Syntax

``````object != object
object <> object
NOTEQUAL? object object
``````

#### Description

NOTEQUAL? reports TRUE if its two inputs are not equal; otherwise it reports FALSE. Its inputs may be numbers, words, or lists. The symbols != and <> are abbreviations for NOTEQUAL?. The symbols “<>” and “!=” may be used as infix or prefix operators.

The value of the :EPSILON variable determines how the inequality operator compares two numbers. See :EPSILON for details.

#### Example

NOTEQUAL? 6 6 Result: TRUE NOTEQUAL? 6 66 Result: TRUE NOTEQUAL? “AZURE “AZURE Result: FALSE NOTEQUAL? [SPRING GREEN] [SPRING GREEN] Result: FALSE NOTEQUAL? “AZURE [AZURE] Result: TRUE

## .LT

Also: <, LESS?, LESSP

Tests if its first input is less than its second input.

#### Syntax

``````number < number
.LT number number
``````

#### Description

The Less Than operator compares two numbers. If the first number is less than the second number, the output is TRUE; otherwise it is FALSE. The symbol “<” may be used either as a prefix or as an infix operator.

#### Example

4 < 5 Result: TRUE < 4 5 Result: TRUE .LT 5 5 Result: FALSE

## GREATER?

Also: >, GREATERP, .GT

Tests if its first input is greater than its second input.

#### Syntax

``````number > number
.GT number number
``````

#### Description

The Greater Than operator compares two numbers. If the first number is greater than the second number, the output is TRUE; otherwise it is FALSE. The symbol “>” may be used either as a prefix or as an infix operator.

#### Example

4 > 5 Result: FALSE .GT 5 5 Result: FALSE

## .GE

Also: >=, GREATEREQUAL?, GREATEREQUALP

Tests if its first input is greater than or equal to its second input.

#### Syntax

``````number >= number
.GE number number
``````

#### Description

The Greater Than or Equal operator compares two numbers. If the first number is greater than or equal to the second number, the output is TRUE; otherwise, it is FALSE. The symbol “>=” may be used either as a prefix or as an infix operator.

#### Example

4 >= 5 Result: FALSE .GE 5 5 Result: TRUE

## .LE

Also: <=, LESSEQUAL?, LESSEQUALP

Tests if its first input is less than or equal to its second input.

#### Syntax

``````number <= number
.LE number number
``````

#### Description

The Less Than or Equal operator compares two numbers. If the first number is less than or equal to the second number, the output is TRUE; otherwise it is FALSE. The symbol “⇐” may be used either as a prefix or as an infix operator.

#### Example

4 <= 5 Result: TRUE .LE 5 5 Result: TRUE

## ABS

Reports the absolute value of a number.

#### Syntax

``````ABS number
``````

#### Description

ABS reports the absolute value of its input.

#### Example

ABS -30 Result: 30 ABS 30 Result: 30

## AND

Logical AND.

#### Syntax

``````AND input1 input2
(AND input1 input2 input3 ...)
``````

#### Description

AND accepts two or more inputs, which must be either TRUE or FALSE. AND reports TRUE if all of its inputs are true; otherwise it reports FALSE. By default, AND expects two inputs. If more inputs are supplied, both AND and its inputs must be enclosed in parentheses. See also OR.

#### Example

AND TRUE TRUE Result: TRUE AND TRUE FALSE Result: FALSE (AND TRUE FALSE TRUE) Result: FALSE

## ARCCOS

Also: ACOS

Reports the arccosine.

#### Syntax

``````ARCCOS number
``````

#### Description

ARCCOS takes as input a number between -1 and 1 and reports its arccosine, a value between 0 and 180.

#### Example

ARCCOS 0.5 Result: 60

## ARCCOT

Also: ACOT

Reports the arccotangent.

#### Syntax

``````ARCCOT number
``````

#### Description

ARCCOT reports the arccotangent of its input.

#### Example

ARCCOT 1 Result: 45

## ARCCSC

Also: ACSC

Reports the arccosecant.

#### Syntax

``````ARCCSC number
``````

#### Description

ARCCSC reports the arccosecant of its input.

#### Example

ARCCSC 60 Result: 0.95

## ARCSEC

Also: ASEC

Reports the arcsecant.

#### Syntax

``````ARCSEC number
``````

#### Description

ARCSEC reports the arcsecant of its input.

#### Example

ARCSEC 45 Result: 88.73

## ARCSIN

Also: ASIN

Reports the arcsine.

#### Syntax

``````ARCSIN number
``````

#### Description

ARCSIN takes as input a number between -1 and 1 and reports its arcsine, a value between -90 and 90.

#### Example

ARCSIN 0.5 Result: 30

## ARCTAN

Also: ATAN

Reports the arctangent.

#### Syntax

``````ARCTAN number
``````

#### Description

ARCTAN reports the arc tangent of its input as degrees.

#### Example

ARCTAN 0.5 Result: 26.57

## ARCTAN2 (ATAN2)

Reports the polar angle heading of the motion vector (position change) deltaX deltaY.

#### Syntax

``````ARCTAN2 deltaX deltaY
``````

#### Description

ARCTAN2 reports the full-circle polar angle (in degrees) of its input motion vector deltaX deltaY. Output 0 represents the positive x-axis direction (3 o’clock). Polar angles increase going counter clockwise.

#### Example

ARCTAN2 -5 -5 Result: 225

## COS

Reports the cosine.

#### Syntax

``````COS number
``````

#### Description

COS reports the cosine of its input, a number of degrees. Remember that COS x = adjacent / hypotenuse. See also ARCTAN and SIN.

#### Example

COS 0 Result:1 COS 90 Result: 0

## COT

Reports the cotangent.

#### Syntax

``````COT number
``````

#### Description

COT reports the cotangent of its input.

#### Example

COT 1 Result: 57.29

## CSC

Reports the cosecant.

#### Syntax

``````CSC number
``````

#### Description

CSC reports the cosecant of its input.

#### Example

CSC 60 Result: 1.15

## EXPN

Also: EXP

Calculates the natural base e raised to a power.

#### Syntax

``````EXPN number
``````

#### Description

EXPN calculates the natural base e (2.7183. . .) raised to the power specified by its input. See also [^](#).

#### Example

EXPN 3 Result: 20.09 EXPN 0 Result: 1

## INT

Reports the integer part of a number.

#### Syntax

``````INT number
``````

#### Description

INT reports the integer portion of its input by removing the decimal portion, if any. No rounding occurs. See also ROUND.

#### Example

INT 2.345 Result: 2 INT 2.789 Result: 2 INT 57.999 Result: 57

## LOG

Also: LN

Outputs the natural logarithm of its input.

#### Syntax

``````LOG number
``````

#### Example

LOG 10 Result: 2.3 LOG 1 Result: 0 LOG EXPN 1 Result: 1

## LOG10

Outputs the logarithm of its input.

#### Syntax

``````LOG10 number
``````

#### Description

LOG10 reports the base 10 logarithm of its input. See also LOG.

#### Example

LOG10 1 Result: 0 LOG10 100 Result: 2 LOG10 0.001 Result: -3

## LOGAND

Combines its inputs with a boolean AND operation.

#### Syntax

``````LOGAND integer1 integer2
``````

#### Description

LOGAND reports the bitwise logical AND of its two inputs. Each input is expressed internally as a 32 digit binary number. A logical AND operation is performed on the pair of binary digits (bits) in each position, resulting in a 32 bit integer. The logical AND operation is defined on the binary digits 0 and 1 as follows:

`LOGAND 0 0 = 0`
`LOGAND 1 0 = 0`
`LOGAND 0 1 = 0`
`LOGAND 1 1 = 1`

#### Example

LOGAND 2 1 Result: 0 LOGAND 2 3 Result: 2

## LOGNOT

Reports the bitwise logical complement of its input.

#### Syntax

``````LOGNOT integer
``````

#### Description

LOGNOT reports the bitwise logical complement of its input, replacing all 1’s with 0’s and all 0’s with 1’s. Since integers are stored in the computer as base 2 numbers 32 digits long, all the leading 0’s turn into 1’s. See also LOGAND, LOGOR, and LOGXOR.

#### Example

MAKE “BASE 2 21 Result: 10101 LOGNOT 21 Result: 11111111111111111111111111101010 MAKE “BASE 10

## LOGOR

Combines its inputs with a boolean OR operation.

#### Syntax

``````LOGOR integer1 integer2
``````

#### Description

LOGOR reports the bitwise logical OR of its two inputs. Each input is expressed internally as a 32 digit binary number. A logical OR operation is performed on the pair of binary digits (bits) in each position, resulting in a 32 bit integer. The logical OR operation is defined on the binary digits 0 and 1 as follows:

`LOGOR 0 0 = 0`
`LOGOR 1 0 = 1`
`LOGOR 0 1 = 1`
`LOGOR 1 1 = 1`

#### Example

LOGOR 2 0 Result: 2 LOGOR 2 3 Result: 3

## LOGXOR

Combines its inputs with a boolean XOR operation.

#### Syntax

``````LOGXOR integer1 integer2
``````

#### Description

LOGXOR reports the bitwise logical XOR of its two inputs. Each input is expressed internally as a 32 digit binary number. A logical XOR operation is performed on the pair of binary digits (bits) in each position, resulting in a 32 bit integer. The logical XOR operation is defined on the binary digits 0 and 1 as follows:

`LOGXOR 0 0 = 0`
`LOGXOR 1 0 = 1`
`LOGXOR 0 1 = 1`
`LOGXOR 1 1 = 0`

#### Example

LOGXOR 2 1 Result: 3 LOGXOR 2 3 Result: 1

## LSH

Also: ASHIFT

Shifts its input with sign extension.

#### Syntax

``````LSH integer integer
``````

#### Description

LSH reports the first input logically shifted the number of bit positions specified by the second input. If the second input is positive, the logical shift is to the left. If the second input is negative, the logical shift is to the right, with the extension of the sign bit. This is the difference to LSHIFT.

#### Example

LSH 2 1 Result: 1 LSH -2 -1 Result: -4 LSHIFT-2 -1 Result: 2147483647

## LSHIFT

Shifts its input without sign extension.

#### Syntax

``````LSHIFT integer integer
``````

#### Description

LSHIFT reports the first input logically shifted the number of bit positions specified by the second input. If the second input is positive, the logical shift is to the left. If the second input is negative, the logical shift is to the right, filling up the field with zero bits. This is the difference to LSH.

#### Example

LSHIFT2 1 Result: 1 LSHIFT-2 -1 Result: 2147483647 LSH -2 -1 Result: -4

## MINUS

Outputs the negative value of its input.

#### Syntax

``````MINUS number
``````

#### Description

MINUS reports the result of subtracting its input from 0. Note that MINUS binds less than other operators. Thus, MINUS 3 + 4 is -7 (-(3+4)) and not 1 (-3+4).

#### Example

MINUS 5 Result: -5 MINUS 3 + 4 Result: -7

## MODULO

Outputs the remainder of two numbers.

#### Syntax

``````MODULO dividend divisor
``````

#### Description

MODULO reports the number that is the remainder of dividing the first input by the second. See also % and /.

MODULO reports the result of ```(dividend - (divisor * floor(dividend / divisor)))```. The result is the same as for % if the signs of both operand are the same, but are different from % when the signs are different.

#### Example

MODULO 6 3 Result: 0 MODULO 159 2 Result: 1 MODULO -123 4 Result: 1

## NOT

Negates its input.

#### Syntax

``````NOT expression
``````

#### Description

NOT reports TRUE if its input is false; otherwise it reports FALSE.

#### Example

NOT “FALSE Result: TRUE NOT “TRUE Result: FALSE NOT NUMBER? “A Result: TRUE IF NOT (2 > 3) THEN PRINT “YES YES

## OR

Performs a logical OR on its input.

#### Syntax

``````OR object1 object2
(OR object1 object2 object3 ...)
``````

#### Description

OR reports FALSE if all of its inputs are false; otherwise, it reports TRUE. OR accepts two or more inputs, which must be either TRUE or FALSE.

#### Example

OR “TRUE “TRUE Result: TRUE OR “TRUE “FALSE Result: TRUE (OR “FALSE “TRUE “FALSE) Result: TRUE IF OR (2 = 3) (3 = 3) [PRINT “YES] YES

## PI

Reports the number Pi.

#### Syntax

``````PI
``````

#### Description

PI reports the value of pi. The number of digits displayed for PI is determined by the current value of :PRECISION. The full value of PI is always used in calculations, regardless of the value of :PRECISION.

#### Example

MAKE “PRECISION 10 PI Result: 3.14159265356 MAKE “PRECISION 2 PI Result: 3.14

Reports the arc tangent of an angle expressed in radians.

#### Syntax

``````RADARCTAN number
``````

#### Description

RADARCTAN reports the arc tangent of its input as radians. If RADARCTAN has two inputs x and y, it uses y/x if x is nonzero; if x is zero, it outputs PI/2 if y is positive, or -PI/2 if y is negative. To compute the value of pi, use 2*(RADARCTAN 0 1), or use PI.

#### Example

``````RADARCTAN 0.5
``````

Reports the cosine of an angle expressed in radians.

#### Syntax

``````RADCOS number
``````

#### Example

``````RADCOS 0
``````

Reports the sine of an angle expressed in radians.

#### Syntax

``````RADSIN number
``````

#### Example

``````RADSIN PI / 4
``````

## RANDOM

Outputs a random number.

#### Syntax

``````RANDOM number
(RANDOM bottom top)
``````

#### Description

RANDOM reports a randomly selected number from 1 through its input. The output can only be a positive integer. For example: RANDOM 5 could report 1, 2, 3, 4, or 5. If RANDOM has two inputs, RANDOM outputs a random number between, and including, these two numbers. RANDOM delivers cryptographically secure numbers.

#### Example

RANDOM 4 Result: 3 (RANDOM 100 200) Result: 115

## RERANDOM

Seed the random number generator.

#### Syntax

``````RERANDOM number
(RERANDOM)
``````

#### Description

RERANDOM seeds the random number generator with its input. If RERANDOM is used without inputs, it uses a random number to seed the gemerator. See also RANDOM.

## ROUND

Rounds a number.

#### Syntax

``````ROUND number
``````

#### Description

ROUND reports the input number rounded to the nearest integer. See also INT.

#### Example

ROUND 1.45 Result: 1 ROUND 1.50 Result: 2 ROUND -57.99 Result: -58

## SEC

Reports the secant.

#### Syntax

``````SEC number
``````

#### Description

SEC reports the secant of its input.

#### Example

SEC 30 Result: 1.15

## SIN

Reports the sine.

#### Syntax

``````SIN number
``````

#### Description

SIN reports the sine of its input, which is the number of degrees in an angle. Remember that SIN x = opposite/hypotenuse. See also ARCTAN and COS.

#### Example

SIN 30 Result: 0.5 SIN 90 Result: 1

## SQRT

Reports the square root.

#### Syntax

``````SQRT number
``````

#### Description

SQRT reports the square root of its input. The input number must be a positive number or 0.

#### Example

SQRT 25 Result: 5 SQRT -1 Square root of a negative number

## TAN

Reports the tangent.

#### Syntax

``````TAN number
``````

#### Description

TAN reports the tangent of its input, specified in degrees.

TAN 45 Result: 1