Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

test_isinteger

By: a guest on May 1st, 2013  |  syntax: MatLab  |  size: 0.79 KB  |  views: 60  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function t = test_is_int()
  2.     % See: http://stackoverflow.com/a/6916862/97160
  3.    
  4.     % create a vector of doubles, containing integers and non-integers
  5.     x = (1:1e5)';
  6.     idx = ( rand(size(x)) < 0.5 );
  7.     x(idx) = x(idx) + rand(sum(idx(:)),1);
  8.    
  9.     % create functions to test
  10.     funcs = {@func1; @func2; @func3; @func4};
  11.    
  12.     % timeit and store results
  13.     t = zeros(size(funcs));
  14.     v = cell(size(funcs));
  15.     for i=1:numel(funcs)
  16.         f = @() funcs{i}(x);
  17.         t(i) = timeit(f);
  18.         v{i} = feval(f);
  19.     end
  20.     assert( isequal(v{:}) )
  21. end
  22.  
  23. function v = func1(x)
  24.     v = ~mod(x,1);
  25. end
  26.  
  27. function v = func2(x)
  28.     v = x==double(uint64(x));
  29. end
  30.  
  31. function v = func3(x)
  32.     v = x==floor(x);
  33. end
  34.  
  35. function v = func4(x)
  36.     v = x==round(x);
  37. end