Borrar filtros
Borrar filtros

How can I test if an input value is an integer?

157 visualizaciones (últimos 30 días)
Hung Chu
Hung Chu el 21 de Feb. de 2017
Respondida: Weimin Zhang el 23 de Feb. de 2023
I want to know how to test whether an input value is an integer or not. I have tried using the function isinteger, but I obtain, for example, isinteger(3) = 0. Apparently, any constant is double-precision by Matlab default, and is therefore not recognized as an integer. Can anyone tell me what function I'm supposed to use?
  2 comentarios
Torsten
Torsten el 21 de Feb. de 2017
Please explain in further detail the reason why you want to perform the test.
Best wishes
Torsten.
Hung Chu
Hung Chu el 21 de Feb. de 2017
For the function I'm trying to write, if any input of the function is not an integer, the function returns an error.

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 21 de Feb. de 2017
E.g.,
>> isaninteger = @(x)isfinite(x) & x==floor(x)
isaninteger =
@(x)isfinite(x) & x==floor(x)
>> isaninteger(3)
ans =
1
>> isaninteger(3.1)
ans =
0
>> isaninteger(inf)
ans =
0
>> isaninteger(nan)
ans =
0
>> isaninteger(uint8(4))
ans =
1
>> isaninteger([-inf -1.2 0 1.3 5 inf nan])
ans =
0 0 1 0 1 0 0
  3 comentarios
tqml
tqml el 30 de Oct. de 2020
I think using equal with floating point values might be error prone.
> mod((1:5)+eps,1)==0
ans =
1×5 logical array
0 1 1 1 1
For double value eps should be aroung 1e-15, so small rounding errors could mess up the check.
Therefore it might be helpful to introduce an additional parameter to control the tolerance and only check if the result is CLOSE to zero (and not equal)
function [b] = is_integer_value(x, tolerance)
%IS_INTEGER_VALUE Returns true if the value is an integer
% Checks if x modulo 1 is close to 0.
if nargin < 2
tolerance = eps;
end
b = ( mod(x,1) < tolerance ) ;
end
Steven Lord
Steven Lord el 30 de Oct. de 2020
When you call the eps function with no inputs, it returns the distance from 1 to the next largest number.
The distance from (for example) 2 to the next largest number is greater than eps. When you call eps with an input, it returns the distances from the elements of the input to the next largest numbers.
>> x = 1:5;
>> y = x + eps;
>> y > x
ans =
1×5 logical array
1 0 0 0 0
>> z = x + eps(x);
>> z > x
ans =
1×5 logical array
1 1 1 1 1
You can see the value of eps for each element of x:
>> eps(x)
ans =
Columns 1 through 3
2.22044604925031e-16 4.44089209850063e-16 4.44089209850063e-16
Columns 4 through 5
8.88178419700125e-16 8.88178419700125e-16

Iniciar sesión para comentar.

Más respuestas (5)

Jan
Jan el 21 de Feb. de 2017
Editada: Jan el 21 de Feb. de 2017
According to your comment and Guillaume's suggestion:
assert(mod(x, 1) == 0, '%s: X must be an integer', mfilename);
Or safe this as M-file:
function T = isIntegerValue(X)
T = (mod(X, 1) == 0);
end
And then:
if ~isIntegerValue(X)
error('%s: X must be an integer', mfilename);
end

Hung Chu
Hung Chu el 21 de Feb. de 2017
Thank you! It works! But I don't know why LOL :P Could you explain to me how that function handles works?

Adam
Adam el 21 de Feb. de 2017
I use e.g.
validateattributes( myInteger, { 'double' }, { 'scalar', 'integer' } )
for validating my input arguments. Not so useful if you want a boolean/logical returned though to do something with after, although you can catch the exception that gets thrown but its untidy and inefficient to do that.

randell mullally
randell mullally el 3 de Jul. de 2019
I needed the same thing. these answers are Blaah for me so here was my soloution.
clear;
clc;
A=[3.6,4,4.25,5,6.175,nan,inf];
SizeA=size(A);
B=zeros(SizeA);
for i=1:SizeA(1,2)
if A(i)/round(A(i))==1
B(i)=1;
end
end
AB=[A;B]
AB =
3.6000 4.0000 4.2500 5.0000 6.1750 NaN Inf
0 1.0000 0 1.0000 0 0 0
I suppose if you want to test just one at a time... and let y be the test subject.
y=5;
if y/round(y)==1
fprintf('%3g is an intiger!! hooray\n',y)
else
fprintf('%3g is not an intiger!! sorry\n',y)
end
5 is an intiger!! hooray
testing this with A yeilds good results.
clear;
clc;
A=[3.6,4,4.25,5,6.175,nan,inf];
SizeA=size(A);
B=zeros(SizeA);
for i=1:SizeA(1,2)
if A(i)/round(A(i))==1
B(i)=1;
end
y=A(i);
if y/round(y)==1
fprintf('%3g is an intiger!! hooray\n',y)
else
fprintf('%3g is not an intiger!! sorry\n',y)
end
end
AB=[A;B];
3.6 is not an intiger!! sorry
4 is an intiger!! hooray
4.25 is not an intiger!! sorry
5 is an intiger!! hooray
6.175 is not an intiger!! sorry
NaN is not an intiger!! sorry
Inf is not an intiger!! sorry
If you really really really like this, ALOT!
find me on linkedin and endorse my matlab skill.
Best of luck!
  1 comentario
Cris Luengo
Cris Luengo el 9 de Jul. de 2020
This does not work if the input is 0.
What is wrong with the other answers? They actually work!

Iniciar sesión para comentar.


Weimin Zhang
Weimin Zhang el 23 de Feb. de 2023
For an array x, perhaps use this:
assert(~any(mod(x,1)),'x must have integer(s) only');
or a function allIntegers = @(x)~any(mod(x,1))
For example,
>> allIntegers([1,2,3])
ans = logical 1
>> allIntegers([1,2.5,3])
ans = logical 0

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by