Comparison Operators
Compare two values or a range of values.
Syntax
Usual comparison
value_1 comparison_op value_2
Range comparison
value_1 { < | <= } value_2 { { < | <= } value_n }…
Tolerance comparison
value_1 [ ~ ] comparison_op value_2 [ [ ~ ] comparison_op value_n ]… Tol tolerance
Parts
value_1
,value_2
- An expression with a value to compare.
comparison_op
- One of the comparison operators:
<
,<=
,>
,>=
,=
, or<>
. value_n
- For a range comparison, an expression of a type compatible with that of
value_1
andvalue_2
. ~
- Optional symbol before a comparison operator. It shows which comparisons have a tolerance. Not all comparisons must have a tolerance, but all such comparisons use the same tolerance in the full expression.
tolerance
- After
Tol
, a numeric expression to specify the floating-point error tolerance of the comparisons. See Tol Clause for more information.
Instructions
The table that follows shows the comparison operators and the different conditions that give results of True
or False
.
Operator | True if |
False if |
---|---|---|
< (Less than) |
|
|
<= (Less than or equal) |
|
|
> (More than) |
|
|
>= (More than or equal) |
|
|
= (Equal) |
|
|
<> (Not equal) |
|
|
Comparing numbers
TODO
Comparing floating-point numbers
You can use the keyword Tol
to make floating-point comparisons with a tolerance to rounding errors.
A comparison expression without this clause could possibly give incorrect results because of how calculated results can have small rounding errors.
The tolerance value is the maximum the two values can be different but compare as equal.
At times you could have an expression with a mixture of comparisons with and without a tolerance.
You can use a tilde (~
) written before the comparison operator to show which comparisons have a tolerance.
If you use a tilde, you must also supply a tolerance.
A tolerance is applicable only to equality-type comparisons
(<=
, >=
, =
, and <>
).
If you use a tolerance on an expression without one of these operators, you get a warning, and the tolerance is ignored.
Comparing strings
TODO
Range comparisons
You can find if a value is in a range with a special syntax.
You use two operators from the set of <
and <=
to connect the three values.
You put the values in the sequence (1) lower value, (2) test value, and (3) higher value.
This syntax is almost the same as that used by mathamatics.
It is almost the same as what follows:
value_1 { < | <= } value_2 And value_2 { < | <= } value_3
If you wrote code the same as above and value_2
is an expression with side effects, it can have unknown results.
The range comparison makes sure that each expression does not calculate more than one time.
Only the operators <
and <=
are correct in this type of comparison.
It is not a syntax error to mix these with the other comparison operators, but it is usually a programming error.
Each comparison will evaluate from left to right, then the Boolean result will be compared to the next value in sequence.
Thus, we do not recommend it.
Comparing objects
While you can use all the comparison operators on number and string values, object references are different. Objects can only compare for equality with the Is operator. This operator lets you know if two object references refer to the same object.
If an object boxes a number or string, you can use the usual comparison operators.
Number objects compare #Null
as zero.
String objects compare #Null
as the empty string.
Examples
If x ~= 4.5 Tol 1e-2 Then
End If
If 0.0 ~<= y ~<= 1.0 Tol 1e-8 Then
End If