Functions that checks if a number is odd or even.
(This is a little bit more complicated than using "mod(x, 2)" if you want error handling for certain cases.)
The function can handle double, single, and integer classes, as well as arrays of these.
* An error is raised if a floating point number is too large so that it is not possible to determine if it is even or odd.
* An error is raised if a floating point number contains a fraction or is a special number (Inf, Inf, or NaN).
* The function can handle integers, also 64 bits. Care is taken so that the "weird number" in two complement form is handeled appropriately. Many other codes fail when taken the absolute value of the weird number integers. Note that int8(128) = 128 in Matlab, but abs(int8(128)) = 127!!!
By Ulf Carlberg 27MAY2011. Updated 29MAY2011.
BSD 2clause license, see the file "license.txt".
1.1.0.0  Bugfixes and updates:

Create scripts with code, output, and formatted text in a single executable document.
Christoforos Rekatsinas (view profile)
Very useful ty
belal majali (view profile)
thank you
John D'Errico (view profile)
Think of the comments on your files as a peer review of your code. In the end, your code is now better than it was when you first submitted it.
Ulf Carlberg (view profile)
A found a few bugs and improved a few things from John D'Errico's suggestions. The new version must be approved first, within a few hours I hope.
Ulf Carlberg (view profile)
Hi Darren,
The submission in your first link might be an alternative depending on what you want, it's a little different from my submission. The second link does not check the input and you might as well use mod(x, 2) in my opinion.
Ulf Carlberg (view profile)
Hi Oleg,
It can be more complicated if you want errors to be raised on input where it is not possible to determine if a number is odd or even isodd(1e30) for example.
Darren Rowland (view profile)
Hi Ulf,
I notice that there are a couple of other FEX files called "isodd".
http://www.mathworks.com/matlabcentral/fileexchange/24278
http://www.mathworks.com/matlabcentral/fileexchange/2006
Do you know of any differences between these files and yours?
Darren
Ulf Carlberg (view profile)
Answer to John D'Errico.
John D'Errico writes:
It looks like the author did not like my last review of this file, so deleted
the submission and simply uploaded the exact same files. Actually, my last
review was not a poor one, and I would have raised the rating if the author
repaired the flaws I pointed out.
Ulf Carlberg answers:
I did not do that. I only uploaded the zipfile one time, and I never saw your
first review. I do not know why the files where uploaded twice and why your
first comments disappeared. My guess is that it was done by the staff at Matlab
file central or something like that, maybe they forgot something and wanted to
update. I don't know this, I am only speculating.
John D'Errico writes:
This code lacks an H1 line. That simple line enables lookfor, a valuable tool in
MATLAB. It is the tool that helps you to find something in MATLAB using a
search. An H1 line is a single descriptive line of test that includes keywords
that will help someone find your code. Look at the help for any MATLAB function,
and you will see that line.
Ulf Carlberg answers:
I was not aware of that, I haven't used lookfor myself. I have changed the
functions now, and the help section is now similar to the ones in the builtin
functions.
John D'Errico writes:
More importantly, this code fails when inputs that are not integers are
supplied. I see that the one thing the author did change was to add a single
line to the description online, saying that the code does fail under the
circumstance when a noninteger is supplied, or an inf or nan. Sadly, the author
did not bother to add that information to the help in the code itself. I don't
have a problem with the code failing under those circumstances, but it should be
friendly. Good code should tell you things like that in the documentation.
Ulf Carlberg answers:
Ok, I added more description in the help section. The code doesn't really "fail"
in my opinion, it raises errors on inputs that make it inpossible to determine
if a number is odd or even.
John D'Errico writes:
The online description (not the help itself) now tells you that an error will be
generated, but this code does not do any testing for errors. Instead, the author
allows bitget to fail and generate the error, a less friendly, somewhat more
confusing action, since then the user must decide what actually caused the
problem.
Ulf Carlberg answers:
Ok, I added some explicit error checking to give more meaningful error messages
to the user.
John D'Errico writes:
Note that the author CLAIMS that error checking is done. But in fact, very
little error checking is actually done by this code. For example, NO checking is
done for a nan, or an inf or an integer itself. In fact, the only explicit error
check in the code is if a number of class single is greater than 2^24. Of
course, this completely misses inf.
help isinf
help isnan
Ulf Carlberg answers:
I am aware of Inf, Inf, and NaN, and the code did generate an error for these
cases (from bitget). The code did not miss the single(Inf) case, because it
checked if the absolute value of the input was larger or equal to 2^24.
There IS one interesting case in the code, where the author tests to see if
(2^63) stored as an int64 number is odd or even. An int64 can represent all
integers in the interval [2^63,2^631]. You can convince yourself of that by
the simple artifice of trying to create the number 2^63 as a number of class
int64.
>> X = int64(2)^62
X =
4611686018427387904
>> (X)*2
ans =
9223372036854775808
>> (X)*2
ans =
9223372036854775807
See that MATLAB will indeed store 2^63, but not 2^63. This is reflected by
intmax:
>> intmax('int64')
ans =
9223372036854775807
Now, in fact, mod CAN handle the problem of determining if these numbers are odd
or even, and do so very nicely. No clumsy exception is needed comparing the
value of x to 2^63, and there is no need to convert x to a uint64 form from
int64.
Ulf Carlberg answers:
No, mod does not work with 64 bit numbers on my version of Matlab (R2009a). If I
type the following I get an error message:
>> mod(int64(123), 2)
??? Undefined function or method 'mod' for input arguments of type 'int64'.
However, bitget works fine for uint64, thats why the code is implemented the way
it is. I do not agree that is a such a clumsy exception to include, and it will
allow the code to work on older Matlab versions without full 64bit support.
John D'Errico writes:
>> X = intmax('int64') 1
X =
9223372036854775808
>> mod(X+int64([0 1 2 3 4]),2)
ans =
0 1 0 1 0
I am willing to change my ratings when code is improved. And on the file
exchange, when a rating is updated, the old rating is superceded. So there is no
need for the author to delete this submission and upload it again.
Ulf Carlberg answers:
Again, I never did that in the first place. Thanks, for you other suggestions
though.
John D'Errico (view profile)
It looks like the author did not like my last review of this file, so deleted the submission and simply uploaded the exact same files. Actually, my last review was not a poor one, and I would have raised the rating if the author repaired the flaws I pointed out.
This code lacks an H1 line. That simple line enables lookfor, a valuable tool in MATLAB. It is the tool that helps you to find something in MATLAB using a search. An H1 line is a single descriptive line of test that includes keywords that will help someone find your code. Look at the help for any MATLAB function, and you will see that line.
More importantly, this code fails when inputs that are not integers are supplied. I see that the one thing the author did change was to add a single line to the description online, saying that the code does fail under the circumstance when a noninteger is supplied, or an inf or nan. Sadly, the author did not bother to add that information to the help in the code itself. I don't have a problem with the code failing under those circumstances, but it should be friendly. Good code should tell you things like that in the documentation.
The online description (not the help itself) now tells you that an error will be generated, but this code does not do any testing for errors. Instead, the author allows bitget to fail and generate the error, a less friendly, somewhat more confusing action, since then the user must decide what actually caused the problem.
Note that the author CLAIMS that error checking is done. But in fact, very little error checking is actually done by this code. For example, NO checking is done for a nan, or an inf or an integer itself. In fact, the only explicit error check in the code is if a number of class single is greater than 2^24. Of course, this completely misses inf.
help isinf
help isnan
There IS one interesting case in the code, where the author tests to see if (2^63) stored as an int64 number is odd or even. An int64 can represent all integers in the interval [2^63,2^631]. You can convince yourself of that by the simple artifice of trying to create the number 2^63 as a number of class int64.
>> X = int64(2)^62
X =
4611686018427387904
>> (X)*2
ans =
9223372036854775808
>> (X)*2
ans =
9223372036854775807
See that MATLAB will indeed store 2^63, but not 2^63. This is reflected by intmax:
>> intmax('int64')
ans =
9223372036854775807
Now, in fact, mod CAN handle the problem of determining if these numbers are odd or even, and do so very nicely. No clumsy exception is needed comparing the value of x to 2^63, and there is no need to convert x to a uint64 form from int64.
>> X = intmax('int64') 1
X =
9223372036854775808
>> mod(X+int64([0 1 2 3 4]),2)
ans =
0 1 0 1 0
I am willing to change my ratings when code is improved. And on the file exchange, when a rating is updated, the old rating is superceded. So there is no need for the author to delete this submission and upload it again.
Oleg Komarov (view profile)
You say it can be a lot more complicated than mod(x,2), an example?