Originally posted by Cathy Song:
Math.random(-10.8) --> -11
Math.ceil (-10.8) --> -10
Thanks,
Cathy.
Hi Cathy,
First of all, I think you meant
Math.round(-10.8) --> -11 insead of
Math.random(-10.8) --> -11 Now for the definition.
This is how
ceil() is defined in the API,
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer
and this is how
round() is defined:
Returns the closest long to the argument
Take note of the phrase "
that is not less than" for
ceil(). If you apply that in your example, the number(a double value) that is
not less than -10.8 is -10. If you are thinking -11 that number is
less than -10.8
Now, consider the phrase "
closest long" for
round(). And in your example, the closest long value to -10.8 is -11.
The trick that I use to remember how
ceil() works is this.
Ceil is short for ceiling. So what I do is imagine a vertical number line like this:
Now if you want to get the
ceil() of a FP number, say 2.5, all you need to do is
look up as in
look up the ceiling. So looking up the number line you will get the answer 3.0. And if you are given a negative number, say -4.5, you again look up to find the answer -4.0.
You can also use the same trick for
floor() but instead of looking up you look down, as in look down the floor.
Hope this helps.
[ September 27, 2003: Message edited by: Alton Hernandez ]