mirror of
https://github.com/QB64Official/qb64.git
synced 2024-09-20 09:04:44 +00:00
102 lines
No EOL
4.3 KiB
Text
102 lines
No EOL
4.3 KiB
Text
'''MOD''' is mathematical [[INTEGER]] remainder division. Also called Modulus, it can work with integer division when a remainder is necessary.
|
|
|
|
|
|
{{PageSyntax}}
|
|
:: remainder = ''numerator'' '''MOD''' ''divisor''
|
|
|
|
|
|
{{Parameters}}
|
|
* Returns the integer division remainder as a whole [[INTEGER]], [[LONG]] or [[_INTEGER64]] value.
|
|
* ''numerator'' is the [[INTEGER]] value to divide.
|
|
* ''divisor'' is the [[INTEGER]] value to divide by.
|
|
|
|
|
|
''Usage:''
|
|
* Floating decimal point ''numerator'' and ''divisor'' values are [[CINT]] rounded (e.g. 19 MOD 6.7 returns 5 just like 19 MOD 7 would).
|
|
* MOD returns 0 if a number is evenly divisible by Integer division ( [[\]] ) or the number divided is 0.
|
|
* '''Divisor (second value) must not be a value of 0 to .5'''. This will create a [[ERROR Codes|"Division by zero" error!]] due to [[CINT]] rounding.
|
|
* The result has the same sign as the numerator (e.g. -1 MOD 7 returns -1, not 6).
|
|
* Division and multiplication operations are performed before addition and subtraction in Qbasic's order of operations.
|
|
|
|
|
|
''Example 1:''
|
|
{{CodeStart}}
|
|
I% = 100 {{Cl|\}} 9
|
|
R% = 100 {{Cl|MOD}} 9
|
|
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 #### = #### "
|
|
FOR i = 1 TO 6
|
|
SELECT CASE i
|
|
CASE 1: numerator = 1: divisor = 5
|
|
CASE 2: numerator = 13: divisor = 10
|
|
CASE 3: numerator = 990: divisor = 100
|
|
CASE 4: numerator = 1100: divisor = 100
|
|
CASE 5: numerator = 4501: divisor = 1000
|
|
CASE 6: numerator = 50.6: divisor = 10
|
|
END SELECT
|
|
LOCATE 5, 20: PRINT USING tmp1$; numerator; divisor; numerator / divisor
|
|
LOCATE 7, 20: PRINT USING tmp2$; numerator; divisor; numerator \ divisor
|
|
LOCATE 9, 20: PRINT USING tmp3$; numerator; divisor; numerator MOD divisor
|
|
DO: SLEEP: LOOP UNTIL INKEY$ <> ""
|
|
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}}
|
|
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!
|
|
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).
|
|
|
|
|
|
''See also:''
|
|
* [[/|/ (normal division operator)]]
|
|
* [[\|\ (integer division operator)]]
|
|
* [[INT]], [[CINT]], [[FIX]], [[_ROUND]]
|
|
* [[Mathematical Operations]]
|
|
|
|
|
|
{{PageNavigation}} |