Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ceiling/floor function

524 views
Skip to first unread message

Reeza

unread,
May 27, 2010, 12:51:01 PM5/27/10
to
Are there ceiling or floor functions in SPSS?

I can find rnd and trunc. Or is there a way to use those to obtain
the same value?

Thanks,
Reeza

Bruce Weaver

unread,
May 27, 2010, 3:00:03 PM5/27/10
to


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

Ben Pfaff

unread,
May 27, 2010, 3:21:45 PM5/27/10
to
Reeza <fkhu...@hotmail.com> writes:

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

Bruce Weaver

unread,
May 27, 2010, 3:54:09 PM5/27/10
to
On May 27, 3:21 pm, b...@cs.stanford.edu (Ben Pfaff) wrote:

> Reeza <fkhurs...@hotmail.com> writes:
> > Are there ceiling or floor functions in SPSS?
>
> > I can find rnd and trunc.  Or is there a way to use those to obtain
> > the same value?
>
> 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).


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

Reeza

unread,
May 27, 2010, 4:26:36 PM5/27/10
to
On May 27, 12:54 pm, Bruce Weaver <bwea...@lakeheadu.ca> wrote:
> On May 27, 3:21 pm, b...@cs.stanford.edu (Ben Pfaff) wrote:
>
>
>
>
>
> > Reeza <fkhurs...@hotmail.com> writes:
> > > Are there ceiling or floor functions in SPSS?
>
> > > I can find rnd and trunc.  Or is there a way to use those to obtain
> > > the same value?
>
> > 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).
>
> This prompted me to Google "floor and ceiling functions", and I found
> an explanation on the Wikipedia page.  This seems to work:
>
> * Fromhttp://en.wikipedia.org/wiki/Floor_and_ceiling_functions,
> bwea...@lakeheadu.cahttp://sites.google.com/a/lakeheadu.ca/bweaver/Home
> "When all else fails, RTFM."- Hide quoted text -
>
> - Show quoted text -

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!

Bruce Weaver

unread,
May 27, 2010, 4:57:13 PM5/27/10
to

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

Reeza

unread,
May 27, 2010, 5:37:13 PM5/27/10
to

That does work,
Thanks!

Antti Nevanlinna

unread,
May 28, 2010, 5:13:55 AM5/28/10
to

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

Bruce Weaver

unread,
May 28, 2010, 7:25:24 AM5/28/10
to
On May 28, 5:13 am, Antti Nevanlinna <antti.nevanli...@helsinki.fi>
wrote:


Nice one.

--
Bruce Weaver
bwe...@lakeheadu.ca

0 new messages