View previous topic :: View next topic |
Author |
Message |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
Float64 multiplication on PIC24 |
Posted: Wed Aug 17, 2011 8:03 am |
|
|
The micro is a 24FJ128GA106
The compiler is V4.121
The equation is simple and this is only part of the whole program and all other float64 numbers are calculating correctly.
Code: |
ban = -123.260887;
rovx = -931.42884;
man = .00191901;
ban = (float64)(ban - (rovx * man));
//here ban should equal -121.48
//compiler says ban = 6.89959398673419E305
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Aug 17, 2011 8:46 am |
|
|
I have to ask:
1) How are ban, man, and rovx, declared?. You use a 'cast' for the result, which only affects the result, not the arithmetic in the earlier parts.
2) How are you seeing the result?. You say "compiler says ban = 6.89959398673419E305", yet the _compiler_ has no ability to display a value, so presumably you are using some form of debugger?.
I took the code as is. Attached a UART, and compiled with:
Code: |
#use rs232(baud=115200,parity=N,bits=8,errors,UART1)
void main() {
float64 ban;
float64 rovx;
float64 man;
ban = -123.260887;
rovx = -931.42884;
man = .00191901;
ban = ban - (rovx * man);
printf("%16.12f\n\r",ban);
do {
} while (TRUE);
}
|
Result happily prints as:
-125.048308258248
Best Wishes |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Aug 17, 2011 2:05 pm |
|
|
I was too brief.
All variables are declared Float64 and the cast was just an attempt to find the problem. I used the debugger to test the result. In my watch list an on a mouse-over the result is always the same. I changed to this and started getting better results:
Code: |
float32 ban1,ban2;
float64 ban;
ban1 = rovy;
ban2 = rovx * man;
ban = (float64)ban1 - ban2;
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Aug 17, 2011 2:50 pm |
|
|
Have you got the stack overflow trap enabled?.
If not, I'd guess your stack is too small, and just happens to be overflowing on this arithmetic. Without the trap to force a reset, you get random results.....
#build(stack=512)
The default stack is too small for just about anything.....
Best Wishes |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Aug 17, 2011 3:59 pm |
|
|
I had 256 but will try 512.
Also, why does pi show in the watch list as 1078530011? It looks like anywhere I use pi is doing math correctly but the ide displays this bizarre number. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Aug 17, 2011 4:17 pm |
|
|
You don't need to program the stack overflow trap to get a reset on overflow. Using the trap, you can recognize or possibly handle the stack fault. But using a larger stack frame isn't a bad idea.
Which debugger are you using? Are you sure that it can handle double format correctly? With MPLAB, you'll need to select float formats explicitly for variables in the watch window, otherwise, only garbage is displayed. |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Thu Aug 18, 2011 8:19 am |
|
|
I found the problem.
I had 2 variables defined as just float and used them in a float64 variable equation. I redefined the variables and now I get the results I like.
Anyone figure out why I can't see PI displayed correctly in the IDE? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Aug 18, 2011 8:58 am |
|
|
As I said in my first post :
"1) How are ban, man, and rovx, declared?. You use a 'cast' for the result, which only affects the result, not the arithmetic in the earlier parts."
On the stack, I suspect some versions of the compiler incorrectly have variables in use, inside the stack area. Have had 'odd results' from arithmetic, with stack overflow not being triggered, yet enlarging the stack has fixed this.....
On PI, remember it is just a #define, not a variable. You should see it as a value, if you do something basic like:
float64 Pval=PI;
and then watch Pval
It is defined in math.h.
Your debugger is probably 'watching' some odd location in memory, which just happens to contain bytes corresponding to 107830011.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Aug 18, 2011 11:37 am |
|
|
Quote: | On the stack, I suspect some versions of the compiler incorrectly have variables in use, inside the stack area. Have had 'odd results' from arithmetic, with stack overflow not being triggered, yet enlarging the stack has fixed this.... |
Do you have a reproducible example for this issue? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Aug 18, 2011 2:59 pm |
|
|
A few versions ago. Just expanded the stack for other reasons, and the problems disappeared. Didn't bother to waste time chasing it down at the time, and haven't seen it in the last few versions. The version here (4.121), is latter than the versions I had this problem with, but it seemed worth expanding the stack to avoid one possible problem.
Best Wishes |
|
|
|