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

conversion float to int8

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



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

conversion float to int8
PostPosted: Tue Jun 09, 2009 4:31 pm     Reply with quote

I am developing a thermostat based on a DS18b20.

I would like to compared the temperature stored in a float variable to an integer 8 bit.

For this purpose I would like to convert the float variable into a int8 and then make the comparison ?
How to do this conversion assuming that I am not interested in the decimal part of the float ?

Thanks in advance for the hints !! ,

Dynamitron
PCM programmer



Joined: 06 Sep 2003
Posts: 18029

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 4:58 pm     Reply with quote

Do it by casting the float to an int. C tutorial page:
http://www.cs.cf.ac.uk/Dave/C/node9.html#SECTION00930000000000000000
nostep



Joined: 04 Mar 2009
Posts: 16

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 5:05 am     Reply with quote

You don't necessarily have to convert it then compare it. You could do this:
Code:

// declare variables
int num_int;
float num_float;

// Get your data into the variables as you need to
num_int=127;
number_float=124.3;

// Here's the comparison using a "cast" method
if(num_int > (int)num_float) // do what you need to do now
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 1:57 pm     Reply with quote

Thanks to both of you, this is exactly what I wanted.
I like the "no conversion" comparison.

Dynamitron
Ttelmah
Guest







PostPosted: Wed Jun 10, 2009 2:11 pm     Reply with quote

If you had looked at the link PCM programmer gave, you would see exactly the same suggestion. This _is_ a cast, and what you are doing, is converting the float to an int, before the conversion.....

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