Feedback: Journal 1 1 § Exercise 1: Quiz • You were not expected to know the answers. It was part of the introduction to give you an idea about what information you will learn during this semester. § Exercise 2: binary subtraction and multiplication • Please look at the explanations in Piazza. If you need more help, asks me after the lecture and I will explain with an example. § Language: • There is a lot of help in French: videos, book, assistants (make use of them) § Zoom connection is sometimes cut • Recordings are available afterwards § Progress: • Be careful to not fall behind. We are changing topics quickly! ICC
Next week 2 § Start a new topic: algorithm and complexity § Required: watch videos (link on Moodle) § Optional: read note about pseudo-code and complexity (links on Moodle) or check out Chapter 1 in the reference book "Découvrir le numérique" § Heads-up: difficult topic, completely new notions ICC
Representation of Information 01010101010100 (Part 2) 10101010101010 111111010100101 Barbara Jobstmann ICC
Agenda 4 § Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples): • Fixed-point representation • Floating-point representation § Representing alphabets, images, … ICC
Recall: 2’s Complement Representation 5 § Given n bits, we represent a negative integer k using the binary representation for 2 n - |k| • Desired property: k + (-k) = 0 • Idea : k + (-k) = 2 n = 0 (in a system with n bits) § 2 n - |k| can be computed by adding 1 to the 1’s complement of |k| § The 1’s complement can be computed by flipping all bits § Given n bits to represent positive and negative integers, we can represent integers from -2 n-1 until 2 n-1 – 1 ICC
Decimal to Binary 6 § What is the 2’s complement representation of -42 with 7 bits? ICC
Decimal to Binary (Solution) 7 § What is the 2’s complement representation of -42 with 7 bits? • 42 in binary is 0101010 • 1’s complement of 0101010 is 1010101 • 1010101 + 0000001 = 1010110 § Alternative way: • 2 7 – 42 = 128 – 42 = 86 • 86 = 64 + 16 + 4 + 2 and corresponds to 1010110 ICC
Binary to Decimal 8 § Assume a 7-bit 2’s complement representation for integers. Which decimal number corresponds to the following binary pattern 1010110? ICC
Binary to Decimal (Solution) 9 § Assume a 7-bit 2’s complement representation for integers. Which decimal number corresponds to the following binary pattern 1010110? • 1’s complement of 1010110 = 0101001 • 0101001 + 0000001 = 0101010 • 0101010 is decimal is 2 + 8 + 32 = 42 • 1010110 corresponds to -42 § Alternative way: • 1010110 corresponds to 2 + 4 + 16 + 64 = 86 • 2 7 – 86 = 128 – 86 = 42 ICC
Covered Domain of 2’s Complement 10 § Given 3 bit, we can represent the number from -2 2 = -4 until 2 2 -1 = 3. § What happens with numbers smaller than -4 and larger than 3? • They are mapped to the interal [-4, 3], e.g., -4 – 1 = 3. Positive integers sign Negative integers (in two’s complement repr.) ICC
Should I care about the covered domain? 11 § In C++ by default integers are represented with 32 bits § What does the following program print? unsigned int i(0); i = i - 1; cout << "0 - 1 gives " << i << endl; int j(2147483647); j = j + 1; cout << "2'147'483'647 + 1 gives " << j << endl; // 2 31 - 1 = 2'147'483’647 // 2 32 - 1 = 4’294’967’295 ICC
Quiz 12 http://go.epfl.ch/iccsv-quiz1 Assume we are using an 5-bit two’s complement representation of numbers. 1. Which of the following binary patterns represent a negative number? a) 10101 b) 11101 c) 00101 2. What (decimal) value does 11101 represents? 3. What are the results of the following operations (written in decimal)? a) 3 - 5 b) 15 + 3 c) -3 + 5 d) -15 - 3 ICC
Quiz (Solutions) 13 1. a) and b) are negative numbers because the first digit (the MSB), which indicates the sign is 1. 2. 11101 is a negative number because MSB is 1. So we compute the 2s complement to get the absolute value: • 1s complement of 11101 is 00010 • 10 + 1 = 11 • 11 in decimal is 3 • 11101 is the representation of -3 (in a system with 5 bits) 3. Results: 5 bits allow to represent the number from -2 4 to 2 4 -1, so from -16 to 15. a) 3 – 5 = -2 (in [-16,15]) b) 15 + 3 = 18 (not in [-16,15]) – mapped to -14 13 c) -3 + 5 = 2 (in [-16,15]) 14 -14 d) -15 – 3 = -18 (not in [-16,15]) – mapped to 14 -15 -16 15 ICC
Agenda 14 § Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples): • Fixed-point representation • Floating-point representation § Representing alphabets, images, … ICC
Example: Fixed-Point Representation 15 § Assume we have 6 bits and use 3 digits before and 3 digits after the comma to represent non-negative numbers . § The smallest representable number different from 0 is 2 !" = 0.125 § The largest representable number is 2 ! + 2 " + 2 # + 2 $" + 2 $! + 2 $% = 4 + 2 + 1 + 0.5 + 0.25 + 0.125 = 7.875 § In each integer intervals, we can represent 8 numbers precisely, e.g., between 0 and 1, these numbers (in decimal) are 0, 0.125, 0.25, 0.375, 0.5, 0.615,0.75, 0.875 § The largest absolut error is 2 !" = 0.125 ICC
Example: Fixed-Point Representation (cnt.) 16 § Assume we have 6 bits and use 3 digits before and 3 digits after the comma to represent non-negative numbers . § What would be the representation of 0.1875 in this system? 0.1875 + 2 = 0.375 0.3750 + 2 = 0.750 0.7500 + 2 = 1.500 0.5000 + 2 = 1.000 The decimal number 0.1875 is represented by 0.0011 in binary. In order to fit into the system above we would use 0.001, since we store only 3 digits after the comma. ICC In this system 0.1875 is represented by 2 -3 =0.125 (rounding error)
Agenda 17 § Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples): • Fixed-point representation • Floating-point representation § Representing alphabets, images, … ICC
Example: Floating Point Representation 18 § Assume a simplified floating point representation of a positive number using 3 bits as following: 2 bits for the exponent using 2’s complement, 1 bits for the mantissa . 1. # 2 00 01 11 10 2 0 =1 2 1 =2 2 -1 =0.5 2 -2 =0.25 1.0 1.0 2.0 0.5 0.25 1.1 1.5 3.0 0.75 0.325 2 0 +2 -1 =1.5 0.25 0.5 0.75 1 1.5 2 3 4 ICC
The rounding error is the rule § We call rounding error the difference between a number x and its approximate representation with a floating-point number. What is the binary pattern of “one tenth” (0.1)? 0.000110011001100110011001100110011001100... “one tenth” cannot be represented exactly with a finite number of bits.
The rounding error is the rule: Remarks § This problem exists in all bases, e.g., 1/3 in base 10 § Even if the rounding error is inevitable for the majority of numbers by choosing the appropriate representation we can guarantee that the rounding error is below a threshold as defined by the requirements of the problem.
A problematic example Example: What does the following program print? § 4 and 0.25 are exactly represented, their product is 1 § On the contrary, decimals 0.1 and 0.01 are approximated § Consequence of the binary computations: 0.1 * 0.1 ≠ 0.01
Consequence of rounding errors § Testing equality of a floating point result vis-à-vis ONLY ONE theoretical value does not make sense. § The result is, in general, different from the theoretical value. § Therefore, equality of floating point numbers is tested with an interval. There is equality if the result belongs to an interval. | result – theoretical_value | < 𝜁 § 𝜁 depends on: • the theoretical value • the way to obtain the result e e
Consequence of rounding errors § The result can be different according to the order of the operations . The addition is no longer associative, i.e., there are values a, b, c such that (a + b) + c ≠ a + (b +c) § Example with a standard on 64 bits having 53 bits of mantissa: Is (1 + 2 -53 ) + 2 -53 equal to 1 + (2 -53 + 2 -53 )? What does this program print? § Good practice : first add the small numbers between them before adding them to large numbers.
Controlling the accuracy is possible § For a given problem and its algorithmic solution, it is important to ask the following questions: • What precision do I need for my results? • What is the effect of the algorithm on the precision of the results? • What is the maximum available precision on the target machine? § In the case of insufficient precision, one has to reconsider the algorithmic solution, or adjust the representation in order to obtain the desired precision. § Compromise: precision versus computation and memory costs
Recommend
More recommend