?DIVISION BY ZERO ERROR
Commodore Commodore 64
Severity: MinorWhat Does This Error Mean?
?DIVISION BY ZERO ERROR means your program attempted to divide a number by zero. Dividing by zero is mathematically undefined. Add an IF check before any division to guard against a zero divisor.
Affected Models
- Commodore 64
- Commodore 64C
- Commodore 128 (C64 mode)
- VICE emulator (C64)
Common Causes
- Explicit division by zero: PRINT 10/0
- A variable used as a divisor that happens to be zero
- User INPUT that provides a zero value which is then used as a divisor
- A calculation that produces zero as an intermediate result used in a division
How to Fix It
-
Find the line that caused the error using LIST.
BASIC displays the line number when the error occurs. LIST that line and look for a division (/) operation.
-
Add an IF check before every division to guard against zero.
Before dividing by a variable: IF D = 0 THEN PRINT "CANNOT DIVIDE BY ZERO" : GOTO 100 This prevents the error and gives the user a meaningful message.
-
Check INPUT values before using them as divisors.
If the divisor comes from user input, always validate it: INPUT D : IF D = 0 THEN PRINT "ENTER A NON-ZERO NUMBER" : GOTO (the input line)
-
Trace the calculation that produces the divisor to find where zero originates.
Sometimes a variable is zero because of an earlier calculation or because it was never assigned a value. All unassigned numeric variables on the C64 default to zero.
Frequently Asked Questions
What is the default value of an unassigned variable on the C64?
All numeric variables default to 0 on the Commodore 64. This means a variable that was never assigned but is used as a divisor will silently cause DIVISION BY ZERO.
Does the C64 have any built-in protection against divide by zero?
No — it simply throws the error and stops the program. Protection must be written by the programmer using IF checks before each division.