Comparison Operators

Revision as of 21:00, 24 March 2016 by Bbecane (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Comparison operators are written in infix-notation, with the operator appearing between the parameters, as in x < y. The result is either 0 (for false), 1 (for true), NaN (for indeterminate) or Null. Indeterminate values only occur when at least one of the parameters is indeterminate. Null results only occur with inequality comparisons and only when at least one of the parameters is Null.

Comparison operators operate on atoms, so when either parameter is an array, each cell is pairwise compared and the result is an array having the union of the indexes of each parameter.

x = y and x <> y

To test whether two values are equal, use x = y. To test whether two values are different, use x <> y, or equivalently x ≠ y. Because the ≠ character does not appear on most keyboards, x <> y is more commonly used.

A text value is always not equal to a number. So, for example, 5 = "5" is false.

You can test for Null using x = Null or x <> Null, which always returns true (1) or false (0). There is a difference between x = Null and the function IsNull(x) when x is an array. The operator x = Null test each cell of array and returns an array, indicating whether each cell is null, whereas the function IsNull(x) tests whether the entire value is the null atom, which returns false when x is an array.

If either parameter is an indeterminate number (which displays as NaN), the result is an indeterminate number, and a warning occurs. This means you cannot test for NaN using x = NaN, since the result will be NaN, and not true or false. To test for NaN, you must use the IsNaN function.

Inequality comparisons

Use x < y or x > y for strict inequality comparisons. x<y is true only when x is less that y, and is false if they are equal.

Use x <= y or x >= y for less-than or equal, or greater-than or equal comparisons. You may also use x ≤ y and x ≥ y, but these are less commonly used since they don't appear on most keyboards.

Inequality comparisons return Null when either parameter is Null. Even Null <= Null return Null, despite them being equal.

An inequality comparison is indeterminate (returns NaN) when either parameter is indeterminate.

References cannot be meaningfully compared for inequality. A comparison between two references is Null.

All text values are ordered before all numeric values. So '5' < 3 is true and '3' >= 3 is false.

Text values are compared according to the TextLocale ordering, as specified in the TextLocale system variable, which reflects the end-user's spoken language conventions. In general, text comparisons are case-sensitive.

See Also


You are not allowed to post comments.