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
Roundings
Moderator: Benj
- 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
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.
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 247 times
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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
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
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
Re: Roundings
Hi Medelec,
Even simpler![Smile :-)](./images/smilies/icon_smile.gif)
Shame on me, I definitively was looking way to far for a solution.
Rgds,
Eric
Even simpler
![Smile :-)](./images/smilies/icon_smile.gif)
Shame on me, I definitively was looking way to far for a solution.
Rgds,
Eric