On Feb 11, 2008, at 18:05, Vincent Hennebert wrote:
Hi
Andreas Delmelle wrote:
On Feb 11, 2008, at 11:23, Vincent Hennebert wrote:
+ if ((num >= 0 && num == Math.floor(num))
+ || num == Math.ceil(num)) {
Isn’t that the same as
if (num == Math.floor(num))
Yes, for positive integers. For negative ints, it is 'num ==
Math.ceil(num)'.
Err...
Math.floor(-2.0) == Math.ceil(-2.0) == Math.rint(-2.0) == -2.0
Did you actually check that? In that case, your JVM would not be
compliant, I think...
The API docs say:
floor() -> the largest (closest to positive infinity) double value
that is not greater than the argument and is equal to a mathematical
integer
ceil() -> the smallest (closest to negative infinity) double value
that is not less than the argument and is equal to a mathematical
integer
rint() -> the double value that is closest in value to the argument
and is equal to a mathematical integer.
So,
Math.floor(-2.3) = -3
Math.ceil(-2.3) = -2
Math.rint(-2.3) = -2
rint() has the additional side-effect of returning just the closest
even integer, so:
Math.rint(-3.3) == Math.floor(-3.3) != Math.ceil(-3.3)
As for my proposed alternative, that should obviously be:
Math.abs(num) == Math.floor(Math.abs(num))
Cheers
Andreas