View previous topic :: View next topic |
Author |
Message |
s_mack
Joined: 04 Jun 2009 Posts: 107
|
Simple rounding when converting a float to an int |
Posted: Sat Aug 29, 2009 11:21 am |
|
|
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
|
|
Posted: Sat Aug 29, 2009 11:33 am |
|
|
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
|
|
Posted: Sat Aug 29, 2009 3:16 pm |
|
|
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
|
|
Posted: Sat Aug 29, 2009 7:27 pm |
|
|
Thanks. I gave your suggestion a shot, but it actually used 2 bytes more than the +=0.5 method
Interesting implementation though!
I'm using a Pic 18F4480. |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Aug 29, 2009 9:49 pm |
|
|
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 _________________ Andrew |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Sat Aug 29, 2009 11:10 pm |
|
|
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. |
|
|
|