Thread Subject: Even/odd

Subject: Even/odd

From: Edward Seger

Date: 23 Aug, 2007 14:43:43

Message: 1 of 7

How about even and odd functions built-in:

bool x;

x = even(num);
x = odd(num);

Subject: Even/odd

From: Steve Amphlett

Date: 23 Aug, 2007 15:00:21

Message: 2 of 7

"Edward Seger" <edward.h.seger@saic.com> wrote in message
<fak6ev$t2k$1@fred.mathworks.com>...
> How about even and odd functions built-in:
>
> bool x;
>
> x = even(num);
> x = odd(num);

So what's wrong with <mod> ?

mod(x,2)

0 if even, 1 if odd


Subject: Even/odd

From: us

Date: 23 Aug, 2007 15:21:31

Message: 3 of 7

Steve Amphlett:
<SNIP down to typical even/odd algorithm...

> mod(x,2)
> 0 if even, 1 if odd

% now, since even/odd should be integers,
% this may fail, eg,
     n=-4:4;
     m=n;
     even=mod(m,2)==0;
     odd=~even;
     [m;even;odd]
% if the user is not careful
     m=pi*n;
     even=mod(m,2)==0;
     odd=~even;
     [m;even;odd]
% a better way may be this
     odd=bitand(abs(n),1);
     even=~odd;
     [n;even;odd]
% ...which would give an error early on
     odd=bitand(abs(m),1);

just a thought
us
     

Subject: Even/odd

From: Amber York

Date: 19 Feb, 2008 21:26:02

Message: 4 of 7

How about

function [ise] = iseven(x);
%works for sccalar and vector
ise = roundn(x/2,0) == x/2;

Subject: Even/odd

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 19 Feb, 2008 22:52:37

Message: 5 of 7

In article <fpfhha$86m$1@fred.mathworks.com>,
Amber York <yorksea@gmail.com> wrote:
>How about

>function [ise] = iseven(x);
>%works for sccalar and vector
>ise = roundn(x/2,0) == x/2;

roundn is only in the Mapping toolbox.

Please check the function for a full range of input,
such as iseven([-inf -2 -1 0 1 1.49 1.5 1.51 1.99 2 inf nan])

I do not have roundn to check with, but I suspect you will
find that your iseven function will tell you that -inf and inf
are even.

--
Q: Why did the chicken cross the Mobius strip?

A: There were manifold reasons.

Subject: Even/odd

From: Andy Robb

Date: 16 Aug, 2008 01:42:01

Message: 6 of 7

> % a better way may be this
> odd=bitand(abs(n),1);

Is the domain of bitand() limited to 32-bit integers? At
least mod(x,2) covers the range of whole numbers up to 53
bits. Sure, the returned matrix ought to be sparse, omitting
values where it is not certain if the number was even or odd
(e.g. either the fraction is not zero or the value is too
large to be odd).

Subject: Even/odd

From: us

Date: 16 Aug, 2008 11:35:24

Message: 7 of 7

"Andy Robb"
<SNIP bits and pieces...

> > % a better way may be this
> > =A0 =A0 =A0odd=3Dbitand(abs(n),1);
>
> Is the domain of bitand() limited to 32-bit integers? At
> least mod(x,2) covers the range of whole numbers up to 53
> bits...

so does the bitxxx family of stock functions...
the limit is

     help bitmax;

% the advantage of the BITs...
% they complain if something goes wrong
% and dont let you believe your result is correct as with REM/MOD

     bitand(bitmax,1)
% ans =3D
% these will error out
% bitand(pi,1)
% bitand(bitmax+1,1);
% whereas these don't care to warn you
     b=3Dbitmax+(1:5); % valid doubles
     mod(b,2)
% ans =3D 0 0 0 0 0 %!
     rem(b,2)
% ans =3D 0 0 0 0 0 %!

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
plot Anthony 22 May, 2008 18:32:10
iseven even odd Amber York 19 Feb, 2008 16:30:48
integers us 23 Aug, 2007 11:25:21
bitand us 23 Aug, 2007 11:25:21
code us 23 Aug, 2007 11:25:21
odd us 23 Aug, 2007 11:25:21
even us 23 Aug, 2007 11:25:21
mod us 23 Aug, 2007 11:22:36
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com