1.87k likes | 2.41k Vues
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…. Programming Fundamentals 3 Feliks Kluzniak. Executive summary : We discuss: arithmetic with a limited number of digits
E N D
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 ...
Strange arithmetic… Programming Fundamentals 3 Feliks Kluzniak Strange arithmetic ...
Executive summary: • We discuss: • arithmetic with a limited number of digits • hexadecimal and binary arithmetic Strange arithmetic ...
Imagine a strip of plastic with 10 digits: 0 0 1 2 3 4 5 6 7 8 9 Strange arithmetic ...
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 ...
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 ...
… 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 ...
… 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 ...
What if I then turn it another 4 times? 3 Strange arithmetic ...
Wow, I have an adding machine! 7 Strange arithmetic ...
Wow, I have an adding machine! What if I turn it another 4 times? 7 Strange arithmetic ...
1 Strange arithmetic ...
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 ...
Let us now buy another strip, and connect it in the same way to the first wheel: 1 Strange arithmetic ...
One complete turn of the right wheel will make the left wheel turn by 1/10 th. 0 0 Strange arithmetic ...
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 ...
Nice! What will happen if I turn it 90 more times? (It’s good exercise.) 1 5 Strange arithmetic ...
After 90 more turns: So now we have arithmetic modulo 100. 0 5 Strange arithmetic ...
There is room in the box for two more wheels, all connected as before. 0] 0 0 0 0 Strange arithmetic ...
This machine will allow us to perform addition modulo what? 0] 0 0 0 0 Strange arithmetic ...
This machine will allow us to perform addition modulo 10 000. 0] 0 0 0 0 Strange arithmetic ...
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 ...
What range of positive integers can we represent with: 4 digits ? Strange arithmetic ...
What range of positive integers can we represent with: 4 digits : 0 .. 9999 Strange arithmetic ...
What range of positive integers can we represent with: 4 digits : 0 .. 9999 2 digits: 0 .. 99 1 digit : 0 .. 9 Strange arithmetic ...
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 ...
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 ...
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 ...
An important lesson: If the number of digits is limited, then the inequality a + 1 > a does not always hold! Strange arithmetic ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...