Effacer les filtres
Effacer les filtres

Questions about Fractions and integers?

8 vues (au cours des 30 derniers jours)
Portgas Ace
Portgas Ace le 20 Sep 2012
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 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 20 Sep 2012
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 le 21 Sep 2012
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.

Connectez-vous pour commenter.

Réponses (3)

José-Luis
José-Luis le 20 Sep 2012
Modifié(e) : José-Luis le 20 Sep 2012
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 le 20 Sep 2012
A = 9; % Also try with A = 7/8;
if floor(A)==A
disp('Integer')
else
disp('fraction')
end
  1 commentaire
Malcolm Lidierth
Malcolm Lidierth le 21 Sep 2012
How about A = NaN ?

Connectez-vous pour commenter.


Daniel Shub
Daniel Shub le 20 Sep 2012
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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by