Comparison Operators

The following comparison operators are available:

Operator

Returns true if:

==

the values are equal 

!=

the values are not equal 

<

the first value is less than the second. 

<=

the first value is less than or equal to the second.

>

the first value is greater than the second. 

>=

the first value is greater than or equal to the second.

When comparing two different types of values (eg: comparing a bit of text with a number) the values will automatically be coerced as need so that the comparison makes sense. For example:

$number = 3

$text = "3.14"

if $text > $number

Prompt "Yes $text is larger than $number"

end

Text Comparisons

If either value is a string then a case sensitive string comparison is made using the string’s character values (Unicode code points). Attributes are ignored during comparisons (eg: hello is equal to hello). For additional ways to compare strings, see the Text object’s compare command.

Floating Point Comparisons

Testing decimal values like 3.14 for exact equality using the == operator can product unexpected results. That’s due to floating point rounding errors that may accrue during calculations, or perhaps because of internal storage differences. 

For example a value logically representing 1.1 may be stored internally as either of the equivalent values 1.1000000000000001 or 1.0999999999999999. If your macro code makes a direct comparison of such values using the == operator they may be unequal.

If you need to test if two decimal values are equal it’s almost always better to use the EqualFloats command. That command tests an equality threshold, instead of requiring exact equality.

EqualFloats float1, float2, tolerance v3.0

Returns true if the values differ by less than the given tolerance. Returns false if they exceed the tolerance. Using this command is like testing if:

ABS(float1 - float2) <= tolerance

For example, if you are comparing two line height multiples you might want them to be within 0.1 of each other:

$require = 1.5

$attrs = TextSelection.active.displayTypingAttributes

$factor = $attrs.lineHeightMultiple

If EqualFloats($require, $factor, 0.1)

Prompt "The line heights are equal enough."

End


Previous Chapter
Operators
<<  index  >>
 
Next Chapter
Boolean Operators