Questions about Fractions and integers?
6 ビュー (過去 30 日間)
古いコメントを表示
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
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
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
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.
0 件のコメント
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!