Shop OBEX P1 Docs P2 Docs Learn Events
Can someone explain Modulo operator in SPIN - specific unexpected behavior — Parallax Forums

Can someone explain Modulo operator in SPIN - specific unexpected behavior

agsags Posts: 386
edited 2013-03-26 12:44 in Propeller 1
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

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2013-03-25 11:57
    449 would be the correct answer. 193 is 449 - 256. Is "value" a byte variable? It looks like you are only get the 8 least significant bits of the result.
  • Heater.Heater. Posts: 21,230
    edited 2013-03-25 12:01
    Looks very odd

    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.

  • Heater.Heater. Posts: 21,230
    edited 2013-03-25 12:03
    Dave,

    Bingo you got it:

    4832 mod 1461 mod 256 is indeed 193.

    Next case.
  • agsags Posts: 386
    edited 2013-03-25 12:05
    @Dave Hein - yes, it does look like an overflow.
    @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
  • JonnyMacJonnyMac Posts: 9,191
    edited 2013-03-25 12:40
    value must be a byte variable -- I did a quick check and 193 is what you should get if you assign the result of that operation to a byte.
  • agsags Posts: 386
    edited 2013-03-26 12:44
    Boo on me.

    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.
Sign In or Register to comment.