Can someone explain Modulo operator in SPIN - specific unexpected behavior
ags
Posts: 386
I have been debugging a problem and have it isolated to a specific line of SPIN code. It basically is this:
value : = 4832//1461
I would expect value to contain the value 449. Instead, it contains 193. Can anyone explain this? I suspect some form of overflow but don't know how/why. The manual just says that a division is performed, and a 32-bit remainder is returned
Thanks
value : = 4832//1461
I would expect value to contain the value 449. Instead, it contains 193. Can anyone explain this? I suspect some form of overflow but don't know how/why. The manual just says that a division is performed, and a 32-bit remainder is returned
Thanks
Comments
As usual it's impossible to advice with out a fully working piece of code that shows the problem.
Many times we see problems "isolated to a specific line of SPIN code" that turn out to be elsewhere.
Bingo you got it:
4832 mod 1461 mod 256 is indeed 193.
Next case.
@Heater - yes, you are correct on that front too. I don't have access to the code now. No further effort requested until I can post standalone code that exhibits the problem - or I figure it out while doing so (it happens all the time)
Thanks
Well, to close this out officially - I was back at it yesterday and it was, as predicted, a coding error. Not as obvious as assigning a value >256 to a single byte, but resulting from improper memory reference in other code that was stepping on the MSB where the value was stored.
One of the first things I often do when debugging is some basic math on the expected vs measured values. Off by power of two, or truncation to byte-multiples are easily seen this way. I had just measured the actual value and had to drop the project for the moment. Before posting, I should have tested a simple program that did nothing but the modulo operation with the exact values I saw. Instead, before I could get back to the actual prototype I started thinking about SPIN maybe having a limitation on the modulo operand size (i.e. it was implemented in PASM using immediate value...).
Keep it simple. Do the obvious first. Don't try to debug after three 2-hour sleep nights in a row. Ugh.