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 power


Question 42 (2007):

Submission reference: IN1369

What is the operator for raise-to-the-power? For example, 2^32 – what does occam-pi have for ^?

Answer 42:

occam-pi does not have a buiit-in raise-to-the-power operator. There is a built-in function for floating-point raise-to-the-power::


which returns x^y ... but I don't think you want that!

[By the way, the built-in adjective above just means that we don't have to import any module to use it – in the same way that java.lang classes don't need to be imported.]

The actual number 2^32 is, of course, too high to be represented by 32-bit integers. I'm a bit worried by various large numbers being mentioned (see also the 360 degrees, for angular velocities, in Question 40 (2007)). There is no need for large numbers in this exercise.

If we really need a power operator for integers, we can define our own:

    --* This computes a restricted integer power (not very efficiently).
    --  The power size must be positive.
    --  If the numbers are too large, there will be arithmetic overflow.
    -- @param x The number to be raised
    -- @param y The raising power (must be >= 0)
    -- @return [@ref x]^[@ref y]
      INT result:
          result := 1
	  SEQ i = 0 FOR y
            result := result*x
        RESULT result

To use this function, just use the specified symbol as an infix operator – for example:

    answer := n^i

This demonstrates two further mechanisms in occam-pi (that are not part of this course module): in-lining and user-defined operators.

The in-lining doesn't change any semantics – it just removes the function call overhead (inserting code for the body of the function wherever the call is made).

We can build new operators as single or double symbols, using any exisiting operator symbol (or ^, @, &, %, ...). As is normal, operator symbols can be overloaded – but each operator should be defined for a unique operand-type-result-type signature (otherwise, usual block-structure scope rules hide the previously defined operator for that signature). Operators are either infix binary or prefix unary (respectively two-parameter or one-parameter operator functions). If we put such things in some appropriate occam-pi module, the language acquires new operators. To this extent, occam-pi is user-extensible.

But this is an aside ... I still don't think you need a power operator. What you might need (and which the code already uses in compute.speed) is left-shift, <<, and right-shift, >>. So long as it doesn't overflow:

    n << i

shifts the bit pattern in n left by i bits. If n were positive, this multiplies it by 2^i (so long as there is no numeric overflow). Similarly:

    n >> i

shifts the bit pattern in n right by i bits. If n were positive, this divides it by 2^i (rounding towards zero). Maybe this is what you are after?

Keywords: cylons , operators , power

Referrers: Question 43 (2007)

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:29 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.