iam_msams
Hi,
I wanted to ceil/floor a number and couldn't find any predefined POUs available. So i tried to create this (ceil):
Annotation 2020-05-11 154100.png
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
thiagoralves
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
iam_msams
thank you for your response,
I will try out both the OSCAT library and your algorithm.

i don't feel quite confident with my algorithm, considering its processing speed and pitfalls, but it works out well in c++/python.
*but, by the way, I found out that 1.5 -> REAL_TO_DINT -> 2; while other numbers with a fraction of .5 will result in their own base number. Then I was expecting the conversion to return the base number no matter what the fraction is, and that is where my fault is. The conversion actually round the value, but somehow a .5 is floored not ceiled except for base 1. however, normal rules still apply to fractions below .5 (floored) and above .5 (ceiled).
Quote 0 0