|
|
View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
confused with Square Root task... |
Posted: Wed Apr 14, 2004 4:02 pm |
|
|
Assuming i'm using the default #types as
int is 8 bits;
long is 16 bits;
char is 8 bits;
Suppose I have:
Code: |
float x1; /* a number from 0 - 999,999 */
int n = 3;
code = x1 - sqrt(x1 * (float) n);
|
Okay, so let x1=54321.0.
After calculating code = 543210.0 - sqrt ( 543210.0 * 3.0),
I get something like: 541933.962702
I want to truncate the decimal portion, so the code is now:
"541933". I would like to save the single digits in eeprom as:
0x00: 5
0x01: 4
0x02: 1
0x03: 9
0x04: 3
0x05: 3
How can I truncate the decimal portion and get the digits "5", "4", "1", "9", "3", "3" separated out digit by digit?
I initially tried casting the float 541933.962702 into a long int, but with default data type sizes, then 541933 would exceed the 65,535 (or 32767 if signed) limit of a long. And I can't use the % operator either, since it doesn't work on floating point types.
I would hate to have to to 32-bit-sized ints, since this will screw up the rest of my non-portably written code, and CCS warns not to change the #type sizes, or else i won't be able to use the examples and driver libraries. |
|
|
Ttelmah Guest
|
Re: confused with Square Root task... |
Posted: Wed Apr 14, 2004 4:32 pm |
|
|
valemike wrote: | Assuming i'm using the default #types as
int is 8 bits;
long is 16 bits;
char is 8 bits;
Suppose I have:
Code: |
float x1; /* a number from 0 - 999,999 */
int n = 3;
code = x1 - sqrt(x1 * (float) n);
|
Okay, so let x1=54321.0.
After calculating code = 543210.0 - sqrt ( 543210.0 * 3.0),
I get something like: 541933.962702
I want to truncate the decimal portion, so the code is now:
"541933". I would like to save the single digits in eeprom as:
0x00: 5
0x01: 4
0x02: 1
0x03: 9
0x04: 3
0x05: 3
How can I truncate the decimal portion and get the digits "5", "4", "1", "9", "3", "3" separated out digit by digit?
I initially tried casting the float 541933.962702 into a long int, but with default data type sizes, then 541933 would exceed the 65,535 (or 32767 if signed) limit of a long. And I can't use the % operator either, since it doesn't work on floating point types.
I would hate to have to to 32-bit-sized ints, since this will screw up the rest of my non-portably written code, and CCS warns not to change the #type sizes, or else i won't be able to use the examples and driver libraries. |
sprintf.
If you use:
int8 array[8];
sprintf(array,"%.0F",code);
Then the array, should contain 0x35 in the fist entry. 0x34 in the second etc. (the ASCII representations of the digits). You can either store these in ASCII (makes it easy to read in), or just subtract 0x30 from each digit.
Best Wishes |
|
|
|
|
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
|