image thumbnail

Functions that checks if a number is odd or even

version 1.1.0.0 (2.92 KB) by Ulf Carlberg
Determine if a number is odd or even. Works with floats and integers. Error checking.

1.4K Downloads

Updated 29 May 2011

View License

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 27-MAY-2011. Updated 29-MAY-2011.

BSD 2-clause license, see the file "license.txt".

Cite As

Ulf Carlberg (2021). Functions that checks if a number is odd or even (https://www.mathworks.com/matlabcentral/fileexchange/31593-functions-that-checks-if-a-number-is-odd-or-even), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!