kent logo

CO538 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for negative-numbers


Question 22 (2011):

Submission reference: IN2052

Hi, I'm having some problems with negative numbers and IF statements. For example, if I have the following statement:

      x = 1
        x := -1
        x := 1

The compile throws no problems, and the program works okay (ish but for other reasons), importantly it does use "x" as a negative number. However if I flip this around to...

      x = -1
        x : = 1
        x := -1

I get:

    Error-occ21-/..{directory}../src.occ(428)- expected operand, found "-"

I don't understand why I can use the negative symbol in that context in the assignment, but not in the IF statement?

Answer 22:

This is quite a common problem, but straight-forward enough solution. When you write "-x", for instance, you're actually applying the unary minus operator to "x". Same as when you write "-1", where you're applying the minus operator to the constant 1 (one) instead. This is an expression ... so, when it appears as part of another expression, it must be bracketed.

An assignment has the form "l-value := expression". So, if the whole expression is "-1", it needs no bracketing.

In an "IF" construct, each condition is its own expression (with boolean value). Now, the whole of "x = -1" is such an expression (very different from an assignment) and, to be correct, needs bracketing, i.e. "x = (-1)".

It may seem slightly silly, as there is no misinterpretation possible in that case; but consider something like: "-x + 42". Should that mean "(-x) + 42" or "-(x + 42)"? Being explicit about what you mean is good programming practice, hence the need for bracketing sub-expressions :-).

Keywords: negative-numbers


Question 26 (2003):

Could you tell me why the following cose is outputting negative numbers?

    [snip code]

I really dont understand what's wrong with it!

Answer 26:

See the answer to Question 24 (2003). That should explain it.

Keywords: negative-numbers

Question 24 (2003):

How is it possible that If I run the process (without inputting anything) at a certain point I start getting negative numbers for the 2nd and 3rd column ?

Answer 24:

This is quite possibly normal behaviour. In standard 32-bit 2's complement signed arithmetic:

    1500000000 PLUS 1500000000 = -1294967296

In occam, you must use the `PLUS' operator to get this behaviour. The standard `+' operator will cause a run-time error when this happens, which is generally what you want.

You might want to consider slowing the output down with a suitable delay. See Question 13 (2000) for how to do this. In relation to Q4, the initial values output by the program should be correct, until something overflows that is. If the output goes negative very early on, then something is probably wrong -- check the values!

Keywords: overflow , negative-numbers

Referrers: Question 26 (2003)


Question 39 (2000):

In the specification for the p bit in q4.occ, it says: "take the output arriving from the tail from that arriving directly from the delta". I'm certain, well confident, well satisfied, ... ERM.. at least I think I've implemented it as specified, but my output gives:


I am very tempted just to switch the channels going into plus so that it gives:


when its in subtractor mode. Or are the negative numbers actually what's required ... i.e. the assignment means do as I say not do what you think I mean ... ?

Answer 39:

You've got me here! In question 4 on the exercise sheet, it doesn't attempt to specify which way around the minusing should operate (in response to a p from the keyboard). So, doing it either way round would be OK.

In the q4.occ file, I tried to make this precise but - given the complexities of natural language - managed to say it the wrong way around :-( ... Apologies for this. So, doing it either way round - what I said or what I meant - is OK.

Keywords: q4 , negative-numbers

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:28 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.