# Number Values

Integer : Number
Float : Number NEW in 3ds Max 9: In addition, three new Number value classes have been added to MAXScript in 3ds Max 9. Please see 64 Bit Values - Double, Integer64, IntegerPtr for details.

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

Literals

[-]{<digit>}[.{<digit>}[(e | E)[+ | -]{<digit>}+] -- decimal 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)
Double
Integer64

IntegerPtr

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.

### Trigonometric Functions

MAXScript supports the following standard trigonometric functions. Angles are represented in degrees. 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>

### Transcendental Functions

MAXScript supports the following standard transcendental functions. The result is always a float.

exp <number>

log <number>

log10 <number>

pow <number> <number>

sqrt <number>

### Random Value Methods

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.

### Angle Conversion Methods

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

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

### Bit Operation Methods

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.

bit.intAsFloat <integer>

Returns the integer value cast to a float value. Typically used for converting between integer and float values so that the remaining bit methods can be used on a float. Available in 3ds Max 8 and higher.

bit.floatAsInt <float>

Returns the float value cast to a integer value. Typically used for converting between integer and float values so that the remaining bit methods can be used on a float. Available in 3ds Max 8 and higher.

bit.swapBytes <integer> <integer byte1> <integer byte2>

Returns an integer with bytes byte1 and byte2 of value int swapped. Byte 1 is the lowest order byte. Available in 3ds Max 8 and higher.

bit.isNAN <float>

Returns true if the float is the reserved float value NaN (Not a Number). A NAN is generated when the result of a floating-point operation cannot be represented as a floating point number. An example of NAN is f = (1.0/0.0). Available in 3ds Max 8 and higher.

bit.isFinite <float>

Returns true if the float is any value other than NaN, negative infinity, or positive infinity. In those three cases, it returns false. Available in 3ds Max 8 and higher.

### 64 Bit Operation Methods NEW in 3ds Max 9

bit.doubleAsInt64 <double>

Converts a Double value into an Integer64 value.

bit.int64AsDouble <int64>

Converts an Integer64 value into a Double value.

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

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