Comparison operator

A comparison operator is used to compare the values of two expressions, primarily in the context of a conditional expression. In NWScript, the comparison operators are  ,  ,  ,  ,  , and  .

The basic comparisons are those for equality and inequality. The equality operator will evaluate to a true value if the expression on its left is the same as that on its right. Similarly, the inequality operator will return a true value if the values on either side of it are different. These operators can be used for any type of data, but data types (other than s and  s) cannot be mixed. For example, the comparison  is an error because it tries to compare an integer (5) to a string ("5"). There are special considerations when comparing some data types.

Floating point numbers require some care with regards to (in)equality, as these values often suffer from some form of round-off error, even in cases that might appear simple. (For example, the value 2.1 is stored by the computer as slightly more than 2.100000001) In most cases, floating point tests for equality and inequality should only occur when the value is being compared to 0.0. When the comparison is to other values, or is between variables, an inequality operator is usually more appropriate.

Also warranting care are the data types specific to Neverwinter Nights, such as the effect type. These types are instantiated, with the consequence that values of these types are equal only when the values refer to the same instance. Two instances can be equivalent in every way, but if they are not the same instance, they are not equal. For example, the expression EffectDaze == EffectDaze is false since each invocation of  returns a different instance of a daze effect. (This could be seen as analogous to currency. Two bills of the same denomination are worth the same amount of money&mdash;they are equivalent&mdash;but they are not the same bill; they are not equal. In this analogy, the computer would be comparing the serial numbers of the bills rather than the monetary values.)

The remaining comparison operators are the inequality operators, which evaluate the usual order of numeric values. Since they are tied to numeric order, they only operate on integer and floating point data. The strict "less than" operator evaluates to a true value if the value on its left is less than the value on its right. Similarly, the strict "greater than" operator checks for the value on its left being greater than the value on its right. The weak inequality operators ( and  ) additionally return a true value if the values on either side are equal.

Order of operations
Comparison operators are evaluated after arithmetic and shift operators but before bitwise and logical operators. This is an order that seeks to minimize the number of required parentheses. For example, 3 + x <= y &&  z == "test" is evaluated as ((3 + x) <= y) &&  (z == "test") which is probably what was intended. When relevant (this is an uncommon circumstance), inequality operators are evaluated before both equality and inequality operators, but otherwise comparison operators are evaluated left-to-right.