CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

subtraction signed int8

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

subtraction signed int8
PostPosted: Tue Oct 21, 2014 1:14 pm     Reply with quote

This is only a test program.

I have tested it in MPLAB V8.92. with the simulator.
It is compiled in CCS 5.027! and tested in real hardware (not the posted code)


Only the first OK1 is working all the other fail! But why?

I am Confused.



Code:
#include <18F26k22.h>
#use delay(clock=8M,int)
#use RS232(Baud=57600,STOP=1,PARITY=N,BITS=8,UART1,ERRORS,stream=sim900)


/*Max dif: ad-adtmp=+-5 adtmp-ad=+-5*/
void GetTemp2(){
 int8 ok=0;
 int8 ad,adtmp;
 int8 cnt;

 ad=127;
 adtmp=127;

 for (cnt=0; cnt<10; cnt++){
  ad++; /* Test with ad--, ad++, adtmp--, adtmp++ */
  printf("AD:%u ADTmp:%u Cnt:%u\r\n",ad,adtmp,cnt);

  ok=0;
  if ( (((signed int8)ad-adtmp)<5) && (((signed int8)ad-adtmp)>-5) ) {ok=1;}
  printf("OK1:%u\r\n",ok);

  ok=0;
  if ( ((-adtmp+ad)<5) && ((-adtmp+ad)>-5) ) {ok=1;}
  printf("OK2:%u\r\n",ok);

  ok=0;
  if ( ((ad+(-adtmp))<5) && ((ad+(-adtmp))>-5) ) {ok=1;}
  printf("OK3:%u\r\n",ok);

  ok=0;
  if ( ((ad+(-adtmp))<5) && ((adtmp+(-ad))<5) ) {ok=1;}
  printf("OK4:%u\r\n",ok);
 
  printf("******\r\n");
 }
}


void main() {
 GetTemp2();
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Tue Oct 21, 2014 1:23 pm     Reply with quote

A signed int8, can hold values from -128 to +127.
An unsigned int8, uses the same storage to hold 0 to 255.
You start at 127.
Then increment - now have 128. When this is converted to a signed int8, you get -1.
Then increment again - now have 129. When this is converted to a signed int8, you have -2.

etc. etc.

You are overflowing the signed int8 arithmetic, just about everywhere....
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Tue Oct 21, 2014 2:06 pm     Reply with quote

Now I ended up with this working on:
Code:
if (ad>adtmp) { ok=(ad-adtmp)<5; } else { ok=(adtmp-ad)<5; }


Thanks for hints on the overflow:-)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group