Puzzling behaviour?

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Richard Russell
Posts: 540
Joined: Tue 18 Jun 2024, 09:32

Puzzling behaviour?

Post by Richard Russell »

I spent far too much time puzzling over this seemingly unexpected behaviour:

Code: Select all

dest%() = a - b   : REM Reports 'Number too big' error  
dest%() = (a - b) : REM Runs successfully, no error
only to realise that it isn't unexpected at all! The key thing to appreciate is that it's assigning to an array, not to a scalar variable (had that been the case it would definitely have been worrying), and that means the two statements do quite different things internally.

The first statement initially copies a into every element of array dest%() then it subtracts b from every element of dest%(). The second statement subtracts b from a and then copies the difference into every element of array dest%().

But why should that make a difference to the behaviour? Most likely it won't, but in my particular case the issue was that whilst a - b was a value compatible with a 32-bit integer (i.e. in the range -2147483648 to +2147483647) the value of a itself wasn't!

So when the first statement attempted to copy a into every element of dest%() it quite correctly failed with a 'Number too big' error, whereas the second statement succeeded because only the difference was being copied into the array. 8-)