 Data and processes in computing

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

Free course

# 5.3 Comparison functions

Another situation where we use infix notation is in writing comparisons, such as x < y, where x and y are numbers. Such a comparison is either true or false. For example, 5 < 9 is true but 5 < 2 is false. To describe a corresponding function, we take the output set to be Bool = {true, false}. Thus the comparison < on integers corresponds to a function, which we can describe as follows.

function (infix) (x < Int y on Int) return in Bool

pre   true.

post   The returned value is true if x < y and is false otherwise.

Equality is also a comparison function. This is normally written using the infix symbol =. If x and y are numbers, then x = y is true if the numbers x and y are the same and is false if they are not. If one is working with real numbers on a computer, then one needs to be very careful with tests of equality. There will be an element of approximation in the way real numbers are stored, and this may result in a computer reporting real numbers as equal when they are only approximately equal. However, we shall confine our attention to integers, where there is no such problem in deciding whether two numbers are the same.

Sometimes we may wish to test to see whether two characters are the same. (This can be done by comparing their ASCII codes, for example.) A computer will need to work in a different way when comparing two integers for equality from that needed when comparing two characters for equality. These are different processes. Equality of numbers is a function with signature Int × IntBool; equality of characters, on the other hand, has signature Char × CharBool. Since these are different functions, we will give them different identifiers. We will write =Int for equality of integers and =Char for equality of characters.

function (infix) (x =Int y on Int) return in Bool

pre   true.

post   The returned value is true if the integers x and y are equal and is false otherwise.

function (infix) (x =Char y on Char) return in Bool

pre   true.

post   The returned value is true if the characters x and y are the same and is false otherwise.

One could give a binary operation < that compares characters by comparing their ASCII codes. Again, this is a different function from that of comparing integers (which is why we used a subscript Int in the identifier <Int for comparison of integers above).

Note that in mathematics the term binary operation would only be used for a total function whose return type is the same as the type of the inputs.

## Activity 27

Give a full description of an infix function corresponding to <Char comparing two characters by comparing their ASCII codes.

### Discussion

A description is given below.

function (infix) (x <Char y on Char) return in Bool

pre   true.

post   The returned value is true if ASC(x) <Int ASC (y) and is false otherwise.

In practice, a variety of different comparisons are used on integers (and other forms of data, such as characters), including: > (strictly greater than), ≤ (less than or equal), ≥ (greater then or equal), and ≠ (not equal). Functions may be described for each of these, but we shall not do so here.

M263_1

### Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371