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

Simple rounding when converting a float to an int

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



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

Simple rounding when converting a float to an int
PostPosted: Sat Aug 29, 2009 11:21 am     Reply with quote

I've seen a dozen very complicated answers to similar (but not exact) questions. I'm just trying to save ROM space. Is there a simpler (and/or more efficient) way of converting a float to an int but rounding to the nearest rather than truncating?

This is what I'm doing now but it adds a suprising amount of size to the program vs simply letting it truncate and I'm hoping there's a way to write it more efficiently.

Code:

int8 pedal_output;
float pedal_setting = 4.5678; //an example. in practice we get this from a function
pedal_output = (pedal_setting - (int8)pedal_setting >= 0.5 ? (int8)pedal_setting+1 : (int8)pedal_setting);
//example output rounds value up to 5


Thanks.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sat Aug 29, 2009 11:33 am     Reply with quote

This is slightly more efficient (less ROM), and I *think* it produces the same results (?)
Code:

int8 pedal_output;
float pedal_setting = 4.5678; //an example. in practice we get this from a function
pedal_setting += 0.5; //add half a point then truncate normally
pedal_output = (int8)pedal_setting;


That saves 100 bytes per instance. Any better ideas?
Ttelmah
Guest







PostPosted: Sat Aug 29, 2009 3:16 pm     Reply with quote

There is actually a completely 'ludicrous' way of doing this:

result = ((int16)(val*2)+1)/2;

It is silly, but the compiler can double a fp number in only a very few instructions (by just adjusting the exponent). This is then converted to integer, one is added, and the result divided by 2, all in integer.

Using int16 (otherwise it'll only work to 127), and obviously as shown, it'll only work with +ve numbers, but this gives rounding, in 192 bytes less, than simply adding 0.5!.... (on a PIC16).
This gives 4/5 rounding, which is the more normal requirement.

Best Wishes
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sat Aug 29, 2009 7:27 pm     Reply with quote

Thanks. I gave your suggestion a shot, but it actually used 2 bytes more than the +=0.5 method Smile

Interesting implementation though!

I'm using a Pic 18F4480.
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Sat Aug 29, 2009 9:49 pm     Reply with quote

The best way to reduce floating point code size is to not use floating point! The concept is, instead of using floating point to count dollars use integers and count cents.

Did you try:
Code:
pedal_output = (int8)(pedal_setting + 0.5);
?

Edit: Yes, you did Embarassed
_________________
Andrew
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sat Aug 29, 2009 11:10 pm     Reply with quote

Yeah, if I could avoid floats I would but I don't think it is feasible for this application. While I have to send a (rounded) int8 to the final controller, from iteration to iteration I need to maintain a significant accuracy and the math is rather complex so floats are necessary. Otherwise, yes I'd just use integers as you suggest with your dollars and cents analogy.


I'll presume for now that the +0.5 is the most efficient way to go. By the way, thanks for posting what you did... it made me recognize that the way I did it (pedal_setting += 0.5;) was screwing up my code!! It was increasing the variable's value by 0.5 and carrying that over to the next iteration rather than just for rounding purposes during the int8 conversion. Simple but stupid mistake.
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