Test for integer value

8 visualizaciones (últimos 30 días)
Corey Washburn
Corey Washburn el 28 de Sept. de 2018
Editada: Bruno Luong el 29 de Sept. de 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.

Respuesta aceptada

James Tursa
James Tursa el 28 de Sept. de 2018
Editada: James Tursa el 28 de Sept. de 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 comentarios
Corey Washburn
Corey Washburn el 28 de Sept. de 2018
I am not looking for a way to make a decimal value into an integer (round 4.3 to 4) I am looking for a logical operator that is false if the input is not an integer value.
James Tursa
James Tursa el 29 de Sept. de 2018
That's exactly what my code does ...

Iniciar sesión para comentar.

Más respuestas (2)

Bruno Luong
Bruno Luong el 29 de Sept. de 2018
Editada: Bruno Luong el 29 de Sept. de 2018
Perhaps no better no worse than James's solution just different
iswholenumber = mod(x,1)==0

Bruno Luong
Bruno Luong el 29 de Sept. de 2018
Editada: Bruno Luong el 29 de Sept. de 2018
according to my test slightly faster, but need to assume the smaller range of x of be working:
x == int32(x)

Categorías

Más información sobre Antennas, Microphones, and Sonar Transducers en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by