Difference between revisions of "Comparison Operators"

(adding stub)
 
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Doc Status D]]  <!-- For Lumina use, do not change -->
 
[[Category:Doc Status D]]  <!-- For Lumina use, do not change -->
 +
[[Category:Functions]]
 +
 +
 +
__TOC__
 +
 +
Comparison operators are written in infix-notation, with the operator appearing between the parameters, as in <code>x < y</code>. 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 <code>x = y</code>. To test whether two values are different, use <code>x <> y</code>, or equivalently <code>x &ne; y</code>. Because the &ne; character does not appear on most keyboards, <code>x <> y</code> is more commonly used.
 +
 +
A text value is always not equal to a number. So, for example, <code>5 = "5"</code> is false.
 +
 +
You can test for [[Null]] using <code>x = Null</code> or <code>x <> Null</code>, which always returns true (1) or false (0). There is a difference between <code>x = Null</code> and the function [[IsNull]](x) when <code>x</code> is an array. The operator <code>x = Null</code> test each cell of array and returns an array, indicating whether each cell is null, whereas the function <code>IsNull(x)</code> tests whether the entire value is the null atom, which returns false when <code>x</code> 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 <code>x = NaN</code>, since the result will be [[NaN]], and not true or false. To test for [[NaN]], you must use the [[IsNaN]] function.
 +
 +
== Inequality comparisons ==
 +
 +
Use <code>x < y</code> or <code>x > y</code> for strict inequality comparisons. <code>x<y</code> is true only when <code>x</code> is less that <code>y</code>, and is false if they are equal.
 +
 +
Use <code>x <= y</code> or <code>x >= y</code> for less-than or equal, or greater-than or equal comparisons. You may also use <code>x ≤ y</code> and <code>x ≥ y</code>, but these are less commonly used since they don't appear on most keyboards.
 +
 +
Inequality comparisons return [[Null]] when either parameter is [[Null]]. Even <code>Null <= Null</code> 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 <code>'5' < 3</code> is true and <code>'3' >= 3</code> 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 ==
 +
* [[Expression_Syntax#Operator_Precedence|Operator precedence]]
 +
* [[Logical Operators]]
 +
* [[Operators]]
 +
* [[Display of constraint results]]

Latest revision as of 21:00, 24 March 2016


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

Comments


You are not allowed to post comments.