Clock Arithmetic 7 January 2019 OSU CSE 1 Mathematical Modulo - - PowerPoint PPT Presentation

clock arithmetic
SMART_READER_LITE
LIVE PREVIEW

Clock Arithmetic 7 January 2019 OSU CSE 1 Mathematical Modulo - - PowerPoint PPT Presentation

Clock Arithmetic 7 January 2019 OSU CSE 1 Mathematical Modulo (mod) The value of a mod b , or a modulo b , where a and b are mathematical integer s and b > 0 , is computed by doing clock arithmetic on a clock face with b positions


slide-1
SLIDE 1

Clock Arithmetic

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Mathematical Modulo (“mod”)

  • The value of a mod b, or a modulo b,

where a and b are mathematical integers and b > 0, is computed by doing clock arithmetic on a clock face with b positions

– If a > 0, the “hand” on the clock starts at 0 and moves |a| positions clockwise – If a < 0, it moves |a| counter-clockwise – Where it ends up is the value of a mod b

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Example: 24-hr Clock

7 January 2019 OSU CSE 3

slide-4
SLIDE 4

Example: 24-hr Clock

7 January 2019 OSU CSE 4

Any integer mod 24 is a number between 0 and 23 inclusive.

slide-5
SLIDE 5

Example: 24-hr Clock

7 January 2019 OSU CSE 5

What is 67 mod 24? Twice around is 48, and 19 more makes

  • 67. Hence, 19.
slide-6
SLIDE 6

Example: 24-hr Clock

7 January 2019 OSU CSE 6

And (–67) mod 24? Hint: it is not 19, it is not –19, but rather…

slide-7
SLIDE 7

Example: 24-hr Clock

7 January 2019 OSU CSE 7

And (–67) mod 24? Hint: it is not 19, it is not –19, but rather 5.

slide-8
SLIDE 8

Modulo ≠ Remainder (%)

  • What is the remainder upon dividing 67

by 24? It is 19.

  • What is the remainder upon dividing –67

by 24? It is –19.

– At least most people would say it is, and indeed this is how Java evaluates the expression: (-67) % 24

7 January 2019 OSU CSE 8