Number Values

Value > Basic Data Values > Number Values

 

   

Values and Collections - Quick Navigation

Integer : Number Float : Number Double : Number Integer64 : Number IntegerPtr : Number

The last three Number value classes are available in 3ds Max 9 and higher. 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.

Also see Number Literals.

   

Literals

[-]{<digit>}[(.{<digit>}[(e | E | d | D)[+ | -]{<digit>}+) | L | P]  --decimal number
0x{<hex_digit>}+  --hexadecimal number

EXAMPLES

123
123.45
-0.00345
1.0e-6
0x0E
0xFFE0
.1

The number classes support the following operators and methods.

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>

Exponential function.

   

log <number>

Natural logarithm of the argument.

   

log10 <number>

Logarithm with base 10.

   

pow <number> <number>

First argument to the power of the second argument.

   

sqrt <number>

Square root of the argument.

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

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.

Float Close Enough Method

close_enough <float> <float> <int>

Returns true if the two <float> values are approximately equal, where increasing values of <int> increase the range defined as approximately equal. Available in 3ds Max 2008 and higher. Previously available in the Avguard Extensions.

A good value of <int> in most cases is 10.

EXAMPLE:

for d1 in#(1., 10., 100., 1000.) do
(
  print d1
  for i = 0 to 8 do
  (
   d2 = d1+(d1*10.^(-i))
   format "% : % : % : % : %\n" i d1 d2 (d1==d2) (close_enough d1 d2 10)
  )
)

RESULTS:

1.0
0 : 1.0 : 2 : false : false
1 : 1.0 : 1.100000024 : false : false
2 : 1.0 : 1.00999999 : false : false
3 : 1.0 : 1.001000047 : false : false
4 : 1.0 : 1.000100017 : false : false
5 : 1.0 : 1.000010014 : false : false
6 : 1.0 : 1.000000954 : false : true
7 : 1.0 : 1.000000119 : false : true
8 : 1.0 : 1 : true : true
10.0
0 : 10.0 : 20 : false : false
1 : 10.0 : 11 : false : false
2 : 10.0 : 10.10000038 : false : false
3 : 10.0 : 10.01000023 : false : false
4 : 10.0 : 10.0010004 : false : false
5 : 10.0 : 10.00010014 : false : false
6 : 10.0 : 10.00000954 : false : true
7 : 10.0 : 10.00000095 : false : true
8 : 10.0 : 10 : true : true
100.0
0 : 100.0 : 200 : false : false
1 : 100.0 : 110 : false : false
2 : 100.0 : 101 : false : false
3 : 100.0 : 100.0999985 : false : false
4 : 100.0 : 100.0100021 : false : false
5 : 100.0 : 100.0009995 : false : false
6 : 100.0 : 100.0000992 : false : true
7 : 100.0 : 100.0000076 : false : true
8 : 100.0 : 100 : true : true
1000.0
0 : 1000.0 : 2000 : false : false
1 : 1000.0 : 1100 : false : false
2 : 1000.0 : 1010 : false : false
3 : 1000.0 : 1001 : false : false
4 : 1000.0 : 1000.099976 : false : false
5 : 1000.0 : 1000.01001 : false : false
6 : 1000.0 : 1000.000977 : false : true
7 : 1000.0 : 1000.000122 : false : true
8 : 1000.0 : 1000 : true : true
OK

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 in 3ds Max 9 and higher

bit.doubleAsInt64 <double> 

Converts a Double value into an Integer64 value.

   

bit.int64AsDouble <int64> 

Converts an Integer64 value into a Double value.

NOTE:

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 thatare notdetected 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.

AFloat 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.

-- 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 3rdpower
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 

See Also