Any function which will return n.x as (n + 1)

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
 

Cringer

ProgressTalk.com Moderator
Staff member
I don't think there is such a function, but it shouldn't be too hard to roll your own.
 

Cringer

ProgressTalk.com Moderator
Staff member
What is your logic? There's a few solutions around on here and it's always interesting to see how people have solved the problem.
 

Cringer

ProgressTalk.com Moderator
Staff member
That won't work for negative numbers. A simpler solution might be
Code:
def var vd as dec init 25.44.
disp int(trunc(vd,0) + 1).
Obviously this won't work for negative numbers either, but it's a starting point.
 
That won't work for negative numbers. A simpler solution might be
Code:
def var vd as dec init 25.44.
disp int(trunc(vd,0) + 1).
Obviously this won't work for negative numbers either, but it's a starting point.

If init
That won't work for negative numbers. A simpler solution might be
Code:
def var vd as dec init 25.44.
disp int(trunc(vd,0) + 1).
Obviously this won't work for negative numbers either, but it's a starting point.

I don't if I am missing something, if I give init value -25.44, its returning -24, which is expected.
 

TomBascom

Curmudgeon
Didn't we go over this a few weeks ago?

ROUND ( de + 0.5, 0 )

That will fail if "de" just happens to be an integer.

The original poster is apparently looking for a "ceiling" function.

You have to first test that the argument is not already an integer. If it is you are done. If it is not you can add .5 and round or you can truncate and add 1. As Cringer points out you also have to decide what to do about negative numbers.
 

Stefan

Well-Known Member
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:
  1. when x is greater than 0 and less than 5, 1 should be added - what should happen when x >= 5 and x <= 9?
  2. how many digits can x be?
If it were ceiling then 5 thru 9 would also round up.
 

Pavan Yadav

Member
For +ve integer we can add up 0.49 instead of .50 and it will work with the solution as mentioned by Stefan and will work for all +ve integers:
ROUND ( de + 0.49, 0 )

And for -ve integers just add 0.5
ROUND ( de + 0.5, 0 )

But yes, as mentioned by Tom, you have to evaluate separately for -ve and +ve integers.
 
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:
  1. when x is greater than 0 and less than 5, 1 should be added - what should happen when x >= 5 and x <= 9?
  2. how many digits can x be?
If it were ceiling then 5 thru 9 would also round up.


Hi Stefan, Thanks for the solution..Yes it was incomplete requirement. But I was looking at that particular situation.
Actual is like - 20.01 to 20.99, it should return 21. Negative numbers I didnt mention, as in my requirement it will never occured.
 

TomBascom

Curmudgeon
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.
 
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.

Yes - "to truncate and add 1 if not already an integer" is perfect one...!!!
 
Top