Arithmetic and String Operators

From NWN Lexicon
Jump to navigationJump to search

Numeric operators are for addition, subtraction, multiplication, division, and other arithmetic operations. The following operators can be used in the Toolset:

Arithmetic Operators

These symbols can perform operations on float and/or int data types.

Symbol Operation Description
+ Addition Simple addition.
-- Decrement Decrements the value of a variable by one.
/ Division Simple division. Division by 0 results in the classic "Divide by zero" error.
++ Increment Increments the value of a variable by one.
* Multiplication Simple product. Multiplication by zero always results in 0, multiplying by 1 always returns the number being multiplied.
% Modulo Calculates the remainder of a division statement (not the result; works on integers only).
- Subtraction Simple subtraction.

String Operators

These symbols can perform operations on the string data type.

Symbol Operation Description
+ Concatenation Concatenates, or appends, one string to another.

Assignment Operator

The assignment operator ("=") is used to assign the value of an expression on its right side to a variable on the left.

int nSomeNumber = 23;
int nAge = nSomeNumber;

If the data type of nSomeNumber does not evaluate to the data type of nAge, then nSomeNumber will need to be cast using the appropriate type casting/conversion function.

When doing numeric assignments it's important to make sure that the result is stored in the right data type. This is mainly an issue with division, in which the decimal value needs to be preserved. If assigned to an int variable, the result will be truncated (everything after the decimal point will be lopped off).

int nResult = 10 / 3;

After the above expression nResult will equal 3.

float fResult = 10 / 3.0;

After this expression fResult will equal 3.333333254

Concatenation Operator

The only operator that will work on string data is the concatenation operator ("+").

string sFullName = "John" + " " + "Doe";
// sFullName will equal "John Doe"

Data casting (converting one data type to another) is important when concatenating string and numeric data. The concatenation operator will only work on string variables. For instance:

// incorrect
int nTime = GetTimeHour();
string sTime = "The time is: " + nTime;

This example will not compile, and will return the error "ARITHMETIC OPERATION HAS INVALID OPERANDS", because nTime is of type int.

// correct
int nTime = GetTimeHour();
string sTime = "The time is: " + IntToString(nTime);

nTime was cast to string using the IntToString() function. This time it will compile correctly.

Augmented Assignment Operators

An augmented assignment combines the assignment operator with other symbols to shorten assignment operations involving an arithmetic or concatenation operator. For example, the following two expressions are identical.

x = x + y;
x += y;

Both add the variables x and y, then assign the value to x.

In practice, usage of augmented assignment looks something like this:

int nDays  = 4;
    nDays += 7; // nDays now equals 11

The following arithmetic operations can be used with augmented assignment:

Symbol Operation
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= Division and assignment
%= Modulus and assignment

Here are some examples of their usage:

x += y; // Equivalent to x = x + y;
x -= y; // Equivalent to x = x - y;
x *= y; // Equivalent to x = x * y;
x /= y; // Equivalent to x = x / y;
x %= y; // Equivalent to x = x % y;

Augmented assignment also works with the string concatenation operator ("+"):

string sName  = "Tom";
       sName += " Cassiotis"; // sName now equals "Tom Cassiotis"

Increment and Decrement Operators

The increment ("++") and decrement ("--") operators are a special case of augmented assignment operators. They increase or decrease, respectively, the value of a variable by 1. For example:

x++; // equivalent to x = x + 1;
x--; // equivalent to x = x - 1;

The increment and decrement operators act differently depending on where they are placed in relation to the variable. If placed before the variable (this is called "pre-increment"), the incrementation is done and then the variable is used; if placed after it ("post-increment"), the current value is used and then the incrementation is done. For example:

int x;
int y;
 
// Increment operators
x = 1;
y = ++x; // x is now 2, y is also 2
y = x++; // x is now 3, y is 2
 
// Decrement operators
x = 3;
y = x--; // x is now 2, y is 3
y = --x; // x is now 1, y is also 1

Pay attention to whether you are using pre- or post-increment operators, as using the wrong one may yield unexpected performance. Further example using pre- and post-increment operators in function arguments:

void main()
{
    int nTest = 1;
    SpeakString(IntToString(nTest));   // display 1
    SpeakString(IntToString(++nTest)); // display 2
    SpeakString(IntToString(nTest));   // display 2
    SpeakString(IntToString(--nTest)); // display 1
    SpeakString(IntToString(nTest));   // display 1
    SpeakString(IntToString(nTest++)); // display 1
    SpeakString(IntToString(nTest));   // display 2
    SpeakString(IntToString(nTest--)); // display 2
    SpeakString(IntToString(nTest));   // display 1
}

author: Ryan Hunt, editor: Charles Feduke, additional contributor(s): Gerry Raban, Michael Nork, Grimlar, Squatting Monk