Data Types¶
BASIC-80 supports four data types.
Type Suffixes¶
Variables are typed by their suffix character:
| Suffix | Type | Range | Precision |
|---|---|---|---|
| % | INTEGER | -32768 to 32767 | Whole numbers |
| ! | SINGLE | ±2.938736×10^-39 to ±1.701412×10^38 | ~7 significant digits |
| # | DOUBLE | ±2.938736×10^-308 to ±1.797693×10^308 | ~16 significant digits |
| $ | STRING | 0 to 255 characters | Text |
INTEGER (%)¶
Whole numbers from -32768 to 32767.
Examples:
Uses: - Loop counters - Array subscripts - Counting operations - Fast arithmetic (no decimal calculations)
SINGLE Precision (!)¶
Floating-point numbers with ~7 significant digits of precision.
Examples:
Range: Approximately 2.938736×10^-39 to 1.701412×10^38
Uses: - Prices and money (with rounding) - Measurements - General calculations
Default type: Variables without a suffix are SINGLE by default.
DOUBLE Precision (#)¶
Floating-point numbers with ~16 significant digits of precision.
Examples:
Range: Approximately 2.938736×10^-308 to 1.797693×10^308 (much larger range than single-precision, with greater precision)
Uses: - Scientific calculations - High-precision mathematics - Astronomy, physics calculations
Exponent Notation: - D notation (e.g., 1.5D+10) forces double-precision representation in the code itself - E notation (e.g., 1.5E+10) uses single-precision representation by default, but will convert to double if assigned to a # variable - For practical purposes, both work with # variables, though D notation makes the intent explicit
STRING ($)¶
Text data, up to 255 characters per string.
Examples:
String Operations:
- Concatenation: A$ + B$
- Comparison: A$ = B$, A$ < B$
- Functions: LEFT$, RIGHT$, MID$, LEN, INSTR
Type Conversion¶
Automatic Conversion¶
BASIC automatically converts between numeric types when needed:
Explicit Conversion Functions¶
| Function | Converts To | Description |
|---|---|---|
| CINT | INTEGER | Rounds to nearest integer |
| CSNG | SINGLE | Converts to single precision |
| CDBL | DOUBLE | Converts to double precision |
| STR$ | STRING | Converts number to string |
| VAL | Number | Converts string to number |
Example:
10 X! = 3.7
20 I% = CINT(X!) ' I% = 4 (rounded)
30 S$ = STR$(X!) ' S$ = " 3.7"
40 Y = VAL("123.45") ' Y = 123.45
Default Types¶
You can set default types for variable names using DEF statements:
10 DEFINT A-Z ' All variables are INTEGER by default
20 DEFSNG A-C ' Variables A-C are SINGLE
30 DEFDBL D-F ' Variables D-F are DOUBLE
40 DEFSTR S ' Variables starting with S are STRING
See: DEFINT/SNG/DBL/STR
Type Coercion Rules¶
When mixing types in expressions:
- INTEGER + INTEGER = INTEGER
- INTEGER + SINGLE = SINGLE
- INTEGER + DOUBLE = DOUBLE
- SINGLE + DOUBLE = DOUBLE
The result takes the "wider" type (DOUBLE > SINGLE > INTEGER).
Overflow and Underflow¶
INTEGER Overflow:
Solution: Use SINGLE or DOUBLE for larger numbers.
Floating-Point Overflow:
See Error Codes for more information on error 6 (OV - Overflow).
Underflow (too small):
String Length Limit¶
Strings are limited to 255 characters:
Memory Usage¶
| Type | Bytes | Memory Efficiency |
|---|---|---|
| INTEGER | 2 | Most efficient |
| SINGLE | 4 | Moderate |
| DOUBLE | 8 | Least efficient |
| STRING | 3 + length | Variable |
Tip: Use INTEGER when possible for faster arithmetic and less memory.
See Also¶
- Variables - Using variables
- Operators - Arithmetic and logical operators
- Type Conversion Functions - CINT, CSNG, CDBL, STR$, VAL
- DEFINT/SNG/DBL/STR - Set default types