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

Float division with two int16

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



Joined: 01 Feb 2005
Posts: 21
Location: Paris

View user's profile Send private message

Float division with two int16
PostPosted: Wed Mar 16, 2005 10:52 am     Reply with quote

Hello, i'am actually programming a PIC 16F877A with CCS 3.150, i'am trying to do a division between two int16 variables and put the result in a float variable.

This is my code:

*****

int16 Current=0;
int16 Capacity=0;
float Time=0;
.......
Current=Readcurrent();
Capacity=(int8)Readcapacity();
Current=abs(Current);
Time=Capacity/current;
printf("remaining time %f" , Time);
printf(lcd_putc,"\fremaining time \n %f" ,Time);
*****

if Capcity=3, and Current=2, the result will be 1,99999
if Capcity=12, and Current=4, the result will be 3.99999
the result on the LCD and on the Serial is the same.


I don't know why the result is not good.
Thanks for your help.
Bruno
mvaraujo



Joined: 20 Feb 2004
Posts: 59
Location: Brazil

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 11:41 am     Reply with quote

Bruno,

I don't have the compiler here to test up, but try to:

Code:

 Time = (float) Capacity/current;


It'll force data casting before calculating.

Best regards,

Marcus
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 11:51 am     Reply with quote

CCS has some non ANSI rules with casts, automatic casting not always
happen as expected.

Try this:

Time= (float)Capacity / (float)current;

Humberto
SherpaDoug



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

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 12:07 pm     Reply with quote

This looks bad:

int16 Current=0;
int16 Capacity=0;
float Time=0;

Time=Capacity/current;

According the proper C convention the PIC will divide the integer Capacity by the integer current producing an integer result. Then it will cast this integer result to a float and assign it to Time.

You probably want:

Time=(float)Capacity/(float)current;

This will cast Capacity and current to floats so that the compiler will do a floating point division.
_________________
The search for better is endless. Instead simply find very good and get the job done.
SherpaDoug



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

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 12:09 pm     Reply with quote

mvaraujo wrote:
Bruno,

I don't have the compiler here to test up, but try to:

Code:

 Time = (float) Capacity/current;


It'll force data casting before calculating.

Best regards,

Marcus


According to Kernighan & Ritchie that will do an integer divide and THEN cast the resulting integer to float.
_________________
The search for better is endless. Instead simply find very good and get the job done.
mvaraujo



Joined: 20 Feb 2004
Posts: 59
Location: Brazil

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 12:17 pm     Reply with quote

Hi SherpaDoug,

Really? I don't work very often with floats on PIC.

So what do you suggest?

Best regards,

Marcus
Guest








PostPosted: Wed Mar 16, 2005 12:27 pm     Reply with quote

Kernighan & Ritchie is the ANSI C bible
mvaraujo



Joined: 20 Feb 2004
Posts: 59
Location: Brazil

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 12:37 pm     Reply with quote

I don't know if you "guest" intended to expose my ignorance on C but I can say that your damn right! Surprised

I started working on uc assembly, went to C compilers to make some things easier but I'm that kind of guy that always read the list file to see if on assembly it seems reasonable. In summary, as programmer, if feel much like an engineer! Razz

As some others, I would like to see the right answer to the guy, so we can all learn.

Regards,

Marcus
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 12:51 pm     Reply with quote

Wink Sorry I didn't sign in. I just wanted you to know, so you are informed. People look to this book because it was written by the designer.
_________________
-Matt
mvaraujo



Joined: 20 Feb 2004
Posts: 59
Location: Brazil

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 12:57 pm     Reply with quote

Hi Matt,

Thanks for the reference!

I was just kidding with my last message. We're all learning, right? At least in different levels!

Take care!

Marcus
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 1:01 pm     Reply with quote

yeah, I didn't know what a cast was a year ago.
_________________
-Matt
SherpaDoug



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

View user's profile Send private message

PostPosted: Wed Mar 16, 2005 1:26 pm     Reply with quote

mvaraujo wrote:
Hi SherpaDoug,

Really? I don't work very often with floats on PIC.

So what do you suggest?

Best regards,

Marcus


Time=(float)Capacity/(float)current;

This will cast Capacity and current to floats so that the compiler will do a floating point division.
_________________
The search for better is endless. Instead simply find very good and get the job done.
global



Joined: 01 Feb 2005
Posts: 21
Location: Paris

View user's profile Send private message

PostPosted: Thu Mar 17, 2005 2:40 am     Reply with quote

Thanks for your help, it's now working well using :

Time= (float)Capacity / (float)current;

best regards,
Bruno
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