Strange behavior with math (Resolved)
Greg LaPolla
Posts: 323
I may be missing something here but this has been driving me crazy for 2 days.
I have 2 variables timer_val and timer_ela. I can perform simple math functions (add and subtract) on them, however if I perform anything on them together I get strange results like:
lets say timer_val is 60 and timer_ela is 3
timer_rem := timer_val - timer_ela
timer_rem will be equal to 60, but should be 57.
timer_rem := timer_val -1
timer_rem will be equal to 59
timer_rem := timer_ela + 1
timer_rem will be 4.
All these variables are declared as type long, does anyone have any insight on this ?
Greg
Post Edited (Greg LaPolla) : 7/6/2009 12:18:19 AM GMT
I have 2 variables timer_val and timer_ela. I can perform simple math functions (add and subtract) on them, however if I perform anything on them together I get strange results like:
lets say timer_val is 60 and timer_ela is 3
timer_rem := timer_val - timer_ela
timer_rem will be equal to 60, but should be 57.
timer_rem := timer_val -1
timer_rem will be equal to 59
timer_rem := timer_ela + 1
timer_rem will be 4.
All these variables are declared as type long, does anyone have any insight on this ?
Greg
Post Edited (Greg LaPolla) : 7/6/2009 12:18:19 AM GMT
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL: propeller.wikispaces.com/pPropQL
pPropQL020: propeller.wikispaces.com/pPropQL
OMU for the pPropQL/020 propeller.wikispaces.com/OMU
That's a rather blunt appraisal, but I have to concur.
-Phil
timer_val is obtained by calling Resistance method in RcResistance.spin
timer_ela is obtained by calling the rdReg method in timer.spin using the TMR_MINS parameter
here is the code to get the resistance:
basically what this is is a 10K 10 turn pot that is used th set the value of timer_val. if you press a momentary switch it allows to take a reading from the pot then press it again to return to the timer.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL: propeller.wikispaces.com/pPropQL
pPropQL020: propeller.wikispaces.com/pPropQL
OMU for the pPropQL/020 propeller.wikispaces.com/OMU
-Phil
Print the values of timer_val, timer_ela and timer_rem and you will find out how you get false results... then investigate the source.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL: propeller.wikispaces.com/pPropQL
pPropQL020: propeller.wikispaces.com/pPropQL
OMU for the pPropQL/020 propeller.wikispaces.com/OMU
timer_rem := timer_val - timer_ela
will always be equal to timer_val.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL: propeller.wikispaces.com/pPropQL
pPropQL020: propeller.wikispaces.com/pPropQL
OMU for the pPropQL/020 propeller.wikispaces.com/OMU
if I set timer_rem := timer_ela
The timer display on the screen will increment by 1 every minute
if I set timer_rem := timer_val
The timer display on the screen will show the reading from the pot.
If I set timer_rem := timer_ela + 1
The timer display on the screen will show the reading from the timer + 1
if I set timer_rem := timer_val + 1
The timer display on the screen will show the reading from the pot + 1
Hence my problem that timer_rem := timer_val - timer_ela will always be equal to timer_val.
So the difference is 3 minutes?
That sounds like an overroll in CNT.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
everything works as expected withe the proper results except for this line:
timer_rem := timer_val - timer_ela
no matter what timer_ela is this equation will always be equal to timer_val.
Did you try a print of the values directly before and after the assignment?
Did you try so filler-vars to avaoid overwrites?
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Making sure your stack is big enough is a good idea. Otherwise, the value for one of your operands in the subtract is simply not what you think it is. Spin math is airtight. If the compiler/interpreter were making an error of the kind you supect, it would've been discovered and corrected long before now.
One of the hardest lessons to learn when debugging is to search for what you did wrong, rather than convincing yourself that what you did was right. ("It should work!") After forty years of programming, I still have to check myself on this issue!
-Phil
It's OK if you say that *before* letting run the program. But if it fails, the only allowable question is "what did I do wrong", and the only allowable statement is "I don't get it", or "I'm an idiot" (after you found the bug).
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
please use the archive function of the propellertool to zip ALL lines of code that you were using
you made several quick shots that didn't solved the problem.
So it is time to do a careful analyse for EVERY DAMMED SINGLE line of code.
we can go on guessing into the fog for weeks and month or take half a day to
I repeat myself
a careful analyse for EVERY DAMMED SINGLE line of code.
best regards
Stefan
Your absolutely right after almost 30 years I still fall into that hole too!
This one has me perplexed.
I have done as nick suggested looking at the values before and after and also trying filler vars, everything is correct.
I really don't think the stack is the issue. I am not performing any calculations in the cog running timer.
I even tried changing variable names to X, Y and Z just to make sure nothing was overwriting.
Greg
Never make assumptions. Only verification helps.
> I even tried changing variable names to X, Y and Z just to make sure nothing was overwriting.
Do you think the variables are sorted alphabetically in RAM?
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
-Phil
isolate the code into the smallest chunks that will run the code block in question - eliminate everything else. If the problem goes away... well, obviously add in code piece by piece. (The lazy way to check 'every damn line of code' [noparse];)[/noparse]
- H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Can you add a serial port object, and serial.dec(timer_ela) ?? I suspect you will get a 0
I'd say test that for sure, then find out why it became 0
if you expected some other value.
regards peter
-Phil
Greg
Sorry for granny-telling: The first thing you should have for µC-debugging is a serial connection on the µC and a terminal on the other end. As soon as you do have that, you have won. All else is simply a PITA to work with.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO