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

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

Description

DIFFERENCE reports the result of subtracting its inputs. DIFFERENCE expects two inputs, but will accept more if it and its inputs are enclosed in 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

Description

SUM reports the result of adding its inputs. SUM expects two inputs, but will accept more if it and its inputs are enclosed in 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

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

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

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

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? [6] [6] Result: TRUE EQUAL? 6 [6] Result: FALSE

.NE

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

Tests its inputs for inequality.

Syntax

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: FALSE 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

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 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

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 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

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

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

Description

ABS reports the absolute value of its input, its non-negative value regardless of its sign.

Example

ABS -30 Result: 30 ABS 30 Result: 30

AND

Logical AND.

Syntax

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 AND 3=3 3=4 Result: FALSE AND 10=10 6=6 Result: TRUE

ARCCOS

Also: ACOS

Reports the arccosine.

Syntax

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

Description

ARCCOT reports the arccotangent of its input.

Example

ARCCOT 1 Result: 45

ARCCSC

Also: ACSC

Reports the arccosecant.

Syntax

Description

ARCCSC reports the arccosecant of its input.

Example

ARCCSC 60 Result: 0.95

ARCSEC

Also: ASEC

Reports the arcsecant.

Syntax

Description

ARCSEC reports the arcsecant of its input.

Example

ARCSEC 45 Result: 88.73

ARCSIN

Also: ASIN

Reports the arcsine.

Syntax

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

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

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.

See also PANGLE, PSETHEADING and PHEADING.

Example

ARCTAN2 -5 -5 Result: 225

COS

Reports the cosine.

Syntax

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

Description

COT reports the cotangent of its input.

Example

COT 1 Result: 57.29

CSC

Reports the cosecant.

Syntax

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

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

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

Description

LOG reports the natural logarithm of its input. See also LOG10.

Example

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

LOG10

Outputs the logarithm of its input.

Syntax

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

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 equals 0
LOGAND 1 0 equals 0
LOGAND 0 1 equals 0
LOGAND 1 1 equals 1

See also LOGNOT, LOGOR, LOGXOR.

Example

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

LOGNOT

Reports the bitwise logical complement of its input.

Syntax

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

The number 21 in binary notation would be

21 = 00000000000000000000000000010101

With all bits inverted, it is

-22 = 11111111111111111111111111101010

Note that negative numbers always start with a “1” bit.

TO PRINT.BITS :N MAKE “N ROUND :N REPEAT 32 [ IF (LOGAND :N #H80000000) <> 0 [TYPE 1][TYPE 0] MAKE “N LSHIFT :N 1 ] (PR) END PRINT.BITS 21 00000000000000000000000000010101 PRINT.BITS LOGNOT 21 11111111111111111111111111101010 LOGNOT 21 Result: -22

LOGOR

Combines its inputs with a boolean OR operation.

Syntax

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 equals 0
LOGOR 1 0 equals 1
LOGOR 0 1 equals 1
LOGOR 1 1 equals 1

See also LOGNOT, LOGOR, LOGXOR.

Example

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

LOGXOR

Combines its inputs with a boolean XOR operation.

Syntax

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 equals 0
LOGXOR 1 0 equals 1
LOGXOR 0 1 equals 1
LOGXOR 1 1 equals 0

See also LOGNOT, LOGOR, LOGOR.

Example

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

LSH

Also: ASHIFT

Shifts its input with sign extension.

Syntax

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: 4 LSH -2 -1 Result: -1 LSHIFT -2 -1 Result: 2147483647

LSHIFT

Shifts its input without sign extension.

Syntax

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.

See also LSH.

Example

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

MINUS

Outputs the negative value of its input.

Syntax

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

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: 3

NOT

Negates its input.

Syntax

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

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. With more than two inputs, OR and all inputs must be enclosed in parentheses.

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

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

RADARCTAN

Also: RADATAN

Reports the arc tangent of an angle expressed in radians.

Syntax

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

RADCOS

Reports the cosine of an angle expressed in radians.

Syntax

Description

RADCOS reports the cosine of its input as radians. Remember that RADCOS x = adjacent / hypotenuse. See also RADARCTAN and RADSIN.

Example

RADSIN

Reports the sine of an angle expressed in radians.

Syntax

Description

RADSIN reports the sine of its input, which is the number of radians in an angle. Remember that SIN x = opposite/hypotenuse. See also RADARCTAN and RADCOS.

Example

RANDOM

Outputs a random number.

Syntax

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

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

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

Description

SEC reports the secant of its input.

Example

SEC 30 Result: 1.15

SIN

Reports the sine.

Syntax

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

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 Error: SQRT cannot work with negative numbers

TAN

Reports the tangent.

Syntax

Description

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

Example

TAN 45 Result: 1