INTEGER: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "INTEGER" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(One intermediate revision by the same user not shown)
Line 22: Line 22:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' QBasic signed integers were limited from -32768 to 32767, but could not exceed 32767 or it would error:
''Example 1:'' QBasic signed integers were limited from -32768 to 32767, but could not exceed 32767 or it would error:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO...LOOP|DO}}: {{Cl|_LIMIT}} 2000
{{Cl|DO...LOOP|DO}}: {{Cl|_LIMIT}} 2000
   i% = i% + 1
   i% = i% + 1
   {{Cl|PRINT}} i%
   {{Cl|PRINT}} i%
{{Cl|LOOP}} {{Cl|UNTIL}} i% = 0 '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} i% = 0
{{CodeEnd}}
{{CodeEnd}}
:''Explanation:'' In '''QB64''' the count will go to 32767, then count up from -32768 to 0 before repeating the process without error.  
:''Explanation:'' In '''QB64''' the count will go to 32767, then count up from -32768 to 0 before repeating the process without error.




''Example 2:'' When a signed '''QB64''' INTEGER value exceeds 32767, the value may become a negative value:
''Example 2:'' When a signed '''QB64''' INTEGER value exceeds 32767, the value may become a negative value:
{{CodeStart}} '' ''
{{CodeStart}}
i% = 38000
i% = 38000
{{Cl|PRINT}} i% '' ''
{{Cl|PRINT}} i%
{{CodeEnd}}{{OutputStart}}-27536
{{CodeEnd}}{{OutputStart}}-27536
{{OutputEnd}}
{{OutputEnd}}
Line 41: Line 41:


''Example 3:'' In '''QB64''' [[_UNSIGNED]] INTEGER values greater than 65535 cycle over again from zero:
''Example 3:'' In '''QB64''' [[_UNSIGNED]] INTEGER values greater than 65535 cycle over again from zero:
{{CodeStart}} '' ''
{{CodeStart}}
i~% = 70000
i~% = 70000
{{Cl|PRINT}} i~% '' ''
{{Cl|PRINT}} i~%
{{CodeEnd}}{{OutputStart}} 4464
{{CodeEnd}}{{OutputStart}} 4464
{{OutputEnd}}
{{OutputEnd}}
:''Explanation:'' In QB64 an unsigned integer value of 65536 would be 0 with values increasing by the value minus 65536.  
:''Explanation:'' In QB64 an unsigned integer value of 65536 would be 0 with values increasing by the value minus 65536.





Latest revision as of 01:50, 23 January 2023

INTEGER is a 2-byte number type definition that can hold whole numerical values.


Syntax

DIM variable AS INTEGER


  • Integers do not use decimal point values but will round those off to the nearest even whole number.
  • QBasic integer values can range from -32768 to 32767 without an "overflow" error.
  • For larger integer values use the LONG integer type.
  • QB64 INTEGER values greater than 32767 become negative signed values instead of throwing an "overflow" error, as the top bit designates a negative value. See example 1 below.
  • QB64 _UNSIGNED integers can range from 0 to 65535.
  • QB64 _UNSIGNED _INTEGER64 values range from 0 to 18446744073709551615
  • Many graphic programs require INTEGER arrays.
  • Variable type suffix is % or ~% for _UNSIGNED. Suffix can also be placed after a literal or hexadecimal numerical value.
  • LONG integers use the & suffix and _INTEGER64 use the && suffix.
  • Values can be converted to 2 byte ASCII string values using MKI$ and back with CVI.
  • When a variable has not been defined or has no type suffix, the value defaults to SINGLE.
  • Warning: QBasic keyword names cannot be used as numerical variable names with or without the type suffix.


Examples

Example 1: QBasic signed integers were limited from -32768 to 32767, but could not exceed 32767 or it would error:

DO: _LIMIT 2000
  i% = i% + 1
  PRINT i%
LOOP UNTIL i% = 0
Explanation: In QB64 the count will go to 32767, then count up from -32768 to 0 before repeating the process without error.


Example 2: When a signed QB64 INTEGER value exceeds 32767, the value may become a negative value:

i% = 38000
PRINT i%
-27536
Explanation: Use an _UNSIGNED INTEGER or a ~% variable type suffix for only positive integer values up to 65535.


Example 3: In QB64 _UNSIGNED INTEGER values greater than 65535 cycle over again from zero:

i~% = 70000
PRINT i~%
 4464
Explanation: In QB64 an unsigned integer value of 65536 would be 0 with values increasing by the value minus 65536.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link