필터 지우기
필터 지우기

Questions about Fractions and integers?

조회 수: 8 (최근 30일)
Portgas Ace
Portgas Ace 2012년 9월 20일
James deleted this, so I (Matt Fig) and rewriting it from memory.
How can I make a way to distinguish between integers and fractions? For example, the output will say fraction if the input is a fraction and will say integer if the input is an integer.
  댓글 수: 5
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 20일
James, where is the question and the answer? when you post a question, the answer is useful for all members of this forum
Matt Fig
Matt Fig 2012년 9월 21일
OP:
How can I make a way to distinguish between integers and fractions? For example, the output will say fraction if the input is a fraction and will say integer if the input is an integer.

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

답변 (3개)

José-Luis
José-Luis 2012년 9월 20일
편집: José-Luis 2012년 9월 20일
If integer, the following will return true:
floor(your_num) == your_num;
Otherwise, if the number has a fractional part, it will return false.
If you are talking about data type, Matlab is not a strongly typed language, and by default everything is a double. You can however define a number to have integer type
doc int32
and other flavors of integers.

Matt Fig
Matt Fig 2012년 9월 20일
A = 9; % Also try with A = 7/8;
if floor(A)==A
disp('Integer')
else
disp('fraction')
end

Daniel Shub
Daniel Shub 2012년 9월 20일
You can test if a numeric input is an "integer" with validateattributes ...
function TF = mytest(x)
TF = false;
try
validateattributes(x, {'numeric'}, {'integer'});
disp('integer')
catch
disp('fraction')
TF = true;
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by