About isinteger command(confusion)

8 views (last 30 days)
C Zeng
C Zeng on 14 Mar 2013
Edited: per isakson on 16 Jan 2015
Hi, by the command of isinteger I can check if it is an integer, however, when defined at first, Matlab assume it is double precision right? So even a=3, isinteger(a) returns 0.
How to solve this problem?

Accepted Answer

per isakson
per isakson on 14 Mar 2013
Edited: per isakson on 16 Jan 2015
Matlab represents integers in differnt ways:
  • int8, int16, etc. see Christians answer
  • with a special use of double, which is called flint. See Floating Points
I use this function to test for flint
function isf = isflint( m )
% floating double only
try
bitand( abs( m ), 1 );
isf = true;
catch me
isf = false;
end
end
I picked up the idea from a contribution by the "Pedestrian" in the FEX.
(I stripped off comments and error handling.)
  5 Comments
C Zeng
C Zeng on 17 Mar 2013
Thanks, I understand it. It is good to know.

Sign in to comment.

More Answers (2)

Jan
Jan on 16 Mar 2013
Joining the rounding with the checks for overflows:
function isf = isflint(m)
isf = (abs(m) <= bitmax && m == floor(m));
  1 Comment
per isakson
per isakson on 18 Mar 2013
I'll replace my cryptic isflint.

Sign in to comment.


ChristianW
ChristianW on 14 Mar 2013
doc isinteger
isinteger(int8(3))
  5 Comments

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!