posted 16 years ago

Bhaskar

You can think of floor and ceil as rounding the arguments. For floor it rounds the argument down (towards negative infinity) to the next whole integer, so these two lines

System.out.println(Math.floor(1.2));

System.out.println(Math.floor(-1.2));

will print

1

-2

Math.ceil rounds the argument up (towards positive infinity), so the lines

System.out.println(Math.ceil(1.2));

System.out.println(Math.ceil(-1.2));

will print

2

-1

It's fairly obvious how to do the positive numbers but the negative numbers are what throw me sometimes. There are some special cases for when the number is NaN or when it is one of the infinities, check out the API for the exact details.

hope this helps

Dave

[This message has been edited by Dave Vick (edited July 05, 2001).]

You can think of floor and ceil as rounding the arguments. For floor it rounds the argument down (towards negative infinity) to the next whole integer, so these two lines

System.out.println(Math.floor(1.2));

System.out.println(Math.floor(-1.2));

will print

1

-2

Math.ceil rounds the argument up (towards positive infinity), so the lines

System.out.println(Math.ceil(1.2));

System.out.println(Math.ceil(-1.2));

will print

2

-1

It's fairly obvious how to do the positive numbers but the negative numbers are what throw me sometimes. There are some special cases for when the number is NaN or when it is one of the infinities, check out the API for the exact details.

hope this helps

Dave

[This message has been edited by Dave Vick (edited July 05, 2001).]

Dave

posted 16 years ago

I believe that the definitions are -

floor: the first integer number not bigger than the given value

e.g. floor(1.2) -> 1

floor(-1.2) -> -2

ceil: the first integer number not less then the given value

e.g. ceil(1.2) -> 2

ceil(-1.2) -> -1

With this in mind, negative number is no problem.

I know this amounts to the same as what Dave said, just thought I'd clarify a bit.

/Mike

floor: the first integer number not bigger than the given value

e.g. floor(1.2) -> 1

floor(-1.2) -> -2

ceil: the first integer number not less then the given value

e.g. ceil(1.2) -> 2

ceil(-1.2) -> -1

With this in mind, negative number is no problem.

I know this amounts to the same as what Dave said, just thought I'd clarify a bit.

/Mike

posted 16 years ago

Hi Bhaskar,

I prefer using graphics to help me remember:

Ceiling --->

-2 -1 0 +1 +2

Floor <---<br /> Now we can easily see the examples (negative or positive):<br /> floor(1.2) ---> 1.0

floor(-1.2) ---> -2.0

ceil(1.2) ---> 2.0

ceil(-1.2) --> -1.0

A picture is worth a thousand words,

Manfred.

I prefer using graphics to help me remember:

Ceiling --->

-2 -1 0 +1 +2

Floor <---<br /> Now we can easily see the examples (negative or positive):<br /> floor(1.2) ---> 1.0

floor(-1.2) ---> -2.0

ceil(1.2) ---> 2.0

ceil(-1.2) --> -1.0

A picture is worth a thousand words,

Manfred.