In the A level Syllabus 9618, you will need to know further advance data types. But before we go into detail, we classify the data types into 2 groups which can be further broken down into another 2 groups
There are two types of data types:
These are data types that are defined by the user or the programmer itself. This means that they are designed by users. Examples are record types, enumerated data types
These are data types that are already defined by the programming language itself, for example str is defined by the programming language as a string.
Each Group can be further broken to two more groups, depending on if the data type is independent or not...
These are data types that refer to other data types. For the record type, we can declare each field inside the record type as String, Real or Integer etc
TYPE RecordName DECLARE FIELDNAME:DATATYPE DECLARE FIELDNAME:DATATYPE DECLARE FIELDNAME:DATATYPE DECLARE FIELDNAME:DATATYPE . . ENDTYPE
Examples are important to remember and they include: Sets, Lists, Classes, Arrays, Record Types
These are data types that has no reference to other data types.
Examples include: Real, Date, String, Integer, Enumerated Data Type
This data type declares all the possible solutions or values it can take
For example, the variable "ANSWER" is declared using an enumerated data type and can only take the value A, B ,C or D. If a Value of E is given then an error would occur
TYPE DECLARE ANSWER=(A, B, C, D) ENDTYPE
When we use this variable we will declare the variable directly:
DECLARE Value: ANSWER
Another point to remember is that the enumerated values are not in "".
The order matters as there is a numerical weightage which means B is greater than A
We use Record types in pseudocode whereas classes in python and other high-level languages
A class gives us the layout or in other words, a record structure for each variable. It's easier to think that each variable is considered to be a record where there are many fields and each field is different
class MyClass: def__init__(self): self.__Pointer=1 self.__Value=""
This is similar to the record type.
It is considered to be a composite data type which is a built-in data type structure. You can store strings, number, or even boolean
What is so special about a set data type is that the values are not ordered or indexed, so it is not possible to extract them in order
It also can't have any duplicate values in the data structure. This can be useful especially when we need to store all unique values. You can convert an array to a set then back again
The Set Data type is mainly used to apply mathematical operations so the set data type has a set of mathematical operations it can work on
Arrays and lists have almost similar functions but, the only difference is that a list can hold different data types under a single variable whereas the arrays can store data of the same data type. But why then is an array a composite data type, this is because the array itself is a data type and it refers to another data type like string
You are familiar with the concept that we use two's complement to store numbers in the computer. There are two ways in which the computer can store Two's Complement numbers to represent signed real numbers, not just integers.
A thing to remember is that this has a fixed number of bits for the whole number part and number of bits for the decimal part.
0111 0011 Has 4 bits for whole and 4 bits for decimal Which can be converted to: 0111.0011 7 . 1875 7.1875
It is clear how we got the 7 but how did we get the decimal part. There is a way to do this
0 1 1 1 . 0 0 1 1 8 4 2 1. 0.5 0.25 0.125 0.0625 Behind the decimal is 2-1, 2-2, 2-3, 2-4
Remember this is the same principle in floating point, the only difference is the decimal place "." is changed
If we get a two's complement value like:
1010 0100 -8 + 2 + 0.25 =-5.75
We convert it to denary almost the same way. You can almost convert it to sign and magnitude form and do the same thing
1010 0100 → 11011010 1 | 101.1100 which is also -5.75
There is no fixed position for the decimal point or a fixed defined number of bits for whole and decimal. So we use something called the mantissa and the exponent
Do you remember the concept of scientific notation 1.6*10-19
The same is for floating point...
Let us convert the value 5.75 to floating point representation
First we write it in the sign and magnitude form
5.75 0101.1100 then put in the decimal point to either 0.1 when positive and 1.0 when negative 0.101 1100 so the dot moves 3 sides left then exponent part must have the value of 3 in other words: 0.1011100 * 23 gives us 0101.1100
The answer is overall 0.1011100 for mantissa and 0011 for exponent
The decimal doesn't necessarily have to be in the form of 0.1 or 1.0 for example:
01.1000 for mantissa 0010 means *22 0110.00 which is just 6
In this case, this called the non-normalised form or the mantissa is not in the normalised form
This normalisation is not the same as the one in databases
If it is in the normalised form then it must be either in starting with 0.1 or 10 not 11 or 00
For small numbers, the exponent is negative and when you're handling with negative numbers, you must always convert them to two's complement:
0101000 for mantissa 1110 for exponent
Remember these are always in two's complement so for the exponent 1110 it is -2. So must be multiplied by 2-2
0.00101000 then convert to denary which is 0.15625If the value is negative it is the same principle but we put it to 1.0
I always found converting to small negative values to mantissa very hard. So let us try out one:
10100010 for mantissa and 1110 for exponent
So this means 10100010 * 2-2 but how do we go back, a principle you need to remember is that adding leading 1's to a two's complement negative value doesn't change its value. For example 1000 is same as 11000, both gives -8
111 | 1010010 convert to sign and magnitude which is 100 | 0101110
It is important that you put leading 1's first then convert to sign and magnitude. Then move the decimal point 2 left so you get 1.0
1 | 000101110 is a very long negative small decimal place which you can try to find...
You don't need to go that in depth, all you need to know is:
1. More bits for mantissa, higher the precision
2. More bits for exponent, higher the range
Usually there is a compromise between these two factors
The main problems comes when the number stored is highly precise so it can not be represented causing a rounding error, also if the value is too great, it can cause an overflow error and make the system crash
We will take 8 bits for mantissa and 4 bits exponent as an example:
Types | Mantissa | Exponent |
---|---|---|
Highest Positive Value | 01111111 | 0111 |
Lowest Positive Value | 01000000 | 1000 |
Highest NegativeValue | 10000000 | 0111 |
Lowest Negative Value | 10111111 | 1000 |
These are things you might like. Clicking these ads can help us improve our free services in the future...