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

round() in CCS [solved elegantly]

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



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

round() in CCS [solved elegantly]
PostPosted: Thu Jan 21, 2016 9:13 am     Reply with quote

Is there an easy equivalent to the c standard :
IN CCS - that does an honest round() function?

where the input argument is a float and the output is
an integer of the 8 or 16 bit flavor ?


Last edited by asmboy on Fri Jan 22, 2016 8:30 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Thu Jan 21, 2016 9:57 am     Reply with quote

Actually, 'round' is not a C standard function. However a lot of libraries do offer it.
It's easy to code as:
Code:

#define round(x) (x<0.0)?(int16)(x-0.5):(int16)(x+0.5)


an int16, will be truncated to an int8 automatically.
You could use a overloaded function to give int32, int16 & int8 versions if required.
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