annotate scripts/general/idivide.m @ 9051:1bf0ce0930be

Grammar check TexInfo in all .m files Cleanup documentation sources to follow a few consistent rules. Spellcheck was NOT done. (but will be in another changeset)
author Rik <rdrider0-list@yahoo.com>
date Fri, 27 Mar 2009 22:31:03 -0700
parents 58604c45ca74
children 16f53d29049f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
1 ## Copyright (C) 2008 David Bateman
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
20 ## @deftypefn {Function File} {} idivide (@var{x}, @var{y}, @var{op})
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 7627
diff changeset
21 ## Integer division with different round rules. The standard behavior of
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
22 ## the an integer division such as @code{@var{a} ./ @var{b}} is to round
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 7627
diff changeset
23 ## the result to the nearest integer. This is not always the desired
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
24 ## behavior and @code{idivide} permits integer element-by-element
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
25 ## division to be performed with different treatment for the fractional
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 7627
diff changeset
26 ## part of the division as determined by the @var{op} flag. @var{op} is
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
27 ## a string with one of the values:
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
28 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
29 ## @table @asis
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 ## @item "fix"
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 ## towards zero.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
33 ## @item "round"
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
34 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
35 ## towards the nearest integer.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 ## @item "floor"
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 ## downwards.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
39 ## @item "ceil"
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 ## upwards.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 ## @end table
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 ## @noindent
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 ## If @var{op} is not given it is assumed that it is @code{"fix"}.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
46 ## An example demonstrating these rounding rules is
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
48 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
49 ## @group
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 ## idivide (int8 ([-3, 3]), int8 (4), "fix")
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
51 ## @result{} int8 ([0, 0])
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 ## idivide (int8 ([-3, 3]), int8 (4), "round")
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 ## @result{} int8 ([-1, 1])
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
54 ## idivide (int8 ([-3, 3]), int8 (4), "ceil")
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 ## @result{} int8 ([0, 1])
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
56 ## idivide (int8 ([-3, 3]), int8 (4), "floor")
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
57 ## @result{} int8 ([-1, 0])
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
58 ## @end group
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
59 ## @end example
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
60 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
61 ## @seealso{ldivide, rdivide}
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
62 ## @end deftypefn
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
63
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
64 function z = idivide (x, y, op)
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 if (nargin < 2 || nargin > 3)
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
66 print_usage ();
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 elseif (nargin == 2)
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
68 op = "fix";
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
69 else
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
70 op = tolower (op);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
71 endif
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
72
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
73 if (strcmp (op, "round"))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
74 z = x ./ y;
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
75 else
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
76 if (isfloat (x))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
77 typ = class (y);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
78 elseif (isfloat (y))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 typ = class (x);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
80 else
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
81 typ = class (x);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
82 if (!strcmp (class (x), class (y)))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
83 error ("idivide: incompatible types");
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
84 endif
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
85 endif
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
86
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
87 if (strcmp (op, "fix"))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
88 z = cast (fix (double (x) ./ double (y)), typ);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
89 elseif (strcmp (op, "floor"))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
90 z = cast (floor (double (x) ./ double (y)), typ);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
91 elseif (strcmp (op, "ceil"))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
92 z = cast (ceil (double (x) ./ double (y)), typ);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
93 else
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
94 error ("idivide: unrecognized rounding type");
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
95 endif
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
96 endif
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
97 endfunction
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
98
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
99 %!shared a, af, b, bf
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
100 %! a = int8(3);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
101 %! af = 3;
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
102 %! b = int8([-4, 4]);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
103 %! bf = [-4, 4];
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
104
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
105 %!assert (idivide (a, b), int8 ([0, 0]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
106 %!assert (idivide (a, b, "floor"), int8([-1, 0]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
107 %!assert (idivide (a, b, "ceil"), int8 ([0, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
108 %!assert (idivide (a, b, "round"), int8 ([-1, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
109
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
110 %!assert (idivide (af, b), int8 ([0, 0]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
111 %!assert (idivide (af, b, "floor"), int8([-1, 0]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
112 %!assert (idivide (af, b, "ceil"), int8 ([0, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
113 %!assert (idivide (af, b, "round"), int8 ([-1, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
114
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
115 %!assert (idivide (a, bf), int8 ([0, 0]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
116 %!assert (idivide (a, bf, "floor"), int8([-1, 0]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
117 %!assert (idivide (a, bf, "ceil"), int8 ([0, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
118 %!assert (idivide (a, bf, "round"), int8 ([-1, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
119
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
120 %!error (idivide (uint8(1), int8(1)))