comparison scripts/general/rem.m @ 7494:bd2bd04e68ca

Treat integer types for mod/rem correctly
author David Bateman <dbateman@free.fr>
date Mon, 18 Feb 2008 18:08:29 +0100
parents 8b7b4f58199f
children eb63fbe60fab
comparison
equal deleted inserted replaced
7493:f1bce8450fd0 7494:bd2bd04e68ca
41 41
42 if (! size_equal (x, y) && ! (isscalar (x) || isscalar (y))) 42 if (! size_equal (x, y) && ! (isscalar (x) || isscalar (y)))
43 error ("rem: argument sizes must agree"); 43 error ("rem: argument sizes must agree");
44 endif 44 endif
45 45
46 ## Matlab allows complex arguments, but as far as I can tell, that's a
47 ## bunch of hooey.
48
49 if (isreal (x) && isreal (y)) 46 if (isreal (x) && isreal (y))
50 r = x - y .* fix (x ./ y); 47 if (isinteger(x) || isinteger(y))
48 if (isinteger (x))
49 typ = class (x);
50 else
51 typ = class (y);
52 endif
53 r = x - y .* cast (fix (double (x) ./ double (y)), typ);
54 else
55 r = x - y .* fix (x ./ y);
56 endif
51 else 57 else
52 error ("rem: complex arguments are not allowed"); 58 error ("rem: complex arguments are not allowed");
53 endif 59 endif
54 60
55 endfunction 61 endfunction
56 62
57 %!assert(all (all (rem ([1, 2, 3; -1, -2, -3], 2) == [1, 0, 1; -1, 0, -1]))); 63 %!assert(rem ([1, 2, 3; -1, -2, -3], 2), [1, 0, 1; -1, 0, -1]);
58 64
59 %!assert(all (all (rem ([1, 2, 3; -1, -2, -3], 2 * ones (2, 3)) 65 %!assert(rem ([1, 2, 3; -1, -2, -3], 2 * ones (2, 3)),[1, 0, 1; -1, 0, -1]);
60 %! == [1, 0, 1; -1, 0, -1])));
61 66
62 %!error rem (); 67 %!error rem ();
63 68
64 %!error rem (1, 2, 3); 69 %!error rem (1, 2, 3);
65 70
66 %!error rem ([1, 2], [3, 4, 5]); 71 %!error rem ([1, 2], [3, 4, 5]);
67 72
68 %!error rem (i, 1); 73 %!error rem (i, 1);
69 74
75 %!assert(rem (uint8([1, 2, 3; -1, -2, -3]), uint8 (2)), uint8([1, 0, 1; -1, 0, -1]));
76
77 %!assert(uint8(rem ([1, 2, 3; -1, -2, -3], 2 * ones (2, 3))),uint8([1, 0, 1; -1, 0, -1]));
78
79 %!error rem (uint(8),int8(5));
80
81 %!error rem (uint8([1, 2]), uint8([3, 4, 5]));