Number Values

Integer : Number
Float : Number

The Number class provides standard arithmetic capabilities to MAXScript. The two Number types, Integer and Float, can be freely intermixed and MAXScript will convert as necessary. They both support all the following operators and methods. Also see Number Literals.

Literals

[-]{<digit>}[.{<digit>}[(e | E)[+ | -]{<digit>}+]  -- decimal number

0x{<hex_digit>}+                                   -- hexadecimal number

Examples:

123

123.45

-0.00345

1.0e-6

0x0E

0xFFE0

.1

Operators

<number> + <number>

<number> - <number>

<number> * <number>

<number> / <number>

Standard arithmetic operations. If both numbers are integers, the result is a non-rounded integer. If one or both numbers are floats, both arguments are widened to floats and the result is a float.

<number> ^ <number>

Raise the first number to the power given by the second number. If the first number is an integer, the result is a rounded integer.

- <number>

unary minus

<number> == <number>

<number> != <number>

<number> > <number>

<number> < <number>

<number> >= <number>

<number> <= <number>

Standard number comparison operations.

<number> as <class>

Converts the number to an instance of the given class. Allowed classes are:

Float

Integer

String

Time (number taken as frames)

Methods

copy <number>

Creates a new copy of the number value. This method exists primarily to support copying arrays.

abs <number>

Returns the absolute value of the number. The result is the same type as the argument.

mod <number1> <number2>

Modulo arithmetic, the remainder when number1 is divided by number2. The result is always a float.

ceil <number>

Returns the nearest whole number greater than or equal to number. The result is always a float.

floor <number>

Returns the nearest whole number less than or equal to number. The result is always a float.

acos <number>

asin <number>

atan <number>

atan2 <number> <number>

cos <number>

cosh <number>

sin <number>

sinh <number>

tan <number>

tanh <number>

Standard trigonometric functions. Angles are represented in degrees. The result is always a float.

exp <number>

log <number>

log10 <number>

pow <number> <number>

sqrt <number>

Standard transcendental functions. The result is always a float.

random <number> <number>

Returns a pseudo random number inclusively between the two arguments. Return value type is the type of the first argument.

seed <number>

Reseeds the random number generator using the specified value.

degToRad <number>

Returns the conversion of the number from degrees to radians. The result is always a float.

radToDeg <number>

Returns the conversion of the number from radians to degrees. The result is always a float.

bit.and <integer1> <integer2>

Returns the <integer> result of AND-ing the bits in integer1 and integer2

bit.or <integer1> <integer2>

Returns the <integer> result of OR-ing the bits in integer1 and integer2

bit.xor <integer1> <integer2>

Returns the <integer> result of XOR-ing the bits in integer1 and integer2

bit.not <integer1>

Returns the <integer> result of flipping the bits in integer1

bit.shift <integer1> <integer2>

Returns the <integer> result of shifting the bits in integer1 by integer2 places. If integer2 is positive, the bits are shifted to the left, if negative, to the right. The highest order bit is not carried when shifting to the left (unsigned shift operation).

bit.set <integer1> <integer2> <boolean>

Returns an <integer> result where bit integer2 of integer1 is set to the specified boolean value. Bit 1 is the lowest order (least significant) bit.

bit.flip <integer1> <integer2>

Returns an <integer> result where bit integer2 of integer1 is flipped. Bit 1 is the lowest order (least significant) bit.

bit.get <integer1> <integer2>

Returns the <boolean> state of the integer2 bit of integer1. Bit 1 is the lowest order (least significant) bit.

bit.intAsChar <integer>

Returns a <string> result of length 1 containing the character corresponding to the integer value. Only the lowest order 8-bits (16-bits for localized versions of 3ds max) of the integer are used.

bit.charAsInt <string>

Returns the <integer> value corresponding to the first character of the string.

bit.intAsHex <integer>

Returns a <string> value containing the hex representation of the integer.

Notes

The range of valid Integers in MAXScript is -2,147,483,648 to 2,147,483,647. If you perform calculations that result in integers outside of this range, you will get integer overflow errors that are not detected by MAXScript. You must take care in designing your code to prevent or detect this overflow yourself. The result of an overflow is typically a large number of the wrong sign. Dividing an integer by 0 will result in a MAXScript system exception.

A Float in MAXScript has an absolute value range of is 1.18E-38 to 3.40E38, with a precision of one part in 1.0E7. If you perform calculations that result in floats with an absolute value less than this range, the result will be stored as 0.0. If you perform calculations that result in floats with an absolute value larger than this range, the result will be stored as a special value that represents infinity, 1.#INF. Adding, subtracting, or multiplying a number by 1.#INF results in a value of 1.#INF. Dividing a number by 1.#INF results in a value of 0.0. Dividing 0.0 by 0.0 or 1.#INF by 1.#INF, multiplying 1.#INF by 0, or 1.#INF from 1.#INF results in a special value that represents an indeterminate number, -1.#IND. Adding, subtracting, multiplying, or dividing a number by -1.#IND results in a value of -1.#IND.

When you display or print a Float, MAXScript prints the value to the 6th significant digit. The following table shows examples of values stored to a MAXScript variable, the value as stored, and the value as displayed.

Input Value

Stored Value

Displayed Value

1.23456789 

1.23456788 

1.23457 

1.1 

1.10000002 

1.1 

1.01 

1.00999999 

1.01 

1.001 

1.00100004 

1.001 

1.0001 

1.00010001 

1.0001 

1.00001 

1.00001001 

1.00001 

1.000001 

1.00000095 

1.0 

1.0000001 

1.00000011 

1.0 

1.00000001 

1.00000000 

1.0 

100017.911 

100017.914 

100018.0 

Examples

The following script shows the use of various literals, constructors, properties, operators, and methods of the Number class.

Script:

-- numbers test bed

i=10               -- assign integers to variables

j=20

i/j                -- integer divide, result is integer

i=i as float       -- convert i to a float

i/j                -- float divide, result is float

i += 1             -- increment i by 1

if i < j do i=2.^3 -- if i is less than j, set i to 2 to the 3rd power

mod j i            -- return remainder of j divided by i

cos 30.0           -- return cosine of 30 degrees

sqrt j             -- return square root of j

seed 12345         -- seed the random number generator

for k=1 to 5 do print (random i j) -- print out 3 random numbers

Output:

10                        -- result of line 2

20                        -- result of line 3

0                         -- result of line 4 (10/20)

10.0                      -- result of line 5

0.5                       -- result of line 6 (10./20)

11.0                      -- result of line 7

8.0                       -- result of line 8 (2.^3)

4.0                       -- result of line 9

0.866025                  -- result of line 10

4.47214                   -- result of line 11 (sqrt 20)

OK                        -- result of line 12

10.7775                   -- output from line 13 (random 8. 20)

15.0183

17.4467

16.1027

10.1344

OK                        -- result of line 13