|
|
View previous topic :: View next topic |
Author |
Message |
brian m Guest
|
floating points and maths |
Posted: Wed Mar 10, 2004 6:26 pm |
|
|
Quick question for you all.
i'm trying to do some calculations, i'm decalring some variables as doubles, however i'm getting an error saying it can't handle floating points.
does anybody know how to overcome this i need to be able to display decimals on my lcd for a project i'm doing.
also i can multiply no problem but divison doesn't work. any suggestions.
i'm using a 16f877 and not to sure of the ccs version. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 10, 2004 7:38 pm |
|
|
The double data type is not supported. You have to
declare them as float. Example:
float my_value;
To find your compiler version, compile a source file
without errors and then look at the top of the .LST file.
Example:
Compile TEST.C
Then look at TEST.LST |
|
|
Guest
|
|
Posted: Thu Mar 11, 2004 6:23 am |
|
|
float doesn't seem tobe working.
its only accepting ints, whcih are only 8 bit so its giving a range of -127 to +127
any ideas. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Mar 11, 2004 8:54 am |
|
|
Just because you need to display decimals in your output doesn't mean you have to use floats. Floating point numbers use a lot of processor resources and should be avoided if at all possible.
Instead consider using scaled integers. If you need to display temperature to 1/100 of a degree, consider calculating it in integer "centidegrees." A signed long can then hold +/-327.68 degrees, enough for many applications.
As many on this list know I previously worked on a product that measured ship hulls, looking for rust, dents, warpage, etc.. We did our measurements in 24 bit integer millimeters. We could cover anything from the depth of a dent to the length of a supertanker. We did need another number format for measuring steel and paint thickness, 16 bit integer mils.
When we displayed data we would either print it to a string and use string functions to insert decimal points and commas in appropriate places, or use / and % to break the number where needed for printing. It took a little more programming effort than just declairing everything as huge floats, but the processing power required was dramatically lower and round-off errors were exactly predictable. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Thu Mar 11, 2004 10:25 am |
|
|
Anonymous wrote: | float doesn't seem tobe working.
its only accepting ints, whcih are only 8 bit so its giving a range of -127 to +127
any ideas. |
You are really going to have to find the compiler version. Floats have been supported for longer than the 16F877, so something is 'silly'. Are you sure you have a 'full' compiler version, not a 'demo'?. I seem to remember one of the demo versions lacked float support.
As another poster has said, it can often be better, faster, and more code efficient to use a 'scaled integer'. However to do this you would need at least int16 support, or int32 support, and you seem to be stuck with signed int8. This is even 'odder', since the default type is unsigned. Are you even sure the compiler is 'CCS'?...
Best Wishes |
|
|
Thomas Blake
Joined: 18 Jan 2004 Posts: 22 Location: Burbank CA
|
Re: Floats |
Posted: Thu Mar 11, 2004 11:17 am |
|
|
While I agree that something is goofy, perhaps even your compiler, I also agree that you should avoid floating point and use fixed-point as mentioned above. Essentially you should do a high-precision result, convert to BCD (assuming that's what your display accepts) and then just turn on the decimal point at the proper position on your display. You really should move the decimal point position for scale change if that is visually acceptable, because it's much simpler than any other way.
Another advantage of doing that is that you don't have to worry about IEEE-to-display format conversion. BCD is irritating enough as it is!
tcb |
|
|
brian m Guest
|
|
Posted: Thu Mar 11, 2004 11:44 am |
|
|
i've manged to sole the problem.
int count=0;
float speed=0;
speed=((float)count*(float)(22.00/7.00)*2*33);
printf(lcd_putc,"\n");
printf(lcd_putc,"\nC= %d,secs= %3.2f",count,speed); |
|
|
|
|
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
|