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

float variable with only 2 decimal

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



Joined: 25 Nov 2007
Posts: 4

View user's profile Send private message

float variable with only 2 decimal
PostPosted: Sun Jan 06, 2008 9:56 am     Reply with quote

Hi.
i need to know how truncate a float VARIABLE, for example:

1,23456 ----> 1,23

couse i have to compare values with only two decimal numbers.
Thanks.
Ttelmah
Guest







PostPosted: Sun Jan 06, 2008 10:20 am     Reply with quote

Do you absolutely 'have' to be working with float values?. If not, consider using the %w print format, and working using 16bit, or 32bit scaled integers, so you hold 1.23xxx, as 123. Will make comparisons much more accurate, and faster.
If you must use float values, you start to run into the problems that the 'edges' for a trimmed value, will not be well defined, depending on the size of the value concerned. If you want 4/5 rounding, then using a cast is probably the best solution, so:
Code:


if ((int32)(val*100 +0.49999) == 123)


Which will take the float value, multiply it by 100,add 0.49999 (so that 1.236xxx will become 124xxxx), and tun it into an integer for the comparison.
The internal ceil, and floor functions, can also be used on the value*100, but the comparison will be less accurate, and slower.


Best Wishes
trustno1



Joined: 25 Nov 2007
Posts: 4

View user's profile Send private message

PostPosted: Sun Jan 06, 2008 1:59 pm     Reply with quote

Ttelmah wrote:

If you must use float values, you start to run into the problems that the 'edges' for a trimmed value, will not be well defined, depending on the size of the value concerned. If you want 4/5 rounding, then using a cast is probably the best solution, so:
Code:


if ((int32)(val*100 +0.49999) == 123)


Which will take the float value, multiply it by 100,add 0.49999 (so that 1.236xxx will become 124xxxx), and tun it into an integer for the comparison.
The internal ceil, and floor functions, can also be used on the value*100, but the comparison will be less accurate, and slower.


Best Wishes

Thanks for your reply.

I must use float variables!

I already used ceil function but it returns me an error:

"a #device required before this line" in CCS compiler open automatically math.h library...don't know why!!
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