21
1. mod() 2. Properties of mod() 3. Ex1: even or odd? 4. Ex2: error when not a whole number The Modulus Function 1

The Modulus Function

Embed Size (px)

DESCRIPTION

The Modulus Function. mod() Properties of mod() Ex1: even or odd? Ex2: error when not a whole number. 1. Modulus. The modulus-function calculates the remainder of a long division >> help mod. 1. Modulus. The modulus-function calculates the remainder of a long division >> help mod - PowerPoint PPT Presentation

Citation preview

Page 4: The Modulus Function

1. Modulus

The modulus-function calculates the remainder of a long division

>> help mod

For example:

4

>>result = 77/3result = 25.6667>>result = mod(77,3)result = 2>>

mod(..) is a function that REQUIRES TWO ARGUMENTS.(mod(77) is an invalid statement…)

Page 6: The Modulus Function

2. Properties of mod()

If x is evenly divisible by y (i.e no left-overs), mod(x,y) will return 0

“mod” any number with another one “N”, the return-value will be a whole number from 0 to N-1. For example:

6

Mod by 2

mod(2,2) 0

mod(3,2) 1

mod(4,2) 0

mod(5,2) 1

mod(6,2) 0

mod(15,2) ?

Mod by 3

mod(3,3) 0

mod(4,3) 1

mod(5,3) 2

mod(6,3) 0

mod(7,3) 1

mod(26,3) ?

Mod by 5

mod(2,5) 0

mod(5,5) 0

mod(6,5) 1

mod(7,5) 2

mod(8,5) 3

mod(9,5) 4

mod(10,5) ?

Page 7: The Modulus Function

2. Properties of mod()

If x is evenly divisible by y (i.e no left-overs), mod(x,y) will return 0

“mod” any number with another one “N”, the return-value will be a whole number from 0 to N-1. For example:

7

Mod by 2

mod(2,2) 0

mod(3,2) 1

mod(4,2) 0

mod(5,2) 1

mod(6,2) 0

mod(15,2) ?

Mod by 3

mod(3,3) 0

mod(4,3) 1

mod(5,3) 2

mod(6,3) 0

mod(7,3) 1

mod(26,3) ?

Mod by 5

mod(2,5) 0

mod(5,5) 0

mod(6,5) 1

mod(7,5) 2

mod(8,5) 3

mod(9,5) 4

mod(10,5) ?

Page 8: The Modulus Function

2. Properties of mod()

If x is evenly divisible by y (i.e no left-overs), mod(x,y) will return 0

“mod” any number with another one “N”, the return-value will be a whole number from 0 to N-1. For example:

8

Mod by 2

mod(2,2) 0

mod(3,2) 1

mod(4,2) 0

mod(5,2) 1

mod(6,2) 0

mod(15,2) ?

Mod by 3

mod(3,3) 0

mod(4,3) 1

mod(5,3) 2

mod(6,3) 0

mod(7,3) 1

mod(26,3) ?

Mod by 5

mod(2,5) 0

mod(5,5) 0

mod(6,5) 1

mod(7,5) 2

mod(8,5) 3

mod(9,5) 4

mod(10,5) ?

Page 9: The Modulus Function

Ex1. Even or Odd?

Prompt the user for a whole number, then display whether that number is even or odd.

Algorithm is rather straightforward!% prompt the user for whole number

% if number is odd% Display ‘odd’

% elseif number is even% Display ‘even’

% else%error message

9

Page 10: The Modulus Function

Ex1. Even or Odd?

Prompt the user for a whole number, then display whether that number is even or odd.

Algorithm is rather straightforward!% prompt the user for whole number

% if number is odd% Display ‘odd’

% elseif number is even% Display ‘even’

% else%error message

10

But how?

What does it mean for a number to be odd?

Page 12: The Modulus Function

Ex1. Even or Odd?

A number x is odd if the remainder of the division by 2 is equal to 1. Translate to coding:

“If the remainder of the division by 2 is equal to 1.”

if mod(x,2) == 1

A number x is odd if the remainder of the division by 2 is not equal to 0. Translate to coding:

“If the remainder of the division by 2 is not equal to 0.”

if mod(x,2) ~= 0

12Think about “even” on your own..

Page 13: The Modulus Function

Ex1. Even or Odd?

% prompt the user for whole number

nb = input(‘Enter a WHOLE number: ’);

%if number is odd

if mod(nb,2) == 1

fprintf(‘%d is odd\n’, nb)

elseif mod(nb,2) == 0 %nb is even

fprintf(‘%d is even\n’,nb)

else

disp(‘Number is invalid’)

end

13

When would this happen?

Page 15: The Modulus Function

Ex1. Even or odd? – issues?

The remainder of a division of a float value will never give a 0 or a 1!! The number is neither even, nor odd.

Ironically, mod() can be used to fix this issue!!!

15

Let’s apply this to a previous problem that was solved…

Page 16: The Modulus Function

Ex2: Check for integers

Remember “Who Should Start?”% prompt how many players total

totalPlayers = input('How many players (WHOLE number only): ');

% generate the one who starts (0-max)

startPlayer = ceil(rand*totalPlayers);

% continue with game…

fprintf('Player #%d will start.\n', startPlayer);

Since there are no error-check, the following can happen!

16Let’s add an error message when an float is entered!...

Page 18: The Modulus Function

Check for integers, code

%prompt user for total players

totalPlayers = input('How many players (WHOLE number only): ');

%if invalid (negative, zero, or not integer)

if totalPlayers<=0 || ????

%error message

disp(‘error. Enter a positive WHOLE number only!’);

else %input was valid, proceed with code

%generate 1st player

startPlayer = ceil(rand*totalPlayers);

%continue with game…

end18

Using mod() in your answer, what does it mean for a number to not-be-an-integer?

Page 19: The Modulus Function

Check for integers, mod()

%prompt user for total players

totalPlayers = input('How many players (WHOLE number only): ');

%if invalid (negative, zero, or not integer)

if totalPlayers<=0 || mod(totalPlayers,1)~=0

%error message

disp(‘error. Enter a positive WHOLE number only!’);

else

%generate 1st player

startPlayer = ceil(rand*totalPlayers);

%continue with game…

end19

Page 20: The Modulus Function

Wrapping Up mod() is a built-in function that calculates the remainder of a

division >> help mod <enter> to see help

Commonly used to check if a number is divisible by another. In other word, mod can be used to check if a number is a

multiple of another.

mod(.., 2) is used to check even/odd mod(.., 1) is used to check whole/decimal number mod(.., N) is used to check if a number is divisible by N

20

Page 21: The Modulus Function

Global Wrap Up

Concludes the overall module on LIBRARY FUNCTIONS Vocabulary: function-call, argument, return-values, to

collectclc, clear, sin(..), cos(..), sqrt(..), atan(..)

fprintf(..), input(..), disp(..)

rand, round(..), ceil(..), floor(..)

mod(..)

There are million more! For the curious: isnan(..), isinteger(..), isempty(..)

Ask for help, read the documentation!

>> doc msgbox <enter> 21

But the doc requires you to know vocabulary, such as “argument”, “return-value”, “variable”…