I can find rnd and trunc. Or is there a way to use those to obtain
the same value?
Thanks,
Reeza
Sorry, but I don't understand what you're asking for. Please give
some examples.
--
Bruce Weaver
bwe...@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."
I haven't tested the following, but I think that they are
basically correct:
* y=floor(x).
IF (x >= 0) y = TRUNC(x).
IF (x < 0) y = -TRUNC(-x).
* z=ceil(x).
IF (x >= 0) z = -TRUNC(x).
if (x < 0) z = TRUNC(x).
--
"A computer is a state machine.
Threads are for people who cant [sic] program state machines."
--Alan Cox
This prompted me to Google "floor and ceiling functions", and I found
an explanation on the Wikipedia page. This seems to work:
* From http://en.wikipedia.org/wiki/Floor_and_ceiling_functions,
* floor(x) is the largest integer not greater than x
* and ceiling(x) is the smallest integer not less than x .
data list free / x (f5.2).
begin data
0.49 1.50 2.51 -0.49 -1.50 -2.51
end data.
* Use absolute values first, then adjust later.
compute #a = abs(x). /* absolute value of X .
compute #f = trunc(#a).
compute #c = #f + 1.
do if (x GE 0).
- compute floor = #f.
- compute ceiling = #c .
else.
- compute floor = #c * -1 .
- compute ceiling = #f * - 1 .
end if.
format floor ceiling (f5.2).
list.
OUTPUT:
x floor ceiling
.49 .00 1.00
1.50 1.00 2.00
2.51 2.00 3.00
-.49 -1.00 .00
-1.50 -2.00 -1.00
-2.51 -3.00 -2.00
Generally, if x>=0
Floor is round down
Ceiling is round up
If the number is an integer, leave it alone
This is kind of what I need Bruce...the problem is when the value is a
whole number.
ie trunc(x)=x...though I suppose I can just test for that condition
and add it into the logic, but seems a bit cumbersome. Not to whine
or anything but Excel and SAS both have those functions :P
x Floor(x) ceiling(x) round(x)
3.3 3 4 3
4.7 4 5 5
5.2 5 6 5
5.5 5 6 6
7.8 7 8 8
8 8 8 8
Thanks!
Right, I didn't test with whole numbers. This modification works, I
think:
data list free / x (f5.2).
begin data
3.3 4.7 5.2 5.5 8 -0.49 -1.50 -2.51 -6
end data.
* Work with absolute values first, then adjust later.
compute #a = abs(x). /* absolute value of X .
compute #f = trunc(#a).
do if (#a EQ #f) /* X is a whole number .
- compute #c = #f .
else.
- compute #c = #f + 1.
end if.
do if (x GE 0).
- compute floor = #f.
- compute ceiling = #c .
else.
- compute floor = #c * -1 .
- compute ceiling = #f * - 1 .
end if.
format floor ceiling (f5.2).
list.
OUTPUT:
x floor ceiling
3.30 3.00 4.00
4.70 4.00 5.00
5.20 5.00 6.00
5.50 5.00 6.00
8.00 8.00 8.00
-.49 -1.00 .00
-1.50 -2.00 -1.00
-2.51 -3.00 -2.00
-6.00 -6.00 -6.00
--
Bruce Weaver
bwe...@lakeheadu.ca
That does work,
Thanks!
Hey,
floor(x) = rnd(x) - 1*(rnd(x) > x)
ceil(x) = rnd(x) + 1*(rnd(x) < x)
so the following will work
data list free / x.
begin data
3.3 8 -0.22 -7
end data.
compute floor = rnd(x) - 1*(rnd(x) > x).
compute ceil = rnd(x) + 1*(rnd(x) < x).
list.
========================
x floor ceil
3.30 3.00 4.00
8.00 8.00 8.00
-.22 -1.00 .00
-7.00 -7.00 -7.00
Antti Nevanlinna
Nice one.
--
Bruce Weaver
bwe...@lakeheadu.ca