필터 지우기
필터 지우기

About isinteger command(confusion)

조회 수: 15 (최근 30일)
C Zeng
C Zeng 2013년 3월 14일
편집: per isakson 2015년 1월 16일
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?

채택된 답변

per isakson
per isakson 2013년 3월 14일
편집: per isakson 2015년 1월 16일
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
Jan
Jan 2013년 3월 16일
The problem is to decide, if 1e17 is an integer or not: Because a double can store 16 digits only, this number does not contain any information about the fractional part for reasons of the precision. bitand() detect this and throw an error, while 1e17==round(1e17) replies true, because cropping the fractional part does not influence value.
C Zeng
C Zeng 2013년 3월 17일
Thanks, I understand it. It is good to know.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2013년 3월 16일
Joining the rounding with the checks for overflows:
function isf = isflint(m)
isf = (abs(m) <= bitmax && m == floor(m));
  댓글 수: 1
per isakson
per isakson 2013년 3월 18일
I'll replace my cryptic isflint.

댓글을 달려면 로그인하십시오.


ChristianW
ChristianW 2013년 3월 14일
doc isinteger
isinteger(int8(3))
  댓글 수: 5
Jan
Jan 2013년 3월 16일
@C Zeng: @(x) x==round(x) is an anonymous function. You find an exhaustive description ion the documentation.
C Zeng
C Zeng 2013년 3월 17일
OK

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by