View previous topic :: View next topic |
Author |
Message |
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
Float division with two int16 |
Posted: Wed Mar 16, 2005 10:52 am |
|
|
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
|
|
Posted: Wed Mar 16, 2005 11:41 am |
|
|
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
|
|
Posted: Wed Mar 16, 2005 11:51 am |
|
|
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
|
|
Posted: Wed Mar 16, 2005 12:07 pm |
|
|
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
|
|
Posted: Wed Mar 16, 2005 12:09 pm |
|
|
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
|
|
Posted: Wed Mar 16, 2005 12:17 pm |
|
|
Hi SherpaDoug,
Really? I don't work very often with floats on PIC.
So what do you suggest?
Best regards,
Marcus |
|
|
Guest
|
|
Posted: Wed Mar 16, 2005 12:27 pm |
|
|
Kernighan & Ritchie is the ANSI C bible |
|
|
mvaraujo
Joined: 20 Feb 2004 Posts: 59 Location: Brazil
|
|
Posted: Wed Mar 16, 2005 12:37 pm |
|
|
I don't know if you "guest" intended to expose my ignorance on C but I can say that your damn right!
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!
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
|
|
Posted: Wed Mar 16, 2005 12:51 pm |
|
|
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
|
|
Posted: Wed Mar 16, 2005 12:57 pm |
|
|
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
|
|
Posted: Wed Mar 16, 2005 1:01 pm |
|
|
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
|
|
Posted: Wed Mar 16, 2005 1:26 pm |
|
|
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
|
|
Posted: Thu Mar 17, 2005 2:40 am |
|
|
Thanks for your help, it's now working well using :
Time= (float)Capacity / (float)current;
best regards,
Bruno |
|
|
|