Roundings

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
Eric
Posts: 99
Joined: Tue Nov 06, 2007 11:04 pm
Been thanked: 1 time
Contact:

Roundings

Post 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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Roundings

Post 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.
Attachments
Rounder.fcf
(4 KiB) Downloaded 246 times

Eric
Posts: 99
Joined: Tue Nov 06, 2007 11:04 pm
Been thanked: 1 time
Contact:

Re: Roundings

Post by Eric »

Perfect in its simplicity.

Thx Ben,

Eric

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Roundings

Post 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
Attachments
Rounder2.fcf
(4.5 KiB) Downloaded 261 times
Martin

Eric
Posts: 99
Joined: Tue Nov 06, 2007 11:04 pm
Been thanked: 1 time
Contact:

Re: Roundings

Post by Eric »

Hi Medelec,

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

Rgds,
Eric

Post Reply