Mathematical Operations in MAXScript

Besides strings, you can enter other types of data into MAXScript. MAXScript not only recognizes numeric characters as numbers, it can also perform mathematical operations with them. As with strings, if you just enter a number by itself, MAXScript echoes it back to you; it evaluates the number and displays its value. Mathematical operations are more interesting.

Basic Mathematical Operations

MAXScript has many built-in functions that let you use MAXScript as a calculator.

Enter 36.5 * 2, for example, to see what a certain dimension needs to be if you want to double it.

MAXScript returns the result of the calculation. This is useful for quick calculations where loading an external calculator program doesn't seem worth the trouble.

MAXScript also recognizes several mathematical constants. Enter pi.

MAXScript knows the value of pi and returns it.

You can use this value in more complex operations. For example, if you want to know the volume of a sphere with a 2.5-inch radius, multiply the cube of the radius by pi, then multiply by 4 and divide by 3. Enter 4/3 * pi * 2.5^3 .

Operations with Strings

You can also perform some simple mathematical operations with strings. For example, if you have defined a="MAXScript" and b=" is fun!," entering a+b returns "MAXScript is fun!"

For more string operations, see the String Values topic in the MAXScript reference.

Additional Mathematical Operations

MAXScript is able to perform many mathematical operations, including most trigonometric functions (sin, cosh, atan) and transcendental functions (exp, log, sqrt). This topic introduces two of the most useful operations: random numbers and incrementing. For a complete list of the operations supported by MAXScript, see the Number Values topic in the MAXScript reference.

Creating Random Numbers

One very useful mathematical operation in MAXScript is the random number function. It returns a pseudo-random number selected inclusively between two user-specified arguments. For example:

random 1 100

returns a random integer between 1 and 100. The return value type (Float or Integer) is the same as the type of the first argument in the function, therefore if you enter:

random 1.0 100

MAXScript returns a random float between 1 and 100.

Note: For reasons beyond the scope of this tutorial, the random command will generate the same 'random' numbers each time a script is run. This happens if you restart the software and run the script, but not if you run the script over and over. If you want the values created by the random function to change each time you start the software, you can use the seed command:

seed <number>

where <number> is any float or integer. Each time you change the seed value, MAXScript generates new 'random' numbers.

Incrementing

Another useful mathematical operation that MAXScript provides is incrementing. This is a shorthand form of assignment that can be used to modify a value in place, as shown in the following long form examples:

x = x + 1

There are assignment operators corresponding to the four math operations (+,-,*, and /) that apply the operation to the assignment's destination with the result. Their syntax is:

<destination> += <expr> -- add <expr> to destination

<destination> -= <expr> -- subtract <expr> from destination

<destination> *= <expr> -- multiply destination by <expr>

<destination> /= <expr>  -- divide destination by <expr>

Using this shorthand form of assignment, the previous example can be written as:

x += 1

Next Topic

Drawing a Box with MAXScript