# Re: [ferret_users] ceil, floor, and round

```let floor_a = int(a) - (a LT 0 AND int(a) NE a)
let ceil_a  = int(a) + (a GT 0 AND int(a) NE a)
let round_a = int(a-.5+(a GE 0))

Andrew

On Tue, 29 Sep 2009, Ryo Furue wrote:

```
```Hi Peng,

| Does ferret have functions similar to ceil, floor, round in matlab (please
| see their definitions below)?

I don't know the official answer, but I guess Ferret doesn't have those
functions built in.  In case it doesn't, I give some ideas on how to
define them.

| CEIL(X) rounds the elements of X to the nearest integers towards infinity.
| FLOOR(X) rounds the elements of X to the nearest integers towards minus
| infinity.
| ROUND(X) rounds the elements of X to the nearest integers.

You can construct them from the "int()" function, which Ferret does have.

INT is the same as FLOOR for positive arguments but it truncates
toward zero instead of toward minus infinity:

INT(1.4)  == 1
INT(-1.4) == -1

So,

FLOOR(a)
= if (a is a whole number)
then a
else if (a < 0)
then INT(a) - 1
else INT(a)

In Ferret statements,

let int_a = int(a)
let floor_a = if int_a eq a then a else floor_a_helper
let floor_a_helper = if a lt 0 then int_a - 1 else int_a

Likewise,

ROUND(a)
= if (a is a whole number)
then a
else if (a < 0)
then INT(a - 0.5)
else INT(a + 0.5)

These formulae are rather tedious.  There may be better
definitions.

Regards,
Ryo

```
```
```