krishangopalmca
Member
Any function which will return n.x as (n + 1), where x is lesser than 5 and greater than 0.
Like 25.2, should return 26.
Thanks,
Krishan Gopal
Like 25.2, should return 26.
Thanks,
Krishan Gopal
That won't work for negative numbers. A simpler solution might be
Obviously this won't work for negative numbers either, but it's a starting point.Code:def var vd as dec init 25.44. disp int(trunc(vd,0) + 1).
That won't work for negative numbers. A simpler solution might be
Obviously this won't work for negative numbers either, but it's a starting point.Code:def var vd as dec init 25.44. disp int(trunc(vd,0) + 1).
ROUND ( de + 0.5, 0 )
FUNCTION ceiling RETURNS DECIMAL (
i_de AS DECIMAL
):
RETURN
IF i_de = TRUNCATE( i_de, 0 ) THEN
i_de
ELSE
ROUND( i_de + 0.5, 0 ).
END FUNCTION.
Hmm... fishy. For ceiling the integer should indeed have been compensated:
Code:FUNCTION ceiling RETURNS DECIMAL ( i_de AS DECIMAL ): RETURN IF i_de = TRUNCATE( i_de, 0 ) THEN i_de ELSE ROUND( i_de + 0.5, 0 ). END FUNCTION.
I can however make neither heads nor tails of the requirement:
If it were ceiling then 5 thru 9 would also round up.
- when x is greater than 0 and less than 5, 1 should be added - what should happen when x >= 5 and x <= 9?
- how many digits can x be?
You cannot write a correct ceiling function using logic that involves variations on "add .49 and round". There is always just a little more precision -- what if the argument is 20.00001? Then the "add .49 and round" fails... (I don't care if you think you will only ever have 2 decimal digits -- if you really believe that then I have a really fast RAID 6 "filer" to sell you...)
The only reliable way to do it is to truncate and add 1 if not already an integer.
Any time you see a rounding "solution" that uses ".49" and the ilk you know that it will fail on the corner cases.
So many people get this wrong that it scares me. Progress really ought to add a ceiling() function.