BleuLlama / TinyBasicPlus

A C implementation of Tiny Basic, with a focus on support for Arduino
406 stars 118 forks source link

FOR loop with top limit of 32767 #17

Closed gabrias closed 6 years ago

gabrias commented 8 years ago

I don't know if that is a error inherent to basic language in general, but when i use the value 32767 as the top limit for the FOR loop, the loop will run forever since the variable will go from 32767 to -32768 and keep running.

drawkula commented 6 years ago

This is not a typical feature of all BASICs... ;-)

The addition in https://github.com/BleuLlama/TinyBasicPlus/blob/083bba9237770f05b3971f2ff5b1a2a51dcd5530/Arduino/TinyBasicPlus.ino#L1501 overflows into the sign bit.

For the same reason

10 FOR I=-32760 TO -32768 STEP -1
20 PRINT I
30 NEXT I

runs into positive numbers instead of terminating at -32768.

BleuLlama commented 6 years ago

Closed, TinyBasic functions as designed. If this is an issue, open a new ticket to cover overflows or whatever. This is pretty standard for integer basics.