1 / 21

The Modulus Function

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

Download Presentation

The Modulus Function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 1. Modulus • The modulus-function calculates the remainder of a long division >> help mod

  3. 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

  4. 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…)

  5. 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…?

  6. 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. 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. 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:

  9. 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. 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?

  11. 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

  12. 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..

  13. 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?

  14. 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.

  15. 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…

  16. 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!...

  17. 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

  18. 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?

  19. 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

  20. 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

  21. 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”…

More Related