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

confused with Square Root task...

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







confused with Square Root task...
PostPosted: Wed Apr 14, 2004 4:02 pm     Reply with quote

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...
PostPosted: Wed Apr 14, 2004 4:32 pm     Reply with quote

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
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