changelog shortlog tags changeset files revisions annotate raw

scripts/general/rem.m

changeset 10289: 4b124317dc38
parent:c1fff751b5a8
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (59 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004,
2## 2005, 2006, 2007, 2008, 2009 John W. Eaton
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Mapping Function} {} rem (@var{x}, @var{y})
22## Return the remainder of the division @code{@var{x} / @var{y}}, computed
23## using the expression
24##
25## @example
26## x - y .* fix (x ./ y)
27## @end example
28##
29## An error message is printed if the dimensions of the arguments do not
30## agree, or if either of the arguments is complex.
31## @seealso{mod, fmod}
32## @end deftypefn
33
34## Author: jwe
35
36function r = rem (x, y)
37
38 if (nargin != 2)
39 print_usage ();
40 endif
41
42 if (! size_equal (x, y) && ! (isscalar (x) || isscalar (y)))
43 error ("rem: argument sizes must agree");
44 endif
45
46 if (isreal (x) && isreal (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
57 else
58 error ("rem: complex arguments are not allowed");
59 endif
60
61endfunction
62
63%!assert(rem ([1, 2, 3; -1, -2, -3], 2), [1, 0, 1; -1, 0, -1]);
64
65%!assert(rem ([1, 2, 3; -1, -2, -3], 2 * ones (2, 3)),[1, 0, 1; -1, 0, -1]);
66
67%!error rem ();
68
69%!error rem (1, 2, 3);
70
71%!error rem ([1, 2], [3, 4, 5]);
72
73%!error rem (i, 1);
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]));