13. Data Representation
1a. Floating-Point Numbers
Fixed-point binary numbers:
● Fixed point binary numbers have a predetermined number of bits before and after the point
● This makes fixed point numbers:
○ easier to process
○ but they cannot represent the range or accuracy of numbers that may be required
■ range is due to upper limit of places
■ accuracy/precision is due to lower limit of places
Floating-point numbers:
● When decimal numbers become very large, they are held in the format m x 10n (denary) or m x 2n
(binary) where m is known as the mantissa, and n is the exponent - this allows a much larger
range of numbers to be represented in the same number of bits (than fixed-point)
● E.g. decimal: the number 75,000 can be represented as 0.75 x 105
● A greater bit allocation for the mantissa enables numbers to be represented more precisely
● A greater bit allocation for the exponent enables a larger range of numbers to be represented
To convert from floating-point binary to decimal:
● Calculate exponent in denary (remembering that MSB will be negative)
● Apply a shift by that many positions to the mantissa
○ Negative exponent → decimal point shifts left
○ Positive exponent → decimal point shifts right
● Convert number into decimal (remembering MSB is negative, and new position of decimal point)
Normalisation:
● Process of moving the binary point of a floating-point number, so the first digit after the binary
point is a significant digit (0.1 for positive number, 1.0 for negative number)
● Purposes of normalisation:
○ Normalisation minimises the number of leading zeros/ones represented
○ Maximising the precision/accuracy of the number for the given number of bits
○ Maximising the range of numbers in the minimum number of bytes/bits
○ Avoids the possibility of many numbers having multiple representations
Rounding errors:
● In the binary system some numbers (fractions) cannot be represented exactly, Binary represents
numbers based on powers of 2, giving limited fractional representations
● It isn’t possible to represent all fractional numbers exactly, resulting in a loss of precision due to
the rounding error - the fractional part of the number is as close as possible within the constraints
● E.g. 0.110 in 8 bits can only be represented by 0.0001100(0.0625 + 0.0325)
bsolute error - bigger value minus the smaller value
A
Relative error - absolute error divided by the intended value
Overflow/underflow errors:
● Overflow - following an arithmetic/logical operation, the result is too large to be represented in
the number of bits allocated, resulting in truncation of the most significant bit
● Underflow - following an arithmetic/logical operation, the result is too small to be precisely
represented in number of bits allocated
, 2. User-Defined Data Types
User-defined data types:
● Programmer constructs a new (non-primitive) data type (from existing/primitive data types), to
extend the flexibility of the programming language
● User-defined composite data types are constructed by programmer and reference at least one
other data type (which could be primitive/existing or user-defined) e.g. record, set, class
Non-composite data types:
● A non-composite data type can be defined without referencing another data type
● It can be a primitive type or a user-defined data type
● Non-composite user-defined data types are usually used for a special purpose
● E.g. integer, Boolean, real, string, char, pointer, enumerated
Enumerated data type:
● User-defined non-composite data type (defined without referencing another data type)
● Elements are ordered
● Elements are unique (no duplicate values)
● All the possible values that it can take are given in the list when defined
● All elements are the same data type
● Useful for variables that have a limited number of possible values e.g. compass directions (north,
south, east, and west), days of the week
● In pseudocode, the type definition for an enumerated data type has this structure:
TYPE <identifier> = (value1, value2, value3, …)
● E.g.TYPE Tmonth = (January, February, March, April, May, June, July,
August, September, October, November, December)
● The the variables thisMonth and nextMonth of type Tmonth could be defined as:
DECLARE thisMonth : Tmonth
DECLARE nextMonth : Tmonth
thisMonth ← January
nextMonth ← thisMonth + 1
Pointer data type:
● User-defined non-composite data type used to reference/store a memory location/address only
● Also indicates the type of data stored in the memory location
● In pseudocode the type definition has the following structure:
TYPE <pointer> = ^<datatype>
● E.g. a pointer for months of the year could be defined as follows:
TYPE Tmonthpointer = ^Tmonth
DECLARE monthPointer : TmonthPointer
● It could then be used as follows:
monthPointer ← ^thisMonth
● If the contents of the memory location are required rather than the address of the memory
location, then the pointer can be dereferenced e.g. myMonth can be set to the values stored at the
address monthPointer is pointing to:
DECLARE myMonth : Tmonth
myMonth ← monthPointer
1a. Floating-Point Numbers
Fixed-point binary numbers:
● Fixed point binary numbers have a predetermined number of bits before and after the point
● This makes fixed point numbers:
○ easier to process
○ but they cannot represent the range or accuracy of numbers that may be required
■ range is due to upper limit of places
■ accuracy/precision is due to lower limit of places
Floating-point numbers:
● When decimal numbers become very large, they are held in the format m x 10n (denary) or m x 2n
(binary) where m is known as the mantissa, and n is the exponent - this allows a much larger
range of numbers to be represented in the same number of bits (than fixed-point)
● E.g. decimal: the number 75,000 can be represented as 0.75 x 105
● A greater bit allocation for the mantissa enables numbers to be represented more precisely
● A greater bit allocation for the exponent enables a larger range of numbers to be represented
To convert from floating-point binary to decimal:
● Calculate exponent in denary (remembering that MSB will be negative)
● Apply a shift by that many positions to the mantissa
○ Negative exponent → decimal point shifts left
○ Positive exponent → decimal point shifts right
● Convert number into decimal (remembering MSB is negative, and new position of decimal point)
Normalisation:
● Process of moving the binary point of a floating-point number, so the first digit after the binary
point is a significant digit (0.1 for positive number, 1.0 for negative number)
● Purposes of normalisation:
○ Normalisation minimises the number of leading zeros/ones represented
○ Maximising the precision/accuracy of the number for the given number of bits
○ Maximising the range of numbers in the minimum number of bytes/bits
○ Avoids the possibility of many numbers having multiple representations
Rounding errors:
● In the binary system some numbers (fractions) cannot be represented exactly, Binary represents
numbers based on powers of 2, giving limited fractional representations
● It isn’t possible to represent all fractional numbers exactly, resulting in a loss of precision due to
the rounding error - the fractional part of the number is as close as possible within the constraints
● E.g. 0.110 in 8 bits can only be represented by 0.0001100(0.0625 + 0.0325)
bsolute error - bigger value minus the smaller value
A
Relative error - absolute error divided by the intended value
Overflow/underflow errors:
● Overflow - following an arithmetic/logical operation, the result is too large to be represented in
the number of bits allocated, resulting in truncation of the most significant bit
● Underflow - following an arithmetic/logical operation, the result is too small to be precisely
represented in number of bits allocated
, 2. User-Defined Data Types
User-defined data types:
● Programmer constructs a new (non-primitive) data type (from existing/primitive data types), to
extend the flexibility of the programming language
● User-defined composite data types are constructed by programmer and reference at least one
other data type (which could be primitive/existing or user-defined) e.g. record, set, class
Non-composite data types:
● A non-composite data type can be defined without referencing another data type
● It can be a primitive type or a user-defined data type
● Non-composite user-defined data types are usually used for a special purpose
● E.g. integer, Boolean, real, string, char, pointer, enumerated
Enumerated data type:
● User-defined non-composite data type (defined without referencing another data type)
● Elements are ordered
● Elements are unique (no duplicate values)
● All the possible values that it can take are given in the list when defined
● All elements are the same data type
● Useful for variables that have a limited number of possible values e.g. compass directions (north,
south, east, and west), days of the week
● In pseudocode, the type definition for an enumerated data type has this structure:
TYPE <identifier> = (value1, value2, value3, …)
● E.g.TYPE Tmonth = (January, February, March, April, May, June, July,
August, September, October, November, December)
● The the variables thisMonth and nextMonth of type Tmonth could be defined as:
DECLARE thisMonth : Tmonth
DECLARE nextMonth : Tmonth
thisMonth ← January
nextMonth ← thisMonth + 1
Pointer data type:
● User-defined non-composite data type used to reference/store a memory location/address only
● Also indicates the type of data stored in the memory location
● In pseudocode the type definition has the following structure:
TYPE <pointer> = ^<datatype>
● E.g. a pointer for months of the year could be defined as follows:
TYPE Tmonthpointer = ^Tmonth
DECLARE monthPointer : TmonthPointer
● It could then be used as follows:
monthPointer ← ^thisMonth
● If the contents of the memory location are required rather than the address of the memory
location, then the pointer can be dereferenced e.g. myMonth can be set to the values stored at the
address monthPointer is pointing to:
DECLARE myMonth : Tmonth
myMonth ← monthPointer