I'm writing a program that will calculate factorials of integers. However, the part I'm stuck on is if someone enters a non-integer such as 1.3, I'd like to be able to test the input and display "The number you have entered is not an integer"
You can use the mod function, which returns the remainder after division. All integers are divisible by 1. So a good test for non-integer would be
integerTest=~mod(value,1);
This returns 0 if value is not an integer and 1 if it is. You can then use this as a conditional to reject non-integer user inputs.
-
1Clever idea! In Matlab R2014a it's viceversa, however. mod(text,1) is 0 if a number is integer or not. Jun 4 2015 at 12:30
-
mod will not work with vector as an input argument. e.g mod([3 2],1) will return [0 0], which will be evaluated as true (Integer Value). Jun 2 2016 at 11:27
Here is another variation (you can see it being used in ISIND function: edit isind.m):
integerTest = ( x == floor(x) );
On my machine, it is faster than the other proposed solutions:
%# create a vector of doubles, containing integers and non-integers
x = (1:100000)'; %'
idx = ( rand(size(x)) < 0.5 );
x(idx) = x(idx) + rand(sum(idx),1);
%# test for integers
tic, q1 = ~mod(x, 1); toc
tic, q2 = x==double(uint64(x)); toc
tic, q3 = x==floor(x); toc
%# compare results
assert( isequal(q1,q2,q3) )
Timings:
Elapsed time is 0.012253 seconds.
Elapsed time is 0.014201 seconds.
Elapsed time is 0.005665 seconds.
-
-
@KronoS: I'd say both are in the same order of magnitude, the difference is negligible. Here is a another comparison using the TIMEIT function: pastebin.com/xMEmSi5h– AmroMay 1 2013 at 22:44
-
2@ChristopherBarber: ok, then we should add
isfiniteto catch Inf and NaN cases, as in:integerTest = isfinite(x) & ( x == floor(x) );– AmroJul 16 2015 at 16:20 -
2@ChristopherBarber: and if you really want to be thorough, you should also test numbers are non-complex with
isreal– AmroJul 16 2015 at 16:24 -
You can cast the value to an integer and back to a double and check the result against the original value:
>> x = 1.3;
>> x == double(uint64(x))
ans =
0
>> x = 2;
>> x == double(uint64(x))
ans =
1
Interestingly, R.M.'s approach of using MOD runs faster in a loop and the above casting approach runs faster when vectorized:
>> x = rand(100000, 1); >> tic; for ii = 1:100000; ~mod(x(ii), 1); end; toc; Elapsed time is 0.018380 seconds. >> tic; for ii = 1:100000; x(ii) == double(uint64(x(ii))); end; toc; Elapsed time is 0.383020 seconds. >> tic; ~mod(x, 1); toc; Elapsed time is 0.005299 seconds. >> tic; x == double(uint64(x)); toc; Elapsed time is 0.002971 seconds.
-
3In general, you don't know how large the integer is. The maximum in
uint64is of order 10^(18). While we can be sure that you can't compute the factorial of something that large (in which case this works), as a general approach, this fails for large numbers.– user564376Mar 22 2011 at 20:27 -
This seems a bit ... unmatlabesque. Use round(), fix(), floor(), or ceil() if you want to make an integer-valued quantity.– nibotMar 22 2011 at 21:35
-
Why the downvote? d'o-o'b is correct about the limit imposed by
uint64but as he explained this is not an operational limit since computing the factorial of a number near this limit could not be done anyways.– b3.Mar 22 2011 at 21:37 -
1@nibot: I don't understand your "unmatlabesque" comment.
doubleanduint64are as much a part of the MATLAB built-in library as the functions you suggested.– b3.Mar 22 2011 at 21:38
assert(isnumeric(input) && round(input) == input, 'That number is not an integer.')
You could add other checks, (like for positivity) easily as well.
Edited using isinteger. Thanks @SolarStatistics, I hadn't noticed they added this functionality.
Edited back to original answer again as isinteger isn't appropriate (see comments below).
-
5"isinteger" is probably not what's wanted, since it checks for an integer TYPE. The OP probably wants a conditional check like "x - fix(x) == 0" or "x - fix(x) < eps" that works on doubles.– nibotMar 22 2011 at 19:36
As point out by @nibot isinteger tests for the input as an integer TYPE. Instead you could check to see if rounding input returns the same value as input. eg:
assert(abs(round(input)-input))<eps*2,'That number is not an integer.')
for example
>> input=1.3;
>> assert(abs(round(input)-input)<eps*2,'That number is not an integer.')
??? That number is not an integer.
>> input=3;
>> assert(abs(round(input)-input)<eps*2,'That number is not an integer.')
>>
I just wanted to point out that the provided methods all test for whether the input is a Gaussian integer, meaning that the real and imaginary parts are both integers. If you need to care about the imaginary part then you need to deal with it separately.
For my applications, inputs with imaginary components shouldn't be considered a valid integer, so I have this:
function boolResult = fnIsInteger(input)
%validate input
if isempty(input)
error('Input cannot be empty')
elseif ~isnumeric(input)
error('Input must be numeric')
end
boolResult = (imag(input) == 0) & (round(input) == input);
end
Using b3.'s tests:
>> x = rand(100000, 1);
>> tic; for ii = 1:100000; ~mod(x(ii), 1); end; toc;
Elapsed time is 0.003960 seconds.
>> tic; for ii = 1:100000; fnIsInteger(x(ii)); end; toc;
Elapsed time is 0.217397 seconds.
>> tic; ~mod(x, 1); toc;
Elapsed time is 0.000967 seconds.
>> tic; fnIsInteger(x); toc;
Elapsed time is 0.003195 seconds.
The looped call is quite a bit slower mostly due to the function overhead. Replacing the arithmetic expression with ~mod(dataInput, 1) will make it only 50% faster than the code that checks for imaginary parts.
By double command, you cannot get the correct answer:
>> double(uint64(21/22))
ans =
1
>> double(uint64(22/22))
ans =
1
also floor,round,... have problem with such cases:
floor(22/22)==21.99999999999999999999999999999999999/22
but mod seems can distinguish 22/22 and 21.99999999999999999999999999999999999/22:
>> mod(22,22)
ans =
0
>> (21.99999999999999999999999999999999999/22)
ans =
1
-
1-1 this does not answer the question of testing for integers.. As for the literal
21.9999...you entered, it is actually22! (you have to be mindful of the limitations of floating-point representation: floating-point-precision)– AmroFeb 18 2014 at 6:15
doc gammafor the equivalent of factorial for non-integers.