1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-26 15:35:53 +00:00
QB64-PE/internal/help/MOD.txt
SMcNeill 6e01fc8dce Altered string compare routines (<,<=,>,>=) so they don't give false results with CHR$(0).
Added new _STRCMP and _STRICMP commands for quick string comparisons.
Cleaned up QB64 to finish removing the QUI (quick user insert) code and folders.
Altered UCASE and LCASE routines to be faster in some situations for us.
2014-09-22 08:19:03 -04:00

102 lines
4.3 KiB
Plaintext

'''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|&quot;Division by zero&quot; 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 &quot;Integer division =&quot;; I%, &quot;Remainder =&quot;; 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$ = &quot; Normal: ####.# / #### = ##.### &quot;
tmp2$ = &quot; Integer: ####.# \ #### = ### &quot;
tmp3$ = &quot; Remainder: ####.# MOD #### = #### &quot;
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$ &lt;&gt; &quot;&quot;
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}} &quot;Enter a base number system 2 to 36: &quot;, b%
{{Cl|IF...THEN|IF}} b% &lt; 2 {{Cl|OR (boolean)|OR}} b% &gt; 36 {{Cl|THEN}} {{Cl|EXIT DO}}
{{Cl|PRINT}} &quot;Enter a positive value to convert: &quot;;
num$ = &quot;&quot;
{{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&amp; = {{Cl|VAL}}(num$)
{{Cl|IF...THEN|IF}} n&amp; = 0 {{Cl|THEN}} {{Cl|EXIT DO}}
Bnum$ = BASEN$(n&amp;, b%)
{{Cl|PRINT}} Bnum$ ', {{Cl|VAL}}(&quot;{{Cl|&amp;H}}&quot; + Bnum$) 'tests hexadecimal base 16 only
{{Cl|LOOP}}
{{Cl|END}}
{{Cl|FUNCTION}} BASEN$ (number&amp;, basenum%)
{{Cl|IF...THEN|IF}} basenum% &lt; 2 {{Cl|OR (boolean)|OR}} basenum% &gt; 36 {{Cl|OR (boolean)|OR}} number&amp; = 0 {{Cl|THEN}} {{Cl|EXIT FUNCTION}}
num&amp; = number&amp; 'protect value of number!
DO
remain% = {{Cl|ABS}}(num&amp;) {{Cl|MOD}} basenum% ' remainder is used to create actual digit 0 to Z
num&amp; = num&amp; \ basenum% ' move up one exponent of base% with integer division
{{Cl|IF...THEN|IF}} remain% &gt; 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&amp; = 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 [[&amp;H|Hexadecimal]](0 - F).
''See also:''
* [[/|/ (normal division operator)]]
* [[\|\ (integer division operator)]]
* [[INT]], [[CINT]], [[FIX]], [[_ROUND]]
* [[Mathematical Operations]]
{{PageNavigation}}