Ad Space — Top Banner

?ILLEGAL QUANTITY ERROR

Commodore VIC-20

Severity: Minor

What Does This Error Mean?

?ILLEGAL QUANTITY ERROR means a value passed to a BASIC function is outside the allowed range. Common triggers: SQR of a negative number, a negative array subscript, or a PEEK/POKE address outside valid range.

Affected Models

  • Commodore VIC-20

Common Causes

  • SQR() of a negative number (square root of negative is undefined)
  • LOG() of zero or a negative number
  • Array subscript is negative (e.g. A(-1))
  • POKE or PEEK with an address outside 0-65535
  • CHR$() with a value outside 0-255
  • Negative value passed to a function expecting a positive number

How to Fix It

  1. Find the function causing the error and check its argument.

    The error message shows the line number. Type LIST <line> to see which function is receiving the bad value.

  2. Add ABS() for square roots to handle negative inputs safely.

    If your data might be negative and you need the square root, use SQR(ABS(X)) instead of SQR(X). This takes the absolute value first.

  3. Validate array subscripts before use.

    If an index comes from INPUT or a calculation, add a check: IF I < 0 OR I > 10 THEN PRINT "Bad index" : GOTO 100 Then use A(I) safely.

  4. For POKE/PEEK, verify the address is within 0-65535.

    The VIC-20 has a 16-bit address space: 0 to 65535. A negative or calculated address outside this range causes ?ILLEGAL QUANTITY. Print the address value before the POKE to check it: PRINT ADR : POKE ADR, VAL

Frequently Asked Questions

What is the valid range for CHR$() on the VIC-20?

CHR$() accepts values from 0 to 255. Values map to PETSCII characters — the Commodore character set used on all 8-bit Commodore machines. CHR$(256) or higher causes ?ILLEGAL QUANTITY ERROR.

Why does SQR give this error but not other maths functions?

SQR requires a non-negative input because the square root of a negative number is imaginary. Most other maths functions (SIN, COS, TAN) accept any real number without range restrictions.