• Post Reply Bookmark Topic Watch Topic
  • New Topic

Flooring VS Ceiling  RSS feed

 
Bhaskar Selvaraju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one please tell me what is the differense between flooring and celing with the help of some examples ???
Thanking you in anticipation.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!