View previous topic :: View next topic |
Author |
Message |
carlosma
Joined: 24 Mar 2004 Posts: 53 Location: Portugal
|
int from float value |
Posted: Wed Mar 30, 2005 4:52 pm |
|
|
Hello
please I have this problem:
Code: |
float var_A, var_B;
//var_A and var_B is used some times during the program
var_A= 54,75;
var_B = 6,7532;
var_A = var_A*var_B; // the real value is 369,7377
//Now I only need the entire part of the value 369.
|
how to make this? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 30, 2005 5:17 pm |
|
|
Look at the floor() function in the CCS Help file or the manual. |
|
|
carlosma
Joined: 24 Mar 2004 Posts: 53 Location: Portugal
|
|
Posted: Thu Mar 31, 2005 2:03 am |
|
|
I need another way to do this, because the program use 99% of the ROM and have no space for more math.c functions.
I use 16F877 and the program is big
Thanks Carlos |
|
|
Ttelmah Guest
|
|
Posted: Thu Mar 31, 2005 3:05 am |
|
|
Seriously, you can't have something for nothing. Any solution is going to involve code, and so you need to find a way of reducing your code size, or having more space (18F452...).
You might try using 'var_c' as an int16, then var_c=var_a*var_b, will do the multipliation using a float, and cut the result to an integer using the internal cast. However the size will be close to the same. Where this would save space, is if you then print the result, since printing a float, uses a lot more code space than printing an integer.
Best Wishes |
|
|
valemike Guest
|
|
Posted: Thu Mar 31, 2005 3:38 pm |
|
|
I've seen a lot of posts about people running out of space.
I've gone thru the following chips, from start to finish:
16F876 --> 18F252 --> 18F2620
18F448 --> 18F458
* The 16F chips will use more ROM than the equivalent code in its 18F counterpart.
* 18F chips are not much more expensive than its counterpart 16F chip.
* Pre 3.191 code that uses up 90% ROM in an 18F seems to get reduced down to 70+% using the newer PCH versions.
By all means, get a new chip and/or upgrade your compiler! |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Mar 31, 2005 5:08 pm |
|
|
This is where you need to examine the LST file. Look at the asm code and find ways to reduce it. If you are accessing arrays quite a bit, then you might look at using a pointer instead. Printf's can eat up a lot as well. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Apr 03, 2005 2:08 pm |
|
|
Do you really need floats? Floats require a lot of memory space, a workaround is to use a technique called 'scalled integers'. For example instead of performing the float multiplication 54,75 * 6,7532 you do the integer multiplication 547500 * 67532 and skip the four least significant digits. |
|
|
|