Test for integer value
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Corey Washburn
le 28 Sep 2018
Modifié(e) : Bruno Luong
le 29 Sep 2018
I am trying to make a battleship styled game, one of the parts involve getting a set of user input 'firing coordinates', which I then need to test to make sure it is an integer value 1 through 10, what function can I use to test that it is an integer?
*edit: I am looking for a logical operator or else condition that can specify a non integer input.
0 commentaires
Réponse acceptée
James Tursa
le 28 Sep 2018
Modifié(e) : James Tursa
le 28 Sep 2018
See the following:
doc fix
doc ceil
doc floor
doc round
E.g., for a scalar x
if( round(x) == x )
% x is an integer
2 commentaires
Plus de réponses (2)
Bruno Luong
le 29 Sep 2018
Modifié(e) : Bruno Luong
le 29 Sep 2018
Perhaps no better no worse than James's solution just different
iswholenumber = mod(x,1)==0
0 commentaires
Bruno Luong
le 29 Sep 2018
Modifié(e) : Bruno Luong
le 29 Sep 2018
according to my test slightly faster, but need to assume the smaller range of x of be working:
x == int32(x)
0 commentaires
Voir également
Catégories
En savoir plus sur Antennas, Microphones, and Sonar Transducers dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!