Feedback: Journal 1 1 Exercise 1: Quiz You were not expected to - - PowerPoint PPT Presentation

feedback journal 1
SMART_READER_LITE
LIVE PREVIEW

Feedback: Journal 1 1 Exercise 1: Quiz You were not expected to - - PowerPoint PPT Presentation

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


slide-1
SLIDE 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!

Feedback: Journal 1

ICC 1

slide-2
SLIDE 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

Next week

ICC 2

slide-3
SLIDE 3

Representation

  • f Information

(Part 2)

Barbara Jobstmann

ICC

01010101010100 10101010101010 111111010100101

slide-4
SLIDE 4

§ Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples):

  • Fixed-point representation
  • Floating-point representation

§ Representing alphabets, images, …

Agenda

ICC 4

slide-5
SLIDE 5

§ Given n bits, we represent a negative integer k using the binary

representation for 2n - |k|

  • Desired property: k + (-k) = 0
  • Idea: k + (-k) = 2n = 0 (in a system with n bits)

§ 2n - |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 -2n-1 until 2n-1 – 1

Recall: 2’s Complement Representation

ICC 5

slide-6
SLIDE 6

§ What is the 2’s complement representation of -42 with 7 bits?

Decimal to Binary

ICC 6

slide-7
SLIDE 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:

  • 27 – 42 = 128 – 42 = 86
  • 86 = 64 + 16 + 4 + 2 and corresponds to 1010110

Decimal to Binary (Solution)

ICC 7

slide-8
SLIDE 8

§ Assume a 7-bit 2’s complement representation for integers. Which

decimal number corresponds to the following binary pattern 1010110?

Binary to Decimal

ICC 8

slide-9
SLIDE 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
  • 27 – 86 = 128 – 86 = 42

Binary to Decimal (Solution)

ICC 9

slide-10
SLIDE 10

§ Given 3 bit, we can represent the number from -22 = -4 until 22-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.

Covered Domain of 2’s Complement

ICC 10

Negative integers (in two’s complement repr.)

Positive integers

sign

slide-11
SLIDE 11

§ In C++ by default integers are represented with 32 bits § What does the following program print?

Should I care about the covered domain?

ICC 11

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; // 231 - 1 = 2'147'483’647 // 232 - 1 = 4’294’967’295

slide-12
SLIDE 12

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

Quiz

ICC 12

http://go.epfl.ch/iccsv-quiz1

slide-13
SLIDE 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 -24 to 24-1, so from
  • 16 to 15.

a) 3 – 5 = -2 (in [-16,15]) b) 15 + 3 = 18 (not in [-16,15]) – mapped to -14 c)

  • 3 + 5 = 2 (in [-16,15])

d) -15 – 3 = -18 (not in [-16,15]) – mapped to 14

Quiz (Solutions)

ICC 13

  • 14
  • 15 -16 15

14 13

slide-14
SLIDE 14

§ Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples):

  • Fixed-point representation
  • Floating-point representation

§ Representing alphabets, images, …

Agenda

ICC 14

slide-15
SLIDE 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 § 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

Example: Fixed-Point Representation

ICC 15

.

2! + 2" + 2# + 2$" + 2$! + 2$% = 4 + 2 + 1 + 0.5 + 0.25 + 0.125 = 7.875

slide-16
SLIDE 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?

Example: Fixed-Point Representation (cnt.)

ICC 16

.

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. In this system 0.1875 is represented by 2-3=0.125 (rounding error)

slide-17
SLIDE 17

§ Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples):

  • Fixed-point representation
  • Floating-point representation

§ Representing alphabets, images, …

Agenda

ICC 17

slide-18
SLIDE 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

Example: Floating Point Representation

ICC 18

00 20=1 01 21=2 11 2-1=0.5 10 2-2=0.25 1.0 1.0 2.0 0.5 0.25 1.1 20+2-1=1.5 1.5 3.0 0.75 0.325

0.25 0.5 0.75 1 1.5 2 3 4

slide-19
SLIDE 19

§ 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

slide-20
SLIDE 20

§ 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.

The rounding error is the rule: Remarks

slide-21
SLIDE 21

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

A problematic example

slide-22
SLIDE 22

§ 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

Consequence of rounding errors

e e

slide-23
SLIDE 23

§ 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.

Consequence of rounding errors

slide-24
SLIDE 24

§ 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

Controlling the accuracy is possible

slide-25
SLIDE 25

What is the binary representation of 1.171875 using a fixed-point number with 1 bit before and 6 bits after the comma? Assume a simplified floating point representation of a positive number using 3 bits as following: 1 bit for the exponent in base 2, 2 bits for the

  • mantissa. Select all correct statements
  • 1. The decimal number 3.75 can be represented exactly (without

rounding errors).

  • 2. In this representation, only 8 different values in total can be

represented exactly (without rounding errors).

  • 3. The maximum relative error is 2−3 = 0.125.
  • 4. The decimal number 2.6 can be represented exactly (without rounding

errors).

  • 5. The distance between two numbers that can be represented exactly is

always 0.25.

Quiz

ICC 25

https://go.epfl.ch/iccsv-quiz2

slide-26
SLIDE 26

§ 1.171875 with 7 bits

1.171875 0.171875・2 = 0.343750 0.343750・2 = 0.687500 0.687500・2 = 1.375000 0.375000・2 = 0.750000 0.750000・2 = 1.500000 0.500000・2 = 1.000000

Quiz Solutions

ICC 26

1 . 1 1 1 2-3 2-5 2-6

1 + 0.125 + 0.03125 + 0.015625 = 1.171875

slide-27
SLIDE 27

1. # 2

Quiz Solutions (cnt.)

ICC 27

20=1 1 21=2 1.00 1.0 2.0 1.01 20+2-2=1.25 1.25 2.5 1.10 20+2-1=1.5 1.5 3 1.11 20+2-1+2-2=1.75 1.75 3.5 1. The decimal number 3.75 can be represented exactly (without rounding errors). 2. In this representation, only 8 different values in total can be represented exactly (without rounding errors). 3. The maximum relative error is 2−3 = 0.125. 4. The decimal number 2.6 can be represented exactly (without rounding errors). 5. The distance between two numbers that can be represented exactly is always 0.25.

X X X X

slide-28
SLIDE 28

§ Negative Integers: 2s Complement representation (Examples) § Decimal numbers (Examples):

  • Fixed-point representation
  • Floating-point representation

§ Representing alphabets, images, …

Agenda

ICC 28

slide-29
SLIDE 29

§ American Standard Code for Information Interchange. § ASCII codes represent letters with 7 bits, value 0-127

Code ASCII

Letter Dec

A 65 B 66 .. .. a 97 b 98 .. ..

slide-30
SLIDE 30

Can I see this encoding?

ICC 30

Letter Dec

A 65 B 66 .. .. a 97 b 98 .. ..

slide-31
SLIDE 31

§ ASCII extended on 8 bits: Codes 0x80 to 0xFF § The extended code ISO 8859 Latin1 offers the accented characters

lower and upper case of western languages: é è ê à ä ö ü …

Beyond the basic ASCII code

slide-32
SLIDE 32

§ UNICODE standard is maintained by the Unicode Consortium, and as

  • f March 2020, there is a repertoire of 143,859 characters covering 154

modern and historic scripts, as well as multiple symbol sets and emoji.

§ UTF-8 (Unicode Transformation Format) is an encoding of UNICODE

characters including 1 to 4 bytes. It is recommended. Nearly all webpage use UTF-8.

§ The 256 codes of ASCII are included in the UNICODE

UNICODE –Universal Coded Character Set

slide-33
SLIDE 33

From the multi-byte code (ideogram) to the image

60x60

山 - shān = mountain The symbol can be encoded in UTF-8 However, the representation of the symbol = its image asks for more information. Several approaches are possible, from the higher to the lowest level :

1) Specify the font= «classical style». 2) Characterize the outline of the shape using a set of

mathematical parametric curves. This is how fonts are constructed.

3) Decompose the image plane into a set of cells which

indicate the amount of ink (pixel).

slide-34
SLIDE 34

From the multi-byte code (ideogram) to the image

山 - shān = mountain The symbol can be encoded in UTF-8 However, the representation of the symbol = its image asks for more information. Several approaches are possible, from the higher to the lowest level :

1) Specify the font= «classical style». 2) Characterize the outline of the shape using a set of

mathematical parametric curves. This is how fonts are constructed.

3) Decompose the image plane into a set of cells which

indicate the amount of ink (pixel).

slide-35
SLIDE 35

ICC Module 1 Leçon 4 – Représenta7on de l'informa7on

51 /52

Image in layers of gray :

every pixel memorizes an intensity between 0 (black) and the maximum defined by the number of the bits retained (white)

bitmap

slide-36
SLIDE 36

§ Each pixel memorizes the intensity of three primary components whose

combination permits to return a space of colors.

§ RGB encoding (Red, Green, Blue)

Additive synthesis of the colors:

§ Black=(0,0,0) § Red=(255,0,0) § Green=(0,255,0) § Blue=(0,0,255) § White=(255,255,255) § Gray levels when three components are equal § Sometimes completed by a 4th component called alpha (transparency)

for graphical applications.

Image in color

slide-37
SLIDE 37

Example

6b3c87? (see Videos)

[0,255]

slide-38
SLIDE 38

§ We have seen representation of natural numbers (unsigned int),

integers (int), fractional numbers (float or double), characters (char), and images (using discrete sampling).

§ A representation is a human convention of interpretation of a set of

  • signs. Its power is directly connected to the number of people who

share it, hence the importance of standards (e.g., code ASCII, UTF).

§ Is it possible to build an accurate representation of the real world?

  • Calculations with the full positional representation give accurate results as

long as they remain in the covered domain.

  • For floating point numbers, we have to determine the required precision and

adapted the presentation.

  • In addition it is necessary to distinguish the mathematical problem of its

implementation by the numerical calculation resolution.

Summary