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

Division Problems, need help!

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







Division Problems, need help!
PostPosted: Thu Dec 15, 2005 7:58 am     Reply with quote

Im have this code:

int dientes;
int16 diametro;
int32 longitud


....
....
...

for easy things, i make dientes=10 and diametro=1000

so when i do this:

longitud= ((int32)diametro*31415)/(dientes);

printf(lcd_putc,"%ld",longitud);


insted of 3141500 i get 320561.
if i replace the dientes by 10 like this

longitud=((int32)diametro*31415)/10;
i get the correct result, why???. division bug??

i try this too

longitud=((int32)diametro*31415)/((int32)dientes);
same bad result.

HELP PLEASE
Ttelmah
Guest







PostPosted: Thu Dec 15, 2005 9:37 am     Reply with quote

Have you tried this?.
Code:

dientes=10;
longitud= ((int32)diametro*31415)/dientes;


I all honesty, it is behaving as if dientes, is actually getting set to '98' not '10'.
The error is not one that you would normally associate with an arithmetic error (these tend to be binary multiples, or binary 'masks' - where a value has overflowed the allocated storage size).
How is dientes being set/calculated?.

Best Wishes
Dieguito
Guest







PostPosted: Thu Dec 15, 2005 9:48 am     Reply with quote

You where right!!, Embarassed Embarassed

I dont know how even if i use #Zero_Ram

dientes was not zero!!!.

now is working, because before calculating dientes, i put it in zero.

Thanks
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Dec 15, 2005 10:25 am     Reply with quote

If dientes is getting values you are not giving it, check out where it is placed in RAM. Make sure it is not mapped to a register that is also used for something else. Possibly your header file is in error and dientes is being mapped to a register associated with a hardware function.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ttelmah
Guest







PostPosted: Thu Dec 15, 2005 10:48 am     Reply with quote

It is though also worth saying, that if 'dientes', is a variable in a subroutine, you should never expect it to be 'zero', unless you have declared it as zero in the routne (int8 dientes=0;).
The reason is that CCS will reuse memory, that is not declared for 'static' or 'global' variables. So if you have a 'main', calling first one subroutine, then another, variables in the second subroutine, may well reuse the same memory area that has been used in the first. This is one reason why I don't use #zero_ram. This declaration, as well as having some problems on particular chips, may 'mislead' you into expecting a variable to be zero, when this is not guaranteed. All 'zero_ram', does, is ensure that the RAM is zero, at the moment of boot. From then on, it's contents _will_ change. Hence it is better to explicitly declare variables, and _never_ assume their contents.

Best Wishes
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