R
Raquel
Hello, I would like to know if there are some function in some library
in JAVA to know if one number is odd or even.
Thank you
in JAVA to know if one number is odd or even.
Thank you
Raquel said:Hello, I would like to know if there are some function in some library
in JAVA to know if one number is odd or even.
Thank you
if (myNumber & 0x1 > 0) {
Chris said:int myNumber = 3;
if (myNumber & 0x1 > 0) {
// it's odd
} else {
// it's even
}
This only works with integers, not floats or doubles.
Chris said:int myNumber = 3;
if (myNumber & 0x1 > 0) {
// it's odd
} else {
// it's even
}
This only works with integers, not floats or doubles.
Raquel said:Hello, I would like to know if there are some function in some library
in JAVA to know if one number is odd or even.
Jacob said:No there is not.
But there should have been; Many programs needs this feature,
and a clear logic is suddenly poluted by the introduction of
bit arithmetics and/or modulus operators.
I'd suggest the following standard methods:
boolean Integer.isOdd()
static boolean Integer.isOdd (int value)
boolean Integer.isEven()
static boolean Integer.isEven (int value)
And ditto for Long.
Paul said:Sorry, this is silly. Many arithmetic operations are assumed to be the
province of the programmer, such as adding and subtracting numbers. Should
we have class members called "multiply(x,y)" and "divide(x,y)" to avoid
having to use "*" and "/"? If not, then by the same token why avoid the
modulo operator and its many uses, like determining if an integer is odd or
even?
Jacob said:I am concerned about the readability of the code only.
I am sure every programmer knows what this means:
// Check if value is even
if (value % 2 == 0) ...
Still I always add the comment as shown. Mostly because
I always need to think twice if it is an odd or an even
test (BTW, I am a mathematician, which explains a lot![]()
But as I prefer code that comment itself, I would find
this a lot more expressive:
if (Integer.isEven (value)) ...
Jacob said:But there should have been;
and a clear logic is suddenly poluted by the introduction of
bit arithmetics and/or modulus operators.
boolean Integer.isOdd()
static boolean Integer.isOdd (int value)
boolean Integer.isEven()
static boolean Integer.isEven (int value)
Jacob said:Raquel said:[Is] there some function in some library
in JAVA to know if one number is odd or even.
No there is not.
But there should be; Many programs needs this feature,
and a clear logic is suddenly polluted by the introduction of
bit arithmetics and/or modulus operators.
I'd suggest the following standard methods:
boolean Integer.isOdd()
static boolean Integer.isOdd (int value)
boolean Integer.isEven()
static boolean Integer.isEven (int value)
And ditto for Long.
I am sure every programmer knows what this means:
// Check if value is even
if (value % 2 == 0) ...
Still I always add the comment as shown. Mostly because
I always need to think twice if it is an odd or an even
test (BTW, I am a mathematician, which explains a lot
But as I prefer code that comment itself, I would find
this a lot more expressive:
if (Integer.isEven (value)) ...
Thomas Schodt said:Would both isEven() and isOdd() not be a bit redundant?
You could just use
if (!isEven())
Paul said:Chris wrote:
And floats and doubles must be converted to integers before an attempt is
made to run this (or the other common) algorithm, unless of course one
doesn't care whether the outcome is correct.
Why??
static boolean isEven(double a) {
return a%2 == 0;
}
static boolean isOdd (double a) {
return Math.abs(a%2) == 1;
}
would seem to work for all doubles. Of course the majority of doubles are
neither even nor odd.
[I was going to say the vast majority, but on reflection that's probably
[not
true. All very large Java doubles (>~10^17) or floats (>~10^8) are even,
so the fraction of floats/doubles that are even is probably a
good percentage of all of them ~40% at a guess.
The fraction that are odd should be much smaller.]
Paul said:Tom McGlynn wrote:
/ ...
[ P. Lutus said: ]
Why??
Because not all floats can be reliably converted into integers without
rounding and other issues creating an unexpected result.
static boolean isEven(double a) {
return a%2 == 0;
}
static boolean isOdd (double a) {
return Math.abs(a%2) == 1;
}
would seem to work for all doubles. Of course the majority of doubles are
neither even nor odd.
I think that was my point. One would have to accept that the test applied
only to the truncated (not rounded) integer value.
The tests given by Tom return the correct answer for any double value
(including the special ones) without any conversion to integer.
In fact
it is rather difficult to get the correct answer if you do convert to
integer (special handling then required for values out of the integer
range or for non integral values).
Also note that the remainder operator produces an exact result (there is
no rounding required).
Paul said:No, that is not consistent with what you said above. Either the modulo
operation produces a floating-point result where appropriate, or there is a
conversion to integer before, during or after the operation.
Babu said:Paul said:No, that is not consistent with what you said above. Either the modulo
operation produces a floating-point result where appropriate, or there is
a conversion to integer before, during or after the operation.
JLS Second edition (§15.17.3) doesn't seem to specify a conversion to
integer.
It says :
[cases describing operands involving 0, NaN, Infinity skipped]
"In the remaining cases, where neither an infinity, nor a zero, nor NaN is
involved, the floating-point remainder r from the division of a dividend n
by a divisor d is defined by the mathematical relation r = n - (d.q) where
q is an integer that is negative only if n/d is negative and positive only
if n/d is positive, and whose magnitude is as large as possible without
exceeding the magnitude of the true mathematical quotient of n and d"
Paul said:Mark Thornton wrote:
/ ...
The integer conversion is implicit. Obviously carrying out the specified
modulo operation on two floats will produce a floating-point result that is
neither odd nor even. So there is a conversion, in one sense or another,
even if it means testing the remainder against 1.000.
I meant truncation of the integer portion of a float. Somewhat less
problematical.
No, that is not consistent with what you said above. Either the modulo
operation produces a floating-point result where appropriate, or there is a
conversion to integer before, during or after the operation.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.