220 likes | 552 Views
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
E N D
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 • For example: 2 5 >>result = 77/3 result = 25.6667 >>result = mod(77,3) result = 2 >> 3 7 7 -6 1 7 -1 5 2
1. Modulus • The modulus-function calculates the remainder of a long division >> help mod • For example: >>result = 77/3 result = 25.6667 >>result = mod(77,3) result = 2 >> mod(..) is a function that REQUIRESTWO ARGUMENTS. (mod(77) is an invalid statement…)
1. Modulus • The modulus-function calculates the remainder of a long division >> help mod • For example: 2 5 >>result = 77/3 result = 25.6667 >>result = mod(77,3) result = 2 >> 3 7 7 -6 1 7 -1 5 2 How is this ever useful…?
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:
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:
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:
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
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 But how? What does it mean for a number to be odd?
Ex1. Even or Odd? • A number x is odd if the remainder of the division by 2 is equal to 1. • Translate to coding: “Ifthe remainder of the division by 2is equal to 1.” ifmod(x,2)== 1
Ex1. Even or Odd? • A number x is odd if the remainder of the division by 2 is equal to 1. • Translate to coding: “Ifthe remainder of the division by 2is equal to 1.” ifmod(x,2)== 1 • A number x is odd if the remainder of the division by 2 is not equal to 0. • Translate to coding: “Ifthe remainder of the division by 2is not equal to 0.” ifmod(x,2)~= 0 Think about “even” on your own..
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 When would this happen?
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.
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!!! Let’s apply this to a previous problem that was solved…
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! Let’s add an error message when an float is entered!...
Check for integers, algorithm %prompt user for total players %if invalid (negative, zero, or not integer) %error message %else %generate 1st player %continue with game
Check for integers, code %prompt user for total players totalPlayers = input('How many players (WHOLE number only): '); %if invalid (negative, zero, or not integer) iftotalPlayers<=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… end Using mod() in your answer, what does it mean for a number to not-be-an-integer?
Check for integers, mod() %prompt user for total players totalPlayers = input('How many players (WHOLE number only): '); %if invalid (negative, zero, or not integer) iftotalPlayers<=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… end
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
Global Wrap Up • Concludes the overall module on LIBRARY FUNCTIONS • Vocabulary: function-call, argument, return-values, to collect clc, 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> But the doc requires you to know vocabulary, such as “argument”, “return-value”, “variable”…