Number Operators
From Tech Artists Wiki
Below is a list of common number operations and functions, their descriptions, uses, and their implementation in a number of languages.
Contents |
[edit] +,-,*,/
If you need these to be explained, you are in the wrong field...
[edit] Absolute Value (| |)
[edit] Definition
An absolute value of a number is, simply, the positive value of said number. The absolute value of both 4.12 and -4.12 is 4.12.
[edit] Common Uses
Absolute value is useful for comparing two values when you don't want to take sign into account, such as if you want to compare the positions of two objects on either side of an axis.
[edit] Languages
- HLSL: abs(x)
- maxScript: abs x
- MEL: abs x; or abs <<x, y, z>>;
- Python:
[edit] Ceiling
[edit] Definition
Returns the nearest whole number greater than or equal to x. If x is 20.1, floor x returns 21.0. The opposite of Floor.
[edit] Common Uses
Ceil is useful when rounding numbers, and for preparing a number to cast, to make sure it is rounded appropriately.
[edit] Languages
- HLSL:
- maxScript: ceil x
- MEL: ceil x;
- Python:
[edit] Clamp/Min/Max/Saturate
[edit] Definition
All the above functions clamp the function in some way. In the case of Clamp, it would take number x and compare it to y and z.
If x <= y, then return y If x >= z, then return z otherwise return x
Min and Max work the same way but for only one value. Max is
if x >= y then return y, otherwise return x
Min is
if x <= y then return y, otherwise return x
Saturate is generally only for shaders, and will clamp the value to between 0 and 1.
[edit] Common Uses
These functions have a variety of uses, and are generic and obvious enough that they can't be listed here.
[edit] Languages
In all the following examples, x is the value being acted upon, and y and/or z are what it is being compared to.
- HLSL:
- min(x, y)
- max(x, y)
- saturate(x)
- maxScript:
- MEL: clamp y z x;
[edit] Exponent (^)
[edit] Definition
Raises a number to another number, another one I shouldn't have to explain, but it is here for the symbols.
[edit] Languages
The following raise x to the y power.
- HLSL: pow(x, y)
- maxScript:
- x ^ y
- pow x y
- MEL: pow x y;
- Python:
[edit] Floor
[edit] Definition
Returns the nearest whole number less than or equal to x. If x is 20.9, floor x returns 20.0. The opposite of Ceiling.
[edit] Common Uses
Floor is useful when rounding numbers, and for preparing a number to cast, to make sure it is rounded appropriately.
[edit] Languages
- HLSL:
- maxScript: floor x
- MEL: floor x;
- Python:
[edit] Modulo (%)
[edit] Definition
In computing, the modulo operation finds the remainder of division of one number by another.
Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a by n. For instance, the expression "7 mod 3" would evaluate to 1, while "9 mod 3" would evaluate to 0. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands.
Wikipedia page on Modulo, actually very useful even for the layman.
[edit] Common Uses
Modulo returns a remainder between two numbers, which means it is what one would use to 'cycle' through numbers, whether it means cycling back to the beginning of an array when one has reached the last index, or going to the next row of pixels after sampling the last pixel, etc.
[edit] Languages
- HLSL: x % y
- maxScript: mod x y
- MEL: fmod x y;
- Python:
[edit] Square Root
[edit] Definintion
A square root of a number x is a number r such that r2 = x, or in words, a number r whose square (the result of multiplying the number by itself) is x. Every non-negative real number x has a unique non-negative square root, called the principal square root and denoted with a radical symbol as √x. For example, the principal square root of 9 is 3, denoted √9 = 3, because 32 = 3 × 3 = 9. If otherwise unqualified, "the square root" of a number refers to the principal square root: the square root of 2 is approximately 1.4142.
[edit] Common Uses
[edit] Languages
- HLSL: sqrt(x)
- MaxScript: sqrt x
- MEL:
- Python:
Categories: Math | Code
