Data Representation
Types you define yourself, how the records in a file are organised and found, and how a computer stores fractions in floating point.
WATCH IT EXPLAINED
Types you make yourself
When no built-in type fits your data, you build your own from them: a user-defined type.
A language has built-in types: INTEGER, REAL, CHAR, STRING, BOOLEAN and DATE. When none of them fits the data, the programmer builds a new type from them. This is a user-defined type.
Why make one?
- It gives you a type the language does not have.
- The translator (compiler or interpreter) can refuse values that make no sense.
- Data that belongs together stays under one name.
User-defined types come in two families:
| Family | What it holds | Types |
|---|---|---|
| Non-composite type | one single value | enumerated type, pointer type |
| Composite type | a collection of data, of one or more types, under one identifier (name) | record, set, class |
// enumerated TYPE Direction = (North, East, South, West) // pointer TYPE TDirPointer = ^Direction // set TYPE LetterSet = SET OF CHAR // record TYPE Book DECLARE Title : STRING DECLARE Copies : INTEGER ENDTYPE
Enumerated types: a list of allowed values
An enumerated type is an ordered list of every value a variable of that type may hold.
An enumerated type is a non-composite type: it holds one value. It is defined by an ordered list of every value it may hold.
- The list is ordered, so each value has a position. So the value after East can be worked out.
- A variable of the type can only ever hold a value from the list. So the type validates the data for you.
Watch out:The values have no quote marks. North is a value of the type, but "North" would be a string.
TYPE Direction = (North, East, South, West) DECLARE Heading : Direction Heading ← North Heading ← Heading + 1 // the next value: East // West + 1 has no value: // the list ends at West
Pointers: a variable that holds an address
A pointer variable holds the memory address of another variable, not a value.
A pointer type is a non-composite type. Its variables hold the memory address (the place in memory) of another variable, not a value. It is like a home address written on paper: it tells you where the house is, but it is not the house.
| Written | Means |
|---|---|
TYPE TDirPointer = ^Direction | "points to a Direction" |
^Heading | the address of Heading |
P^ | the value at the address held in P |
So changing P^ changes Heading itself, because they are the same memory location.
Watch out:The pointer variable is declared without the ^: DECLARE P : TDirPointer.
Pointers are how the nodes of linked lists and trees are joined together (topic 19).
TYPE TDirPointer = ^Direction DECLARE Heading : Direction DECLARE P : TDirPointer Heading ← North P ← ^Heading // P holds the ADDRESS of Heading P^ ← West // so Heading is now West
Records, sets and classes: composite types
Records, sets and classes are composite types: each groups data under one identifier.
A composite type groups data of one or more types under one identifier.
- A record groups named fields of any types. Declare it with TYPE and ENDTYPE. Reach each field with a dot:
B.Title. - A set holds values of one type, with no value twice and no order. Declare it with SET OF, and give it members with DEFINE.
- A class is like a record that also has its own methods (topic 20).
With a set you can:
- test whether a value is in the set
- combine sets by union, intersection or difference
Remember:Facts about one thing suit a record, like a book's title and copies. A group you test for membership suits a set, like the vowels.
TYPE Book DECLARE Title : STRING DECLARE Copies : INTEGER DECLARE Fiction : BOOLEAN ENDTYPE TYPE LetterSet = SET OF CHAR DEFINE Vowels ('A', 'E', 'I', 'O', 'U') : LetterSet
Three ways to organise a file
A file keeps records in arrival order (serial), in key order (sequential), or at hashed addresses (random).
| Serial file | Sequential file | Random file | |
|---|---|---|---|
| How records are stored | one after another, in the order they arrive | in order of a key field (the field that identifies a record, such as a student ID) | each at an address worked out from its key by a hashing algorithm |
| A new record | goes at the end | must go in its correct place, so a new version of the file is written | can be written and found directly |
| Best for | just recording events, as in a log of transactions | using every record in order, as in a payroll run | finding one record quickly, as in a stock lookup |
Exam:Choose the organisation by how the file is used.
Finding a record: sequential and direct access
Sequential access reads records from the start; direct access goes straight to the one record you want.
Sequential access reads records one after another from the start.
- In a serial file, it is the only way to search. Read until the record is found or the file ends.
- In a sequential file, you can stop as soon as you pass the key you want, because the keys are in order.
Direct access goes straight to one record. It does not read the ones before it.
- In a random file, hash the key to find its address.
- In a sequential file, use an index of the keys.
Exam:Compare the methods by counting how many records each one reads.
Hashing: the key becomes the address
A hashing algorithm turns a record's key into the address where the record is stored.
A hashing algorithm turns a record key into the address where the record is stored. For example: key MOD 7, for a file of seven slots. To read the record later, hash the key again and go straight there.
Example:Key 23 MOD 7 = 2, so the record with key 23 goes in slot 2.
A collision happens when two keys hash to the same address. There are two ways to handle it:
| Closed hashing | Open hashing | |
|---|---|---|
| Where the new record goes | in the next free slot, wrapping round to the start | in a separate overflow area |
| How a search works | read on from the hashed slot until you find the key or an empty slot | search the overflow area in order |
Remember:A good hashing algorithm spreads the keys evenly, so collisions are rare.
// store NewRecord in the random // file Stock.dat Address ← NewRecord.Key MOD 7 OPENFILE "Stock.dat" FOR RANDOM SEEK "Stock.dat", Address GETRECORD "Stock.dat", Slot // 0 marks an empty slot WHILE Slot.Key <> 0 // try the next slot Address ← (Address + 1) MOD 7 SEEK "Stock.dat", Address GETRECORD "Stock.dat", Slot ENDWHILE SEEK "Stock.dat", Address PUTRECORD "Stock.dat", NewRecord CLOSEFILE "Stock.dat" // assumes the file is not full
Floating point: a number and where its point goes
A real number is stored as a mantissa and an exponent, like standard form but in base 2.
A real number is stored in two parts, both in two's complement:
- the mantissa: the bits of the number
- the exponent: how many places the binary point moves
The value is mantissa × 2^exponent. It is like standard form, but in base 2.
In the mantissa, the binary point sits just after the first bit:
| Bit | 1st | 2nd | 3rd | 4th | after that |
|---|---|---|---|---|---|
| Worth | −1 | 1/2 | 1/4 | 1/8 | and so on |
The exponent is an ordinary two's complement integer. The point moves right if the exponent is positive, and left if it is negative.
Remember:This lesson uses an 8-bit mantissa and a 4-bit exponent all the way through.
mantissa (8 bits)
−1 . 1/2 1/4 1/8 1/16 1/32 1/64 1/128
0 . 1 0 1 1 1 0 0
exponent (4 bits)
−8 4 2 1
0 0 1 1
mantissa = 1/2 + 1/8 + 1/16 + 1/32
= 0.71875
exponent = 3
value = 0.71875 × 2^3 = 5.75Denary to normalised floating point, step by step
Write the number in binary, move the point to 0.1…, then fix the sign and the exponent.
- Write the number, without its sign, in binary with a binary point.
- Move the point until the number reads 0.1… The number of places moved is the exponent: positive if you moved left.
- Keep the first 8 bits as the mantissa. Add zeros at the end if it is short. Bits that do not fit are lost.
- For a negative number, take the two's complement of the whole mantissa. The exponent does not change.
- A negative mantissa must start 10. If it starts 11, shift it left once more and subtract 1 from the exponent.
- Write the exponent in two's complement.
Example:+5.75 is 101.11 in binary, which is 0.10111 × 2^3. So the mantissa is 01011100 and the exponent is 0011.
+5.75 = 101.11
= 0.10111 × 2^3
point moved 3 places left
mantissa 01011100 exponent 0011
−5.75: two's complement of 01011100
invert → 10100011, add 1 → 10100100
mantissa 10100100 exponent 0011
starts 10: normalised
−4: +4 = 0.1 × 2^3 → 01000000
two's complement → 11000000
starts 11, so shift left → 10000000
exponent 3 − 1 = 2
mantissa 10000000 exponent 0010
−1 × 2^2 = −4Normalising: shift, and pay for it in the exponent
A normalised mantissa starts 01 or 10; every shift left costs 1 from the exponent.
| The number is | A normalised mantissa starts |
|---|---|
| positive | 01 |
| negative | 10 |
Watch out:"Starts with 1" is not the rule.
To normalise, shift the mantissa left until it starts 01 or 10. Subtract 1 from the exponent for every place you shift. So the value does not change.
Why normalise?
- It gives the most precision the bits allow. No bits are wasted on extra leading 0s or 1s.
- It gives every number exactly one pattern.
Zero cannot be normalised. It is stored as all zeros.
00011000 0101 = 0.1875 × 2^5 = 6
starts 00: not normalised
01100000 0011 = 0.75 × 2^3 = 6
2 shifts left, exponent 5 − 2 = 3
11110100 0011 = −0.09375 × 2^3 = −0.75
starts 11: not normalised
10100000 0000 = −0.75 × 2^0 = −0.75
3 shifts left, exponent 3 − 3 = 0Range against precision
With a fixed number of bits, more mantissa bits give more precision, and more exponent bits give more range.
- Precision is how exactly a number can be stored.
- Range is how big and how small the numbers can be.
| Give more bits to the... | You gain | You lose |
|---|---|---|
| mantissa | more precision: more significant bits, and smaller gaps between the numbers you can store | range: it is smaller |
| exponent | a larger range, reaching bigger and smaller numbers | precision |
Example:With 8 mantissa bits and 4 exponent bits, the largest number is 127. The smallest positive normalised number is 1/512.
If a number needs more bits than the mantissa has, the lowest bits are lost. Then precision falls.
Approximation, rounding, overflow and underflow
Many fractions cannot be stored exactly in binary, and some numbers are too big or too small to store.
Many denary fractions have no exact binary form.
- 0.1 is 0.000110011… in binary, recurring, just as 1/3 recurs in denary.
- With 8 + 4 bits, 0.1 is stored as 0.099609375, because the bits that do not fit are cut off.
- Each calculation on such values can add more rounding error.
Example:In Python, 0.1 + 0.2 gives 0.30000000000000004. So an amount of money that must be exact should not be stored as a binary fraction.
Two more problems:
- Overflow: a number is too large for the biggest exponent.
- Underflow: a number is too close to zero for the smallest exponent. It is stored as 0.
KEY TERMS
CHECK YOURSELF
1.Why is a floating-point number normalised?
2.More bits given to the exponent means:
3.Which file organisation stores each record at an address worked out from its key?
4.A transaction log written in the order events happened is which organisation?
5.Which access method finds one record in a random file?