Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
checking for an integer

Subject: checking for an integer

From: don terry

Date: 3 Feb, 2008 18:10:04

Message: 1 of 9

hi, does anyone know how to check if a value is an integer
using an if statement?
cheers
andy

Subject: checking for an integer

From: Rajiv Narayan

Date: 4 Feb, 2008 22:11:39

Message: 2 of 9

"don terry" <goo_lav@hotmail.com> wrote in message
<fo501r$gah$1@fred.mathworks.com>...
> hi, does anyone know how to check if a value is an integer
> using an if statement?
> cheers
> andy

help isinteger

Subject: checking for an integer

From: Rajiv Narayan

Date: 4 Feb, 2008 22:12:05

Message: 3 of 9

"don terry" <goo_lav@hotmail.com> wrote in message
<fo501r$gah$1@fred.mathworks.com>...
> hi, does anyone know how to check if a value is an integer
> using an if statement?
> cheers
> andy

help isinteger

Subject: checking for an integer

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

Date: 4 Feb, 2008 22:22:23

Message: 4 of 9

In article <fo82jl$k45$1@fred.mathworks.com>,
Rajiv Narayan <askrajiv.nospam@gmail.com> wrote:
>"don terry" <goo_lav@hotmail.com> wrote in message
><fo501r$gah$1@fred.mathworks.com>...
>> hi, does anyone know how to check if a value is an integer
>> using an if statement?

>help isinteger

isinteger checks for an integer data type, not for an integer value.


--
  "There are some ideas so wrong that only a very intelligent person
  could believe in them." -- George Orwell

Subject: checking for an integer

From: Paul

Date: 4 Feb, 2008 22:30:20

Message: 5 of 9

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fo836v$6ri$1@canopus.cc.umanitoba.ca>...
> In article <fo82jl$k45$1@fred.mathworks.com>,
> Rajiv Narayan <askrajiv.nospam@gmail.com> wrote:
> >"don terry" <goo_lav@hotmail.com> wrote in message
> ><fo501r$gah$1@fred.mathworks.com>...
> >> hi, does anyone know how to check if a value is an integer
> >> using an if statement?
>
> >help isinteger
>
> isinteger checks for an integer data type, not for an
integer value.
>
>
> --
> "There are some ideas so wrong that only a very
intelligent person
> could believe in them." --
George Orwell


you can probably just check the remainder after division by one.

if rem(x,1) == 0 ; % then its an integer

Subject: checking for an integer

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

Date: 4 Feb, 2008 22:40:36

Message: 6 of 9

In article <fo83ls$716$1@fred.mathworks.com>,
Paul <par@ceri.memphis.edu> wrote:

>you can probably just check the remainder after division by one.

>if rem(x,1) == 0 ; % then its an integer

Effectively the same as part of Bruno's more complete solution
early on, which tests the imaginary part as well as testing that
mod(x,1) == 0

--
  "I will speculate that [...] applications [...] could actually see a
  performance boost for most users by going dual-core [...] because it
  is running the adware and spyware that [...] are otherwise slowing
  down the single CPU that user has today" -- Herb Sutter

Subject: checking for an integer

From: Rajiv Narayan

Date: 4 Feb, 2008 23:20:04

Message: 7 of 9

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fo836v$6ri$1@canopus.cc.umanitoba.ca>...

>
> isinteger checks for an integer data type, not for an
integer value.

Oops, my bad. Another possibility:

isequal(fix(x),x)

Subject: checking for an integer

From: Steven Lord

Date: 5 Feb, 2008 00:36:45

Message: 8 of 9


"Rajiv Narayan" <askrajiv.nospam@gmail.com> wrote in message
news:fo86j4$m7p$1@fred.mathworks.com...
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> message <fo836v$6ri$1@canopus.cc.umanitoba.ca>...
>
>>
>> isinteger checks for an integer data type, not for an
> integer value.
>
> Oops, my bad. Another possibility:
>
> isequal(fix(x),x)

That depends. Do you want Inf to count as an integer value? If x and a
both contain the value Inf:

rem(x, 1) == 0 returns false
isequal(fix(x), x) returns true
isa(a,'integer') || (imag(a)==0 && mod(a,1)==0) (Bruno's solution) also
returns false

Don't you just love edge cases? :)

--
Steve Lord
slord@mathworks.com

Subject: checking for an integer

From: nor ki

Date: 5 Feb, 2008 08:55:02

Message: 9 of 9

"don terry" <goo_lav@hotmail.com> wrote in message
<fo501r$gah$1@fred.mathworks.com>...
> hi, does anyone know how to check if a value is an integer
> using an if statement?
> cheers
> andy

why not use

ceil(x) == floor(x) ?

works for complex numbers with integer valued real and
imaginary part as well

kinor

Tags for this Thread

No tags are associated with this thread.

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.

Contact us