Hi,
I wanted to ceil/floor a number and couldn't find any predefined POUs available. So i tried to create this (ceil): and it seems didn't work, (probably) because of the REAL number is rounded during conversion to DINT.
Note: VAL1 is a DINT with value 1. IN and OUT are both REAL number.

the result is as follow:
IN 0.5 -> OUT 1
IN 1.0 -> OUT 1
IN 1.5 -> OUT 3 (strange, this is the error)
IN 2.0 -> OUT 2
IN 2.5 -> OUT 3

and so, my questions are:
how exactly a number is ceiled* or floored*?
what probably caused this error?
is there any predefined floor/ceil instead?

Thank you,
Seftikara.

*I don't know the right grammar
Quote 0 0
Your algorithm is flawed. If you want to build ceil or floor instructions, you need to evaluate the decimal part of the number. Normally to do that you need to multiply the number by 10 so that you get the first decimal into the integer part. Then you just subtract the original integer part times 10 to the number and what is left is your decimal number. Ex:

3.254 -> times 10 -> 32.54 -> REAL_TO_DINT -> 32
3.254 -> REAL_TO_DINT -> 3 -> times 10 -> 30
32 - 30 = 2 (this is your first digit decimal part)

Then you can use this number to determine if you want to round your number up or down, which is basically adding one to the original 3.254 number or not.

In any case, you don't have to go over all this trouble (unless you want to do it for a mental exercise). There are CEIL FLOOR function blocks all over the OSCAT library. Download this and be happy: https://openplc.discussion.community/post/oscat-basic-333-oscat-building-openplc-10284818?pid=1310418069
Quote 0 0