mirror of
https://github.com/QB64Official/qb64.git
synced 2024-08-22 02:05:08 +00:00
103 lines
4.5 KiB
Text
103 lines
4.5 KiB
Text
The [[MOD]] operator gives the remainder after division of one number by another (sometimes called modulus).
|
|
|
|
|
|
{{PageSyntax}}
|
|
:{{Parameter|remainder}} = {{Parameter|numerator}} [[MOD]] {{Parameter|divisor}}
|
|
|
|
|
|
{{PageParameters}}
|
|
* Returns the integer division remainder as a whole [[INTEGER]], [[LONG]] or [[_INTEGER64]] value.
|
|
* {{Parameter|numerator}} is the [[INTEGER]] value to divide.
|
|
* {{Parameter|divisor}} is the [[INTEGER]] value to divide by.
|
|
|
|
|
|
{{PageDescription}}
|
|
* Floating decimal point ''numerator'' and ''divisor'' values are [[CINT]] rounded (e.g. {{InlineCode}}19 MOD 6.7{{InlineCodeEnd}} returns 5 just like {{InlineCode}}19 MOD 7{{InlineCodeEnd}} would).
|
|
* MOD returns 0 if a number is evenly divisible by integer division ( [[\]] ) or the number divided is 0.
|
|
* '''{{Parameter|divisor}} (second value) must not be between 0 and .5'''. This will create a [[ERROR Codes|"Division by zero" error]] due to [[CINT]] rounding the value to 0.
|
|
* The result has the same sign as the numerator (e.g. {{InlineCode}}-1 MOD 7{{InlineCodeEnd}} returns -1, not 6).
|
|
* Division and multiplication operations are performed before addition and subtraction in QBasic's order of operations.
|
|
|
|
|
|
{{PageExamples}}
|
|
''Example 1:''
|
|
{{CodeStart}}
|
|
I% = 100 {{Cl|\}} 9
|
|
R% = 100 {{Cl|MOD}} 9
|
|
{{Cl|PRINT}} "Integer division ="; I%, "Remainder ="; R%
|
|
{{CodeEnd}}
|
|
{{OutputStart}}
|
|
Integer division = 11 Remainder = 1 '' ''
|
|
{{OutputEnd}}
|
|
|
|
''Explanation:'' Integer division 100 \ 9 returns 11. 11 [[*]] 9 = 99. So the remainder must be 1 as 100 - 99 = 1. Normal decimal point division would return 11.11111.
|
|
|
|
|
|
''Example 2:'' Comparing normal, integer and remainder division.
|
|
{{CodeStart}}
|
|
tmp1$ = " Normal: ####.# / #### = ##.### "
|
|
tmp2$ = " Integer: ####.# \ #### = ### "
|
|
tmp3$ = " Remainder: ####.# MOD #### = #### "
|
|
{{Cl|FOR}} i = 1 {{Cl|TO}} 6
|
|
{{Cl|SELECT CASE}} i
|
|
{{Cl|CASE}} 1: numerator = 1: divisor = 5
|
|
{{Cl|CASE}} 2: numerator = 13: divisor = 10
|
|
{{Cl|CASE}} 3: numerator = 990: divisor = 100
|
|
{{Cl|CASE}} 4: numerator = 1100: divisor = 100
|
|
{{Cl|CASE}} 5: numerator = 4501: divisor = 1000
|
|
{{Cl|CASE}} 6: numerator = 50.6: divisor = 10
|
|
{{Cl|END SELECT}}
|
|
{{Cl|LOCATE}} 5, 20: {{Cl|PRINT USING}} tmp1$; numerator; divisor; numerator / divisor
|
|
{{Cl|LOCATE}} 7, 20: {{Cl|PRINT USING}} tmp2$; numerator; divisor; numerator \ divisor
|
|
{{Cl|LOCATE}} 9, 20: {{Cl|PRINT USING}} tmp3$; numerator; divisor; numerator {{Cl|MOD}} divisor
|
|
{{Cl|DO}}: {{Cl|SLEEP}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
|
|
{{Cl|NEXT}} '' ''
|
|
{{CodeEnd}}
|
|
|
|
|
|
''Example 3:'' Integer division and MOD can be used to convert values to different base numbering systems from base 2 to 36 as [[STRING|strings]]:
|
|
{{CodeStart}} '' ''
|
|
{{Cl|CLS}}
|
|
{{Cl|DO}}
|
|
{{Cl|INPUT}} "Enter a base number system 2 to 36: ", b%
|
|
{{Cl|IF...THEN|IF}} b% < 2 {{Cl|OR (boolean)|OR}} b% > 36 {{Cl|THEN}} {{Cl|EXIT DO}}
|
|
{{Cl|PRINT}} "Enter a positive value to convert: ";
|
|
num$ = ""
|
|
{{Cl|DO...LOOP|DO}}: K$ = {{Cl|INKEY$}}
|
|
num$ = num$ + K$
|
|
{{Cl|LOCATE}} {{Cl|CSRLIN}}, {{Cl|POS}}(0): {{Cl|PRINT}} K$;
|
|
{{Cl|LOOP}} {{Cl|UNTIL}} K$ = {{Cl|CHR$}}(13)
|
|
n& = {{Cl|VAL}}(num$)
|
|
{{Cl|IF...THEN|IF}} n& = 0 {{Cl|THEN}} {{Cl|EXIT DO}}
|
|
Bnum$ = BASEN$(n&, b%)
|
|
{{Cl|PRINT}} Bnum$ ', {{Cl|VAL}}("{{Cl|&H}}" + Bnum$) 'tests hexadecimal base 16 only
|
|
{{Cl|LOOP}}
|
|
|
|
{{Cl|END}}
|
|
|
|
{{Cl|FUNCTION}} BASEN$ (number&, basenum%)
|
|
{{Cl|IF...THEN|IF}} basenum% < 2 {{Cl|OR (boolean)|OR}} basenum% > 36 {{Cl|OR (boolean)|OR}} number& = 0 {{Cl|THEN}} {{Cl|EXIT FUNCTION}}
|
|
num& = number& 'protect value of number!
|
|
{{Cl|DO}}
|
|
remain% = {{Cl|ABS}}(num&) {{Cl|MOD}} basenum% ' remainder is used to create actual digit 0 to Z
|
|
num& = num& \ basenum% ' move up one exponent of base% with integer division
|
|
{{Cl|IF...THEN|IF}} remain% > 9 {{Cl|THEN}}
|
|
b$ = {{Cl|CHR$}}(65 + (remain% - 10)) 'limited to base 36
|
|
{{Cl|ELSE}}: b$ = {{Cl|LTRIM$}}({{Cl|STR$}}(remain%)) ' make remainder a string number
|
|
{{Cl|END IF}}
|
|
BN$ = b$ + BN$ ' add remainder character to base number string
|
|
{{Cl|LOOP}} {{Cl|UNTIL}} num& = 0
|
|
BASEN$ = BN$
|
|
{{Cl|END FUNCTION}} '' ''
|
|
{{CodeEnd}}
|
|
: ''Note:'' Base numbering systems over base 10(0 - 9) use alphabetical letters to represent digits greater than 9 like [[&H|Hexadecimal]](0 - F).
|
|
|
|
|
|
{{PageSeeAlso}}
|
|
* [[/|/ (normal division operator)]]
|
|
* [[\|\ (integer division operator)]]
|
|
* [[INT]], [[CINT]], [[FIX]], [[_ROUND]], [[_CEIL]]
|
|
* [[Mathematical Operations]]
|
|
|
|
|
|
{{PageNavigation}}
|