COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1
Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you cannot attend your tutorial Let me know and we ll re-schedule Into another tutorial next week Lecture 3 - Data Representation 3 2
Overview Last Lecture Data representation (integers) This Lecture Data representation (real numbers) Source: lecture & lecture notes Source: chapter 10 (10 th edition) Next Lecture Digital logic Gates Boolean algebra Lecture 3 - Data Representation 3 3
What Time Is It? 11 12 10 9 8 1 2 4 3 7 6 5 Lecture 3 - Data Representation 3 4
What Time Is It? What time will it be two hours from now? 11 12 10 1 2 9 3 8 4 7 6 5 Lecture 3 - Data Representation 3 5
What Time Is It? 11 12 10 1 2 What time was it one hour ago? 9 3 8 4 7 6 5 Lecture 3 - Data Representation 3 6
Integers (Two s Complement) Recall: Integers are typically stored in two s complement format 100 101 110 111 000 001 010 011-4 -3-2 -1 0 1 2 3 In this 3-bit example: 0 is stored as 000 Integer addition is binary addition Integer subtraction is binary addition of the negative of the right operand A-B = A + -B Conversion into two s complement +ve: use the binary representation -ve: complement and add 1 Lecture 3 - Data Representation 3 7
Integers (Two s Complement) Two s complement works exactly like clock arithmetic (modulo arithmetic) If we re at 2, what do you get if you subtract 4? 111-1 000 0 1 001 If we re at 2, what do you get if you add 4? 110-2 2 010-3 3 101-4 011 100 Lecture 3 - Data Representation 3 8
Real Numbers Integers are discrete. Real numbers are continuous. In Computer Science real numbers are often called floating point numbers For reasons that will become obvious Floats can be very large or very small 4,386,593,021,854.341 0.000,000,000,008 How do we represent real numbers in the computer? Lecture 3 - Data Representation 3 9
Scientific Notation (decimal) If you move the decimal point to the left: You increase the exponent. If you move the decimal point to the right: You decrease the exponent. p 1 Remember: 10 0 = 1 10 = 10 12,345.6 = 0.012345 = 12,345.6 x 10 0 0.012345 x 10 0 1,234.56 x 10 1 0.12345 x 10-1 123.456 x 10 2 1.2345 x 10-2 12.3456 x 10 3 12.345 x 10-3 1.23456 x 10 4 123.45 x 10-4 p Lecture 3 - Data Representation 3 10
Scientific Notation (binary) 11000.1= 11000.1 x 2 0 1100.01 x 2 1 110.001 x 2 2 11.0001 x 2 3 1.10001 x 2 4 If you move the decimal point to the left: You increase the exponent. If you move the decimal point to the right: You decrease the exponent. Remember: p 2 0 = 1 2 = 1 2 p Lecture 3 - Data Representation 3 11
Floating Point In scientific notation the number is broken into 3 parts The significant digits The power The base The base is usually implicit (we know it) If you store the other two separately then the power determines the location of the decimal point That is, the decimal point floats relative to the significant digits We call these numbers floating point numbers There s a number of formats including the IEEE 754 standard Lecture 3 - Data Representation 3 12
IEEE Floating Point Format Stored as a binary32 (single) 4-byte number (float) Where 1-bit 8-bits 23-bits s e f s = sign (0 for positive, 1 for negative) of the number e = power of two, excess 127 notation (exponent) Allowed values for e are 0-255 f = binary fraction (mantissa, significand) Without the leading 1 before the binary point Why? s (-1) x 1.f x 2 e-127 Lecture 3 - Data Representation 3 13
Decimal to IEEE Conversion 1. Convert the decimal number to binary 2. Write the binary number in scientific notation base 2 3. Write f by taking the fractional part of the normalised number and adding trailing zeroes to get 23 bits 4. Determine sign bit, s 5. Add 127 to the exponent (from step 2) to get e 6. Convert e to an 8 bit binary number (add leading zeroes if needed) 7. Write in IEEE format by concatenating s, e, and f Lecture 3 - Data Representation 3 14
Decimal to IEEE Conversion Example Convert 24.5 10 to IEEE floating point representation: 1. Convert the decimal number to binary 24.5 10 = 16 + 8 + 0 + 0 + 0 + 0.5 = 11000.1 2. Write the binary number in scientific notation using base 2 = 1.10001 * 2 4 Lecture 3 - Data Representation 3 15
Decimal to IEEE Conversion Example (cont.) 3. Write f by taking the fractional part of the normalised number and adding trailing zeroes to get 23 bits 24.5 10 = 1.10001 * 2 4 f = 10001 100 0100 0000 0000 0000 0000 4. Determine sign bit, s Positive number so s = 0 Lecture 3 - Data Representation 3 16
Decimal to IEEE Conversion Example (cont.) 5. Add 127 to the exponent (from step 2) to get e e = 4 + 127 = 131 6. Convert e to an 8 bit binary number (add leading zeroes if needed) 131= 128 + 0 + 0 + 0 + 0 + 0 + 2 + 1 = 1000 0011 7. Write in IEEE format by concatenating s, e, and f 0100 0001 1100 0100 0000 0000 0000 0000 = 41C40000 16 s e f Lecture 3 - Data Representation 3 17
IEEE to Decimal Conversion 1. Group the binary digits into 1, 8, and 23 digits (s,e,f) 2. Convert e to decimal Subtract 127 to get exp 3. Delete the trailing zeroes from f and write: 1.f x 2 exp where the exp is the value from step 2 and f is the original f with the trailing zeroes removed 4. Un-normalise the number by moving the binary point until the exp = 0 5. Convert the binary number to decimal 6. If s is 1, negate the number Lecture 3 - Data Representation 3 18
IEEE to Decimal Conversion Example Convert C1C40000 16 to decimal form C1C40000 16 = 1100 0001 1100 0100 0000 0000 0000 0000 1. Group the binary digits into 1, 8, and 23 digits (s,e,f) 1100 0001 1100 0100 0000 0000 0000 0000 s e f Lecture 3 - Data Representation 3 19
IEEE to Decimal Conversion Example (cont.) 2. Convert e to a decimal number. 1000 0011 = 128 + 0 + 0 + 0 + 0 + 0 + 2 + 1 = 131 Subtract 127 to get exp 131 127 = 4 (exponent) Lecture 3 - Data Representation 3 20
IEEE to Decimal Conversion Example (cont.) 3. Delete the trailing zeroes from f and write 1.f x 2 exp where the exp is the value from step 2 and f is the original f with the trailing zeroes removed 100 0100 0000 0000 0000 0000 10001 = 1.10001 x 2 4 4. Un-normalise the number by moving the binary point until the exp = 0 1.10001 x 2 4 = 11000.1 Lecture 3 - Data Representation 3 21
IEEE to Decimal Conversion Example (cont.) 5. Convert the binary number to decimal 11000.1 = 16 + 8 + 0 + 0 + 0 + 0.5 = 24.5 6. If s is 1, negate the number s = 1, so the number is negative The answer is -24.5 Lecture 3 - Data Representation 3 22
IEEE Special Cases The IEEE floating point has some special cases: e = 0 f = 0 Number is 0.0 e = 255 f = 0 or - e = 255 f 0 Not a number (NaN) e = 0 f 0 (-1) s x 0.f x 2-126 Lecture 3 - Data Representation 3 23
Other IEEE Representations Name Common name Base Digits E min E max Decimal Decimal digits E max binary16 Half precision 2 10+1-14 +15 3.31 4.51 binary32 Single precision 2 23+1-126 +127 7.22 38.23 binary64 Double precision 2 52+1-1022 +1023 15.95 307.95 binary128 Quad precision 2 112+1-16382 +16383 34.02 4931.77 decimal32 10 7-95 +96 7 96 decimal64 10 16-383 +384 16 384 decimal128 10 34-6143 +6144 34 6144 Lecture 3 - Data Representation 3 24
Floating Point Arithmetic (+/-) Addition (subtraction) Align the significands Convert to the same 2 e (align) Add (or subtract) Round and normalize Convert back into scientific notation Lecture 3 - Data Representation 3 25
Floating Point Arithmetic (+/-) 48 + 2.5 48 = 1.100*2 5 2.5 = 1.01 * 2 1 Convert to the same 2 e then add 48 = 2.5 = 1. 1 0 0 0 0 0 *2 5 0. 0 0 0 1 0 1 *2 5 48 + 2.5 = 1. 1 0 0 1 0 1 *2 5 = 50.5 Lecture 3 - Data Representation 3 26
Floating Point Arithmetic (*) Multiplication The significands are multiplied The exponents are added Round and normalize Lecture 3 - Data Representation 3 27
Floating Point Arithmetic (/) Division The significands are divided The exponents are subtracted Round and normalize Lecture 3 - Data Representation 3 28
Consequences What is the output of this program: #include <stdio.h> int main(void) { float x; for (x = 0; x!= 1.0; x += 0.1) printf("%f\n", x); return 0; } Java Equivalent: public static void main(string [] args) { float x; for (x = 0; x!= 1.0; x+= 0.1) System.out.println(x); } Lecture 3 - Data Representation 3 29
Consequences What is the output of this program: #include <stdio.h> int main(void) { float x; x = 123456789.0; printf("%f + 1 = %f\n", x, x + 1); return 0; } Java equivalent public static void main(string [] args) { float x = 123456789.0f; System.out.printf("%f + 1 = %f\n", x, x + 1); } Lecture 3 - Data Representation 3 30
Homework Download the two programs (previous two slides) from the COSC243 website and change them into programs that produce the correct output Lecture 3 - Data Representation 3 31