 Download Download Presentation 32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font

# 32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font

Télécharger la présentation ## 32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. 32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font 12 point font 11 point font 10 point font Strange arithmetic ...

2. Strange arithmetic… Programming Fundamentals 3 Feliks Kluzniak Strange arithmetic ...

3. Executive summary: • We discuss: • arithmetic with a limited number of digits • hexadecimal and binary arithmetic Strange arithmetic ...

4. Imagine a strip of plastic with 10 digits: 0 0 1 2 3 4 5 6 7 8 9 Strange arithmetic ...

5. Imagine a strip of plastic with 10 digits: 0 0 1 2 3 4 5 6 7 8 9 We glue the ends together, getting a ring, with the digits facing outwards. Strange arithmetic ...

6. Imagine a strip of plastic with 10 digits: 0 0 1 2 3 4 5 6 7 8 9 We glue the ends together, getting a ring, with the digits facing outwards. We fit a wheel into the ring, tightly, and … Strange arithmetic ...

7. … connect it to a crank, so that one turn of the crank will advance the wheel by 1/10 th. We then put it in a box with a little window: 0 Strange arithmetic ...

8. … connect it to a crank, so that one turn of the crank will advance the wheel by 1/10 th. We then put it in a box with a little window: What will happen if I turn the crank 3 times? 0 Strange arithmetic ...

9. What if I then turn it another 4 times? 3 Strange arithmetic ...

10. Wow, I have an adding machine! 7 Strange arithmetic ...

11. Wow, I have an adding machine! What if I turn it another 4 times? 7 Strange arithmetic ...

12. 1 Strange arithmetic ...

13. What we have here is arithmetic modulo 10. The result of adding a to b is (a + b) mod 10. (The remainder of dividing a + b by 10.) 1 Strange arithmetic ...

14. Let us now buy another strip, and connect it in the same way to the first wheel: 1 Strange arithmetic ...

15. One complete turn of the right wheel will make the left wheel turn by 1/10 th. 0 0 Strange arithmetic ...

16. One complete turn of the right wheel will make the left wheel turn by 1/10 th. What will happen when I turn the crank 15 times? 0 0 Strange arithmetic ...

17. Nice! What will happen if I turn it 90 more times? (It’s good exercise.) 1 5 Strange arithmetic ...

18. After 90 more turns: So now we have arithmetic modulo 100. 0 5 Strange arithmetic ...

19. There is room in the box for two more wheels, all connected as before. 0] 0 0 0 0 Strange arithmetic ...

20. This machine will allow us to perform addition modulo what? 0] 0 0 0 0 Strange arithmetic ...

21. This machine will allow us to perform addition modulo 10 000. 0] 0 0 0 0 Strange arithmetic ...

22. This machine will allow us to perform addition modulo 10 000. 10000 = 10**4 (i.e., 10 to the power of 4) 100 = 10**2 10 = 10**1 Do you see a pattern here?  0] 0 0 0 0 Strange arithmetic ...

23. What range of positive integers can we represent with: 4 digits ? Strange arithmetic ...

24. What range of positive integers can we represent with: 4 digits : 0 .. 9999 Strange arithmetic ...

25. What range of positive integers can we represent with: 4 digits : 0 .. 9999 2 digits: 0 .. 99 1 digit : 0 .. 9 Strange arithmetic ...

26. What range of positive integers can we represent with: 4 digits : 0 .. 9999 = 0 .. 10**4 – 1 2 digits: 0 .. 99 = 0 .. 10**2 – 1 1 digit : 0 .. 9 = 0 .. 10**1 – 1 NOTE: ** denotes exponentiation, i.e., “raising to the power” (a convention established by Fortran). So, e.g., 10**3 = 10 * 10 * 10 = 1000 . Notice that ** binds more tightly than other operators, so 10**4 – 1 = (10**4) – 1 Strange arithmetic ...

27. What range of positive integers can we represent with: 4 digits : 0 .. 9999 = 0 .. 10**4 – 1 2 digits: 0 .. 99 = 0 .. 10**2 – 1 1 digit : 0 .. 9 = 0 .. 10**1 – 1 What about 0 digits? Strange arithmetic ...

28. What range of positive integers can we represent with: 4 digits : 0 .. 9999 = 0 .. 10**4 – 1 2 digits: 0 .. 99 = 0 .. 10**2 – 1 1 digit : 0 .. 9 = 0 .. 10**1 – 1 What about 0 digits? 0 .. 2**0 – 1 By universal convention, raising an integer to the power of 0 yields 1 . Strange arithmetic ...

29. An important lesson: If the number of digits is limited, then the inequality a + 1 > a does not always hold! Strange arithmetic ...

30. An important lesson: If the number of digits is limited, then the inequality a + 1 > a does not always hold! This is, of course, what we deal with in programming! Strange arithmetic ...

31. An important lesson: If the number of digits is limited, then the inequality a + 1 > a does not always hold! This is, of course, what we deal with in programming! In practice we treat arithmetic as normal, and worry about staying “in range” separately. Strange arithmetic ...

32. An important lesson: If the number of digits is limited, then the inequality a + 1 > a does not always hold! This is, of course, what we deal with in programming! In practice we treat arithmetic as normal, and worry about staying “in range” separately. This is known as separation of concerns. Strange arithmetic ...

33. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? Strange arithmetic ...

34. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . Strange arithmetic ...

35. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Strange arithmetic ...

36. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Thirty, i.e., 3 * (10**1) . Strange arithmetic ...

37. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Thirty, i.e., 3 * (10**1) . 0 2 0 0 ? Strange arithmetic ...

38. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Thirty, i.e., 3 * (10**1) . 0 2 0 0 ? Two hundred, i.e., 2 * (10**2) . Strange arithmetic ...

39. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Thirty, i.e., 3 * (10**1) . 0 2 0 0 ? Two hundred, i.e., 2 * (10**2) . So, how many times would I have to turn the crank to obtain 0 2 3 1 ? Strange arithmetic ...

40. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Thirty, i.e., 3 * (10**1) . 0 2 0 0 ? Two hundred, i.e., 2 * (10**2) . So, how many times would I have to turn the crank to obtain 0 2 3 1 ? Two hundred thirty one, i.e., 2 * (10**2) + 3 * (10**1) + 1 * (10**0) . Strange arithmetic ...

41. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (10**0) . 0 0 3 0 ? Thirty, i.e., 3 * (10**1) . 0 2 0 0 ? Two hundred, i.e., 2 * (10**2) . So, how many times would I have to turn the crank to obtain 0 2 3 1 ? Two hundred thirty one, i.e., 2 * (10**2) + 3 * (10**1) + 1 * (10**0) . Do you see the general pattern ? Strange arithmetic ...

42. I can’t afford another box, but I can afford four longer strips: 0 0 1 2 3 4 5 6 7 8 9 A B C D E F Strange arithmetic ...

43. At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly): 0] 0 0 0 0 Strange arithmetic ...

44. At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly): What range of integers can I represent? 0] 0 0 0 0 Strange arithmetic ...

45. At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly): What range of integers can I represent? 0 .. 16**4 – 1 0] 0 0 0 0 Strange arithmetic ...

46. At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly): What range of integers can I represent? 0 .. 16**4 – 1 0 .. 65535 0] 0 0 0 0 Strange arithmetic ...

47. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (16**0) . Strange arithmetic ...

48. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (16**0) . 0 0 3 0 ? Strange arithmetic ...

49. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (16**0) . 0 0 3 0 ? Forty eight, i.e., 3 * (16**1) . Strange arithmetic ...

50. Let the initial state of the machine be 0 0 0 0. How many times would I have to turn the crank to obtain: 0 0 0 1 ? One, i.e., 1 * (16**0) . 0 0 3 0 ? Forty eight, i.e., 3 * (16**1) . 0 2 0 0 ? Strange arithmetic ...