View previous topic :: View next topic |
Author |
Message |
cmesparza
Joined: 03 Jan 2012 Posts: 4 Location: Pamplona
|
signed long array behaving as a long array [SOLVED] |
Posted: Tue Jan 17, 2012 11:00 am |
|
|
Hi everyone,
I'm having an issue with a "signed long array" declared as a global variable.
Code: |
signed long U[4]={0,0,0,0};
long lambda[25][2];
|
Code: |
signed long sal,consecuente[5]={-15,-10,0,10,15};
float num,den,aux;
int i,cons;
for(i=0;i<25;i++) {
cons=lambda[i][1];
aux=lambda[i][0];
aux=aux*consecuente[cons];
num=aux+num;
den=lambda[i][0]+den;
}
sal=num/den;
U[j]=sal;
|
When the result of the function above is negative (e.g.: -15) sal=15, but I get U[j]=65521 (65536-15) instead of -15.
Has someone had the same issue? I'm programming a PIC18F27J53 using MPLAB 8.8 with CCS 4.120 plugin.
Thanks in advance _________________ CME IngenierĂa
www.cmeingenieria.es
Last edited by cmesparza on Tue Jan 17, 2012 1:40 pm; edited 1 time in total |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1343
|
|
Posted: Tue Jan 17, 2012 11:05 am |
|
|
I may be misunderstanding your issue, but 65521 is the signed 2's compliment 16 bit representation of -15, which is what I would expect.
65535 = -1
65534 = -2
65533 = -3
...
65522 = -14
65521 = -15 |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Jan 17, 2012 11:33 am |
|
|
How are you examining the result? Is the problem in your print function, or maybe in MPLAB's display routines? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Tue Jan 17, 2012 12:45 pm |
|
|
All numbers are represented by notation. 2's complement is a convenient notation for the operation on registers by the PIC. We are used to decimal notation so we must be aware of the decimal translation we request for a PIC register. A PIC register can notate an ascii char a signed integer an unsigned integer or maybe just a piece of a larger number that requires more than one register and many other notations. The register is transistors in binary states so the appropriate translation to decimal notation must be specified. Signed notation incorrectly specified as unsigned will result in a wrong decimal translation. |
|
|
cmesparza
Joined: 03 Jan 2012 Posts: 4 Location: Pamplona
|
|
Posted: Tue Jan 17, 2012 1:40 pm |
|
|
You were all right, I was looking at the value of the variable, not to the decimal value.
The program works perfectly, I wasn't thinking in the 2's complement...
Thanks! _________________ CME IngenierĂa
www.cmeingenieria.es |
|
|
|