Wrong value on 32 bits in Spin - Propeller
EMHmark7
Posts: 93
Hi,
I assign a value DesiredPos:=ActualPos+24 (both DesiredPos and ActualPos are declared WORD)
Then I call a function without parameters
That called procedure uses the global var DesiredPos.
When I repeat several times the above mentioned, the DesiredPos goes above 128.
At that point, the called procedure using the global var value receives it with the 8 most significant bits all at 1,
So 144 gives 65424.
I already used ~~ and ~ for padding the sign for something else and it fixed the problem (not related to the actual problem), although I do not fully understand when to use it.
Here, I cannot use it on the 24 and I do not catch where I need to put it.
And the value did not kick the 16th bit sign of a word but just the 8th bit of a byte in a word value.
I suppose the black sheep is the 24.
As usual, your anyway always fast help appreciated.
Marc
I assign a value DesiredPos:=ActualPos+24 (both DesiredPos and ActualPos are declared WORD)
Then I call a function without parameters
That called procedure uses the global var DesiredPos.
When I repeat several times the above mentioned, the DesiredPos goes above 128.
At that point, the called procedure using the global var value receives it with the 8 most significant bits all at 1,
So 144 gives 65424.
I already used ~~ and ~ for padding the sign for something else and it fixed the problem (not related to the actual problem), although I do not fully understand when to use it.
Here, I cannot use it on the 24 and I do not catch where I need to put it.
And the value did not kick the 16th bit sign of a word but just the 8th bit of a byte in a word value.
I suppose the black sheep is the 24.
As usual, your anyway always fast help appreciated.
Marc
Comments
I see that the value is already corrupted before the function call.
So the problem seems to appear in the assignation statement.
The value limit is only limited by its type.
At that point writing this post, I got ideas to experiment. (remains a practical question billow)
I fixed it.
The actual solution is (Not sure if it could be simpler):
___
DesiredPos:=~~ActualPos-1000 'Desired position
if ~~DesiredPos < ~~MinPos 'Reached minimum position
DesiredPos:= ~~MinPos
MoveToDo:=~~DesiredPos-~~ActualPos
PC.nlTextDec(string("MinPos:") ,~~MinPos)
'Debug monitoring. Even display should be sanitized otherwise misleading on actual value
'Display is a good place to test for a right sanitization
___
So I catched I need to sanitize these WORDS every time I read them in cases where they can get either negative values or values higher the +/1128.
Even MinPos that was declared as BYTE for holding a small values such as 10,
was changed to WORD, if it can help.
But remains the point: Why do we need to sanitize with ~~ every move we do? What would be the right way to declare values so we do not need to load our listing with it? Delcare all Long values? (Or I miss a point).
In my application, I could put a minimum limit to 0, for some stuff but still have to deal with values higher than 128 or 32768.
But using the code in "what if" mode, shows the weaknesses of the algorithm. Always good to test with higher values so we know its limits.
I wanted to point out my concerns (remaining questions) before I comment this:
At that point writing this post, I got ideas to experiment. (so writing the first line of my post)
In the Drupal community, they say, "There is a module for that!"
Well, I could say in the Parallax world: "Write a post in their forum, either you get an answer in the following minutes, or you get the answer writing your post, Best support community I found" (Thanks mainly but not totally to the moderators, thanks to all members, it is the best feature of the Propeller.)
So, ya, JohnnyMac, you help me find the answer, because I found it replying to your post.
You made my day. Mike too, and not less.
Marc
All relevant (I think) that is in my program is:
__
DesiredPos:=~~ActualPos-1000 'Desired position
if ~~DesiredPos < ~~MinPos 'Reached minimum position
DesiredPos:= ~~MinPos
MoveToDo:=~~DesiredPos-~~ActualPos
__
I am sanitizing the values when I read them, because I catched it was a kind of narrow misinterpretation from the compiler.
Are you saying that we can sanitize them naturally when we write into them?
How do we do it? Like this: ~~A:=B+C ? (I think I tried it and the compiler refuses it)
and then, I wont need to use ~~ when reading them?
In the case of DesiredPos:=~~ActualPos-1000
ActualPos being a WORD,
I suppose I need to sanitize that way, because ActualPos is not already a LONG, right?
By the way, hope you saw my comment in post #5, I was thinking primarily about you. : )
Use longs. If you need to transfer the lower 16-bits somewhere later, that's easily done with the .word[0] modifier.
Otherwise, how could we say in a formula that it is a signed value?
I Hope you JonnyMac and Mike saw my non technical comment billow in post #5.
Marc
Thanks,
Marc
I write a LOT of Propeller code. I've never once been concerned about variable space. In actual fact, using bytes or words when longs are okay is actually more work for the compiler because the natural variable type of the Propeller is 32 bits. Of course, things like serial buffers will use arrays of bytes, but for simple variables, I always stick with longs. The few bytes lost to storage are made up for by [under-the-hood] code efficiency.
2) I changed data type to LONG to these vars and removed all ~~ an ~. Everything works (so far) and it compiles with 2 LONG Less!
Great!
Thanks again.
Marc