Page 1 of 1

Roundings

Posted: Tue Nov 03, 2009 9:48 am
by Eric
Hello,

Does anybody knows a good algoritm to simulate in Flowcode the C- function "floor" and "ceiling" to establish roundings on an integer.
What I mean is this: reading an ADC value of 834 ( representing a temperature of 83.4Β°C ), I want to round it to 830 ( giving a rounded temperature of 83Β°C). Simply deviding by 10 doesn't solve the problem because using this method a reading of eg 837 will result in the rounded value of 83Β°C instead of 84. And there is also the intermediate value of 835 which should result in 84Β°C.
I guess the functions floor and ceiling don't do the rounding correctly. Eg the floor function transforms 83.7 to 83 while the ceiling function transforms 83.2 to 84.

Probably the solution is simple, but I don't see it (yet).
Thanks for any input,

Eric

Re: Roundings

Posted: Tue Nov 03, 2009 11:16 am
by Benj
Hello Eric

You could do something similar to the following.

Mod your entire Integer value by 10 to get the real or floating point portion of the number. Then if this is less then 5 you know you can divide the Integer number correctly by 10. Otherwise divide by 10 and then add 1.

Re: Roundings

Posted: Tue Nov 03, 2009 11:47 am
by Eric
Perfect in its simplicity.

Thx Ben,

Eric

Re: Roundings

Posted: Tue Nov 03, 2009 6:36 pm
by medelec35
This is a simple alternative (although Bens solution is elegant and will of course work)

Just add 5 then divide by 10
e.g (834+5)/10 = 83.9 will truncate to 83
(835+5)/10 = 84

Re: Roundings

Posted: Wed Nov 04, 2009 1:46 pm
by Eric
Hi Medelec,

Even simpler :-)
Shame on me, I definitively was looking way to far for a solution.

Rgds,
Eric