About isinteger command(confusion)

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 日

2 投票

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 件のコメント

C Zeng
C Zeng 2013 年 3 月 15 日
Sort of lost of your code. While I am still confused, I use if floor(z)==z or ceil(z)==z to judge if it is indeed an integer.
per isakson
per isakson 2013 年 3 月 15 日
I cannot find the reference now, however see: isodd: a pedestrian parity checker
Most important is that Matlab use flint, which is described in Floating Points
Yes, there are many ways to decide if a double represents an integer: floor, round, ceil, rem, mod and bitand. The Pedestrian, alias us, argue that bitand behaves better with overflow - I've forgotten. I trust the Pedestrian.
C Zeng
C Zeng 2013 年 3 月 15 日
Thanks, Isakson, will take a look.
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 日

1 投票

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 日

0 投票

doc isinteger
isinteger(int8(3))

5 件のコメント

C Zeng
C Zeng 2013 年 3 月 15 日
Thanks, however, isinteger(int8(2.5)) also returns 1, which I want to avoid.
ChristianW
ChristianW 2013 年 3 月 15 日
isint = @(x) x==round(x)
C Zeng
C Zeng 2013 年 3 月 16 日
yes, I used x==round(x) or floor(x). what do you mean by "isint = @(x)"?
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

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2013 年 3 月 14 日

編集済み:

2015 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by