Skip to content
Hunter Close edited this page Aug 17, 2018 · 18 revisions

Math

GlowScript help with math functions

single number

sing num zero

Enter an integer (positive, negative, or zero) or a decimal number. Fractions cannot be entered in this block.

sing num dec sing num neg

Arithmetic involving two inputs (number or vector)

Click the operator sign to select different options:

  • + (add)
  • - (subtract)
  • * (multiply)
  • / (divide)
  • ^ (exponent)

double input math block

Arithmetic with more than two inputs

To combine more than two numbers in an arithmetic operation, multiple operation blocks must be assembled.

Example:

order of ops

This compound block represents the compound operation 4 * (2 + 3), which is equal to 4 * 5, or 20. The inner block is computed first, just as though the operation were enclosed in parentheses.

Vectors and numbers

The available operations in the menu will change according to the inputs that have been entered. The most efficient way (i.e., least number of mouse actions) to assemble an arithmetical operation involving one or more vectors is to first enter the inputs, and then select the operation.

Valid combinations

The following operations are mathematically valid.

  • number + number (returns a number)

n+n

  • number - number (returns a number)

n-n

  • number * number (returns a number)

n*n

  • number / number (returns a number)

n/n

  • number ^ number (returns a number)

n^n

  • vector + vector (returns a vector)

v+v

  • vector - vector (returns a vector)

v-v

  • vector * number (returns a vector)

v*n

  • number * vector (returns a vector)

n*v

  • vector / number (returns a vector)

v/n

Invalid combinations

If the user attempts any of the following invalid mathematical operations, the operations available in the block menu will change according to the inputs that have been entered. For example, if the user attempts to divide a number by a vector, the vector will be accepted, but the divide symbol will be changed to multiply, which is valid.

  • anything / vector
  • anything ^ vector
  • vector * vector
  • vector + number (or vice versa)
  • vector - number (or vice versa)

- (negative)

neg block

This block outputs a quantity that is the additive inverse, or "opposite", of the input argument. The input may be a vector or a number. The block includes a "shadow block" with a 1 in it because a program with the negative sign without a number to follow it would result in an error.

When the negative block takes a vector as an input, the result is also a vector (as indicated by the change in color of the negative block from turquoise to blue). The new vector is equal in magnitude to the original, but exactly opposite in direction.

neg vector

remainder

This block performs the modulo operation between two numbers, producing the remainder when one number is divided by another. For example, 11 / 4 is 2 with a remainder of 3, so the block shown below would output 3.

remainder block

Arithmetic involving one number input

single input arith

square root

The square root block returns the square root of any non-negative (zero or positive) number.

absolute

The absolute block returns the absolute value of any number.

- (negative)

This block outputs a quantity that is the additive inverse, or "opposite", of the input argument. The input may be a vector or a number.

ln

The ln block returns the natural logarithm of a non-negative (zero or positive) number.

log10

The log10 returns the base ten logarithm, or common logarithm of a non-negative (zero or positive) number.

e^

The e^ block takes any number as input and returns the mathematical constant e raised to the power of the input number.

! (factorial)

The ! block returns the factorial of the input number. The input must be a non-negative integer (i.e., 0, 1, 2, 3...)

10^

The 10^ block takes any number as input and returns 10 raised to the power of the input number.

Trigonometry

trigonometric functions

trig functions

The trigonometric functions block returns the sin, cos, tan, asin, acos, atan of any number. The functions asin, acos, and atan are inverse trigonometric functions. In the case of sin, cos, or tan, the input number is assumed to be in radians. In the case of asin, acos, or atan, the output number is in radians.

radians

radians block

The radians block takes a number of degrees as input and returns the equivalent number of radians. The menu inside the block can be changed to degrees, in which case the block takes a number of radians as input and returns the equivalent number of degrees.

Miscellaneous

special constant

math spec const

The special constant block provides quick access to commonly-used special mathematical constants: pi, e, phi, square root of 2, and square root of one half.

random fraction

random fraction

The random fraction block generates a random decimal number between 0 and 1.

math logic

math logic

The math logic block evaluates the truth of the mathematical statement that is constructed and returns a Boolean True or False. The user inputs a number on the left and selects a condition using the pull-down menu on the right.

round

round block

The round block takes a number as input and rounds it, returning the rounded number. The user can select round, round up, or round down using the block menu. If round is selected, the number will be rounded to the nearest integer.