View previous topic :: View next topic |
Author |
Message |
danm Intermediate
Joined: 29 Jun 2004 Posts: 170 Topics: 73
|
Posted: Wed Apr 23, 2008 8:48 am Post subject: Ceiling and Floor function |
|
|
I can't find a ceiling or floor buildin function in REXX.
Code: |
Example:
data = 1.00000...1 to 2 Ceiling returns 2
data = 1 to 1.9999.... Floor returns 1
I can create the ceiling and floor function with 3 buildin functions:
format(data + word('.5 0',1+datatype(data,'W')),,0) <-- Ceiling
format(data + word('-.5 0',1+datatype(data,'W')),,0) <-- Floor
|
Anyone has a more efficient solution? |
|
Back to top |
|
 |
semigeezer Supermod
Joined: 03 Jan 2003 Posts: 1014 Topics: 13 Location: Atlantis
|
Posted: Wed Apr 23, 2008 11:42 am Post subject: |
|
|
I forget how negative #s should be treated, but I think this will do it:
Code: | parse arg a
say floor(a)
say ceil(a)
return
floor: return arg(1)%1-(sign(arg(1)//1)=-1)
ceil: return -floor(-a) |
_________________ New members are encouraged to read the How To Ask Questions The Smart Way FAQ at http://www.catb.org/~esr/faqs/smart-questions.html. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Apr 23, 2008 2:40 pm Post subject: |
|
|
danm,
you can try this
Code: |
FLOOR: procedure
parse arg F
return TRUNC(F) - (F < 0) * (F <> TRUNC(F))
CEILING: procedure
parse arg C
return TRUNC(C) + (C > 0) * (C <> TRUNC(C))
|
_________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
warp5 Intermediate

Joined: 02 Dec 2002 Posts: 429 Topics: 18 Location: Germany
|
Posted: Thu Apr 24, 2008 1:24 am Post subject: |
|
|
Are you talking about the max and min functions of rexx? |
|
Back to top |
|
 |
danm Intermediate
Joined: 29 Jun 2004 Posts: 170 Topics: 73
|
Posted: Fri Apr 25, 2008 11:20 am Post subject: |
|
|
warp5,
No, I am not talking max or min function. I want to round up or round down to the nearest integer after a calculation. Example: If the calculated value is 2.5, ceiling returns 3, floor returns 2. |
|
Back to top |
|
 |
warp5 Intermediate

Joined: 02 Dec 2002 Posts: 429 Topics: 18 Location: Germany
|
Posted: Mon Apr 28, 2008 1:02 am Post subject: |
|
|
Well, you could easily create such a function by adding or subtracting 0.5 and then truncating the decimal. |
|
Back to top |
|
 |
|
|