# Relational operators

Jump to navigation
Jump to search

## Description

*Relational*refers to the relationship one value can have with another. For example, is one value equal (=) to another value? Or, is one value greater than (>) another value? The relational operators produce a result that is either true (1) or false (0).

- The JB relational operators list:

Operator Meaning **>**greater than **<**less than **>=**greater than or equal to **<=**less than or equal to **=**equal **<>**not equal

## Syntax

*expression*operator*expression*

- Expressions compared should be of the same type (both strings or both numeric).
- For details on string comparison order (> < >= <=), see String comparison.
- Result is boolean (logical). For JB, result happens to be 1 for TRUE and 0 for FALSE.

## Hints

- They are normally used as a condition in conditional statements and loops.

- The fact that the result of relational operators is strictly 0 or 1 allows some hacks and shortcuts, see Useful Procedures

- When comparing, if one of the numbers is a double-precision floating point number, and second number is an integer, both will be converted to double-precision. This could bring unexpected results or overflow error, see Examples.

## Example

- When comparing, if one of numbers is double and second integer, both converted to double.
- This could show values equal if difference exceeds double precision (16 digits)

longInt=12345678901234567890 '20 digits fmt$="####################" 'format for using doubleVal = longInt + 0.1 'adding fraction will make number double print doubleVal 'double prints in exponential form, 1.23456789e19 'now print initial longInt and doubleVal using fmt$ print longInt '12345678901234567890 print using(fmt$,doubleVal) '12345678901234567168 'They are obviously not equal 'BUT if longInt>doubleVal then print "more" if longInt=doubleVal then print "equal" if longInt<doubleVal then print "less" 'prints "equal" 'BECAUSE when comparing, both values were converted to double (16 digits only)

- Also, if comparing long integer with double, we can get runtime error - float overflow exception.

longInt = 10^400 Int2 = 10 double = 2.5 print longInt>Int2 'both integer - Ok print Int2>double 'small integer, converts to double - Ok print longInt>double 'JB tries to convert longInt to double - get float overflow exception (double range about +/-1e307)

## Useful Procedures

'returns 0 for 0, 1 otherwise (that is, normalized TRUE or FALSE) function normalize(num) normalize=num<>0 end function

'sign function: +1 for x>0, 0 for x=0, -1 for x<0 function sign(x) sign = (x>0) - (x<0) end function