Math Commands
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.
ABS
Reports the absolute value of a number; one input.
Syntax
ABS number
Description
ABS reports the absolute value of its input, its non-negative value regardless of its sign.
Examples
ABS -30 Result: 30 ABS 30 Result: 30
AND
Logical AND; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
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.
AND may also be used as an infix operator.
Examples
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; one input.
Syntax
ARCCOS number
Description
ARCCOS takes as input a number between -1 and 1 and reports its arccosine, a value between 0 and 180.
Examples
ARCCOS 0.5 Result: 60
ARCCOT
Also: ACOT
Reports the arccotangent; one input.
Syntax
ARCCOT number
Description
ARCCOT reports the arccotangent of its input.
Examples
ARCCOT 1 Result: 45
ARCCSC
Also: ACSC
Reports the arccosecant; one input.
Syntax
ARCCSC number
Description
ARCCSC reports the arccosecant of its input.
Examples
ARCCSC 60 Result: 0.95
ARCSEC
Also: ASEC
Reports the arcsecant; one input.
Syntax
ARCSEC number
Description
ARCSEC reports the arcsecant of its input.
Examples
ARCSEC 45 Result: 88.73
ARCSIN
Also: ASIN
Reports the arcsine; one input.
Syntax
ARCSIN number
Description
ARCSIN takes as input a number between -1 and 1 and reports its arcsine, a value between -90 and 90.
Examples
ARCSIN 0.5 Result: 30
ARCTAN
Also: ATAN
Reports the arctangent; one input.
Syntax
ARCTAN number
Description
ARCTAN reports the arc tangent of its input as degrees. To use radians instead of degrees, use the formula X / 180 * PI
Examples
ARCTAN 0.5 Result: 26.57
ARCTAN2
Also: ATAN2
Reports the polar angle heading of the motion vector (position change) deltaX deltaY; two inputs.
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 counterclockwise.
See also PANGLE, PSETHEADING and PHEADING.
Examples
ARCTAN2 -5 -5 Result: 225
ASHIFT
Also: LSH
Shifts its input with sign extension; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
Syntax
ASHIFT integer integer
Description
ASHIFT outputs its first input arithmetic-shifted to the left by the number of bits supplied as its second input. If the second input is negative, the shift is to the right with sign extension. The inputs must be integers.
The abbreviation LSH is for backwards compatibility only and should not be confused with LSHIFT.
Examples
ASHIFT 2 1 Result: 4 ASHIFT -2 -1 Result: -1 LSHIFT -2 -1 Result: 2147483647
LOGAND
Also: BITAND
Combines its inputs with a bitwise AND operation; two inputs.
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 equals 0
LOGAND 1 0 equals 0
LOGAND 0 1 equals 0
LOGAND 1 1 equals 1
See also LOGNOT, LOGOR, LOGXOR.
Examples
LOGAND 2 1 Result: 0 LOGAND 2 3 Result: 2
LOGNOT
Also: BITNOT
Reports the bitwise logical complement of its input; expects a minimum of one input, but parentheses are needed if not called with one 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.
For 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.
Examples
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 Result: PRINT.BITS defined PRINT.BITS 21 Result: 00000000000000000000000000010101 PRINT.BITS LOGNOT 21 Result: 11111111111111111111111111101010 LOGNOT 21 Result: -22
LOGOR
Also: BITOR
Combines its inputs with a boolean OR operation; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
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 equals 0
LOGOR 1 0 equals 1
LOGOR 0 1 equals 1
LOGOR 1 1 equals 1
See also LOGNOT, LOGAND, and LOGXOR.
Examples
LOGOR 2 0 Result: 2 LOGOR 2 3 Result: 3
LOGXOR
Also: BITXOR
Combines its inputs with a boolean XOR operation; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
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 equals 0
LOGXOR 1 0 equals 1
LOGXOR 0 1 equals 1
LOGXOR 1 1 equals 0
See also LOGNOT, LOGAND, and LOGOR.
Examples
LOGXOR 2 1 Result: 3 LOGXOR 2 3 Result: 1
COS
Reports the cosine; one input.
Syntax
COS degreees
Description
COS reports the cosine of its input, a number of degrees. Remember that COS x = adjacent / hypotenuse. To use radians instead of degrees, use the formula X / 180 * PI
. See also ARCTAN and SIN.
Examples
COS 0 Result: 1 COS 90 Result: 0
COT
Reports the cotangent; one input.
Syntax
COT number
Description
COT reports the cotangent of its input.
Examples
COT 1 Result: 57.29
CSC
Reports the cosecant; one input.
Syntax
CSC angle
Description
CSC reports the cosecant of its input.
Examples
CSC 60 Result: 1.15
DIFFERENCE
Outputs the difference of two or more numbers; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
Syntax
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 in parentheses. The infix operator - does the same for a left and right input.
Examples
DIFFERENCE 6 3 Result: 3 (DIFFERENCE 6 3 2) Result: 1
EXP
Also: EXPN
Calculates the natural base e raised to a power; one input.
Syntax
EXPN number
Description
EXPN calculates the natural base e (2.7183…) raised to the power specified by its input. See also POWER.
Examples
EXPN 3 Result: 20.09 EXPN 0 Result: 1
INT
Reports the integer part of a number; one input.
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.
Examples
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; one input.
Syntax
LOG number
Description
LOG reports the natural logarithm of its input. See also LOG10.
Examples
LOG 10 Result: 2.3 LOG 1 Result: 0 LOG EXPN 1 Result: 1
LOG10
Outputs the logarithm of its input; one input.
Syntax
LOG10 number
Description
LOG10 reports the base 10 logarithm of its input. See also LOG.
Examples
LOG10 1 Result: 0 LOG10 100 Result: 2 LOG10 0.001 Result: -3
LSHIFT
Shifts its input without sign extension; two inputs.
Syntax
LSHIFT integer integer
Description
LSHIFT outputs its first input logically-shifted to the left by the number of bits supplied as its second input. If the second input is negative, the shift is to the right with zero fill. The inputs must be integers.
See also ASHIFT, which shifts to the right taking the sign bit into account.
Examples
LSHIFT 2 1 Result: 4 LSHIFT -2 -1 Result: 2147483647 ASHIFT -2 -1 Result: -1
MINUS
Outputs the negative value of its input; one 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).
Examples
MINUS 5 Result: -5 MINUS 3 + 4 Result: -7
MODULO
Outputs the remainder of two numbers; two inputs.
Syntax
MODULO dividend divisor
Description
MODULO reports the number that is the remainder of dividing the first input by the second. See also REMAINDER and QUOTIENT.
MODULO reports the result of (dividend - (divisor * floor(dividend / divisor))). The result is the same as for REMAINDER if the signs of both operands are the same, but are different from REMAINDER when the signs are different.
Examples
MODULO 6 3 Result: 0 MODULO 159 2 Result: 1 MODULO -123 4 Result: 3
NOT
Negates its input; one input.
Syntax
NOT expression
Description
NOT reports TRUE if its input is FALSE; otherwise it reports FALSE.
Examples
NOT “FALSE Result: TRUE NOT “TRUE Result: FALSE NOT NUMBER? “A Result: TRUE IF NOT (2 > 3) THEN PRINT “YES Result: YES
OR
Performs a logical OR on its input; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
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. With more than two inputs, OR and all inputs must be enclosed in parentheses.
OR may also be used as an infix operator.
Examples
OR “TRUE “TRUE Result: TRUE OR “TRUE “FALSE Result: TRUE (OR “FALSE “TRUE “FALSE) Result: TRUE IF OR (2 = 3) (3 = 3) [PRINT “YES] Result: YES
PI
Reports the number Pi; no inputs.
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.
Examples
MAKE “PRECISION 10 PI Result: 3.14159265356 MAKE “PRECISION 2 PI Result: 3.14
POWER
Raises a number to the power of another number; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
Syntax
POWER number number
(POWER number number number ...)
Description
POWER raises the first number to the power of the second number. When given multiple inputs, POWER and all its inputs must be enclosed in parentheses. The infix operator [^](cmd:^) does the same for a left and right input. See also EXPN.
Examples
POWER 3 2 Result: 9 (POWER 2 3 4) Result: 4096
PRODUCT
Calculates the product of its inputs; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
Syntax
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. The infix operator [](cmd:) does the same for a left and right input.
Examples
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
Reports the quotient of its inputs; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
Syntax
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 infix operator / does the same for a left and right input.. See also REMAINDER and MODULO.
Examples
QUOTIENT 10 2 Result: 5 QUOTIENT 10 3 Result: 3.33
RANDOM
Outputs a random number; expects between one input and two inputs, but parentheses are needed if not called with one input.
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.
Examples
RANDOM 4 Result: 3 (RANDOM 100 200) Result: 115
REMAINDER
Outputs the remainder of two numbers; two inputs.
Syntax
REMAINDER dividend divisor
Description
REMAINDER reports the number that is the remainder of dividing the first input by the second. The infix operator % does the same for a left and right input.
REMAINDER reports the result of (dividend - (divisor * int(dividend /divisor))). The result is the same as for MODULO if the signs of both operands 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.
Examples
REMAINDER 6 3 Result: 0 REMAINDER 159 2 Result: 1 REMAINDER -123 4 Result: -3
RERANDOM
Seed the random number generator; expects between zero inputs and one input, but parentheses are needed if not called with one input.
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 generator. Currently, RERANDOM does nothing; it is here for backwards compatibility.
ROUND
Rounds a number; one input.
Syntax
ROUND number
Description
ROUND reports the input number rounded to the nearest integer. See also INT.
Examples
ROUND 1.45 Result: 1 ROUND 1.50 Result: 2 ROUND -57.99 Result: -58
SEC
Reports the secant; one input.
Syntax
SEC number
Description
SEC reports the secant of its input.
Examples
SEC 30 Result: 1.15
SIN
Reports the sine; one input.
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. To use radians instead of degrees, use the formula X / 180 * PI
.
Examples
SIN 30 Result: 0.5 SIN 90 Result: 1
SQRT
Reports the square root; one input.
Syntax
SQRT number
Description
SQRT reports the square root of its input. The input number must be a positive number or 0.
Examples
SQRT 25 Result: 5 SQRT -1 Result: Error: SQRT cannot work with negative numbers
SUM
Reports the sum of its inputs; expects a minimum of two inputs, but parentheses are needed if not called with two inputs.
Syntax
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 in parentheses. The infix operator + does the same for a left and right input.
Examples
SUM 3 6 Result: 9 SUM 3.2 6.4 Result: 9.6 (SUM 3.2 6.4 1) Result: 10.6
TAN
Reports the tangent; one input.
Syntax
TAN number
Description
TAN reports the tangent of its input, specified in degrees.
Examples
TAN 45 Result: 1