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 support@ccsinfo.com

arithmatic problem.

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







arithmatic problem.
PostPosted: Sat Jun 13, 2009 12:22 am     Reply with quote

HI, I have a question about variable types.
Code:

#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\16f886.h>
#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\def_16f886.h>
#fuses INTRC_IO
#use delay(clock = 1000000)

int8 hund_digit=0;
int8 thou_digit=0;
int8 ten_digit = 0;
int8 one_digit = 0;


int16 num2display = 438;

void disp_rtn(int16 buffer){
   thou_digit = buffer/1000;
   buffer -= thou_digit*1000;
   hund_digit = buffer/100;
   buffer -=hund_digit*100;
   ten_digit = buffer/10;
   one_digit = buffer-ten_digit*10;
   }
   
void main(void){
   disp_rtn(num2display);
}

The coding does not separate correct digits. However, if i change the variable types of thou_digit,hund_digit, etc, to int16, all the sudden it starts working again.
Anybody have an idea?
jaethelegend
Guest







additional question
PostPosted: Sat Jun 13, 2009 12:34 am     Reply with quote

Code:
void main(void){
init_rtn();

   while(1){
adc_in = READ_ADC();
disp_number = adc_in/1023*5000;
disp_rtn(disp_number);
   }
}

Does not work
but if i change
Code:
disp_number = adc_in/1024*5000;

to
Code:
disp_number = adc_in*4.886;

All the sudden it starts working
Anybody has an idea?
Thanks
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jun 13, 2009 1:43 am     Reply with quote

If I remember right, a similar example has been discussed a few days before.
Code:
buffer -=hund_digit*100;

Gives only correct result, if hund_digit <2, because the multiply result is of int8 type. You must typecast one term to int16 before, e.g.:
Code:
buffer -=(int16)hund_digit*100;

Also the second example shows, that you should think about the way, integer expressions are evaluated by the compiler. adc_in/1024 can be supposed to have a result of zero for all possible ADC values.
Code:
(int32)adc_in*5000/1024
should give the intended result.
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