When Maya executes arithmetic operations on constants and variables without a declared data type, it guesses the data type based on the values present. For instance, in this statement:
float $where = 1/2; // Result: 0
Maya treats 1 and 2 as integers because they have no decimal points. The expression divides integer 1 by integer 2. The integer result is 0 with a remainder of 1. Maya discards the remainder.
Because where is a float variable, Maya converts the integer value 0 to floating point value 0 (which is the same value), then assigns this value to where. To get the fractional component of the value, one of the integer operands needs to be converted to a float:
float $there = 1/2.0; // Result: 0.5
Maya treats 2.0 as a floating point number because it has a decimal point. The number 1 is converted to a float and a float division takes place resulting in the value 0.5 which is then assigned to there. Another way to retain the fractional part of the division is as follows.
float $youGo = float(1)/2;
Here the 1 value is converted to a float which causes the 2 value to also be converted to a float. This creates a float division, preserving the fractional part of the division.
Precision and maximum numerical sizes
For a string, matrix, or array, the maximum size is dependent only on the amount of memory available on your computer. Floats and ints, however, have limits on their precision and maximum size.
The maximum size of an int is the same as in the C language and is machine dependent. On most computers this range is from -2,147,483,648 to 2,147,483,647.
The maximum precision and range of a float is the same as a double in the C language and is machine dependent. Floats have limited precision, and round-off errors can accumulate in long calculations. However, since the precision for the float type is so high (about fifteen digits of accuracy), round-off errors usually do not become a problem.
Variables have limited ranges. When these ranges are exceeded, undesired results can occur.
int $mighty = 2147483647 + 1; // Result: -2147483648 int $radical = -2147483648 - 1; // Result: 2147483647 int $buddy = 2147483648; // Result: -2147483648 int $man = 2147483647 + 2; // Result: -2147483647
When the maximum range of a variable is exceeded, the value of the variable wraps around to the minimum range of the variable. Also, when the minimum range of a variable is exceeded, the value of the variable wraps around to the maximum range of the variable.
float $GG = 1.5 + 1000000000 * 3; // Result: -1294967294.5
In this example, the multiplication is done first due to operator precedence. The multiplication is performed on two ints, so the result type is an int. Because the value of this multiplication is over the maximum range of an int, the value wraps around.
The following calculation illustrates what is occurring internally:
$GG = 1.5 + 1000000000 * 3; $GG = 1.5 + 3000000000; // Maxumum int range exceded $GG = 1.5 + 3000000000 + (2147483648) - (2147483648.0); $GG = 1.5 + 3000000000 + (-2147483648) - (2147483648.0); $GG = 1.5 + 3000000000 - 4294967296; $GG = 1.5 + -1294967296; $GG = -1294967294.5;