annotate scripts/general/idivide.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 2008-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27742
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27742
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27742
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27742
diff changeset
5 ##
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## This file is part of Octave.
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## (at your option) any later version.
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ## 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
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
7627
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 ## 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
20 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
21 ## <https://www.gnu.org/licenses/>.
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
22
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
24 ## @deftypefn {} {} idivide (@var{x}, @var{y}, @var{op})
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
25 ## Integer division with different rounding rules.
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
26 ##
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
27 ## The standard behavior of integer division such as @code{@var{a} ./ @var{b}}
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
28 ## is to round the result to the nearest integer. This is not always the
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
29 ## desired behavior and @code{idivide} permits integer element-by-element
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 ## 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
31 ## part of the division as determined by the @var{op} flag. @var{op} is
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
32 ## a string with one of the values:
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
33 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
34 ## @table @asis
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
35 ## @item @qcode{"fix"}
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37 ## towards zero.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
38 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
39 ## @item @qcode{"round"}
7627
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 ## towards the nearest integer.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
42 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
43 ## @item @qcode{"floor"}
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
45 ## towards negative infinity.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
46 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
47 ## @item @qcode{"ceil"}
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
48 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
49 ## towards positive infinity.
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 ## @end table
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
51 ##
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 ## @noindent
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
53 ## If @var{op} is not given it defaults to @qcode{"fix"}.
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
54 ## An example demonstrating these rounding rules is
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
56 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
57 ## @group
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
58 ## idivide (int8 ([-3, 3]), int8 (4), "fix")
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
59 ## @result{} 0 0
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
60 ## idivide (int8 ([-3, 3]), int8 (4), "round")
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
61 ## @result{} -1 1
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
62 ## idivide (int8 ([-3, 3]), int8 (4), "floor")
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
63 ## @result{} -1 0
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
64 ## idivide (int8 ([-3, 3]), int8 (4), "ceil")
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
65 ## @result{} 0 1
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
66 ## @end group
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 ## @end example
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
68 ##
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
69 ## @seealso{ldivide, rdivide}
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
70 ## @end deftypefn
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
71
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
72 function z = idivide (x, y, op)
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 20852
diff changeset
73
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
74 if (nargin < 2 || nargin > 3)
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
75 print_usage ();
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
76 endif
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
77
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
78 if (nargin == 2)
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 op = "fix";
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 op = tolower (op);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
82 endif
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
83
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
84 if (isfloat (x))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
85 if (isinteger (y))
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
86 typ = class (y);
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
87 else
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
88 error ("idivide: at least one input (X or Y) must be an integer type");
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
89 endif
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
90 elseif (isfloat (y))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
91 if (isinteger (x))
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
92 typ = class (x);
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
93 else
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
94 error ("idivide: at least one input (X or Y) must be an integer type");
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
95 endif
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
96 else
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
97 typ = class (x);
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
98 if (! strcmp (typ, class (y)))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
99 error ("idivide: integer type of X (%s) must match integer type of Y (%s)", typ, class (y));
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
100 endif
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
101 endif
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
102
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
103 if (strcmp (op, "fix"))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
104 z = cast (fix (double (x) ./ double (y)), typ);
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
105 elseif (strcmp (op, "round"))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
106 z = x ./ y;
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
107 elseif (strcmp (op, "floor"))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
108 z = cast (floor (double (x) ./ double (y)), typ);
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
109 elseif (strcmp (op, "ceil"))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
110 z = cast (ceil (double (x) ./ double (y)), typ);
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
111 else
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
112 error ('idivide: unrecognized rounding type "%s"', op);
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
113 endif
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 20852
diff changeset
114
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
115 endfunction
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
116
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
117
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
118 %!shared a, af, b, bf
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
119 %! a = int8 (3);
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
120 %! af = 3;
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
121 %! b = int8 ([-4, 4]);
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
122 %! bf = [-4, 4];
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
123
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
124 %!assert (idivide (a, b), int8 ([0, 0]))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
125 %!assert (idivide (a, b, "floor"), int8 ([-1, 0]))
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
126 %!assert (idivide (a, b, "ceil"), int8 ([0, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
127 %!assert (idivide (a, b, "round"), int8 ([-1, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
128
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
129 %!assert (idivide (af, b), int8 ([0, 0]))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
130 %!assert (idivide (af, b, "floor"), int8 ([-1, 0]))
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
131 %!assert (idivide (af, b, "ceil"), int8 ([0, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
132 %!assert (idivide (af, b, "round"), int8 ([-1, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
133
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
134 %!assert (idivide (a, bf), int8 ([0, 0]))
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
135 %!assert (idivide (a, bf, "floor"), int8 ([-1, 0]))
7627
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
136 %!assert (idivide (a, bf, "ceil"), int8 ([0, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
137 %!assert (idivide (a, bf, "round"), int8 ([-1, 1]))
0483fad1d888 Add the idivide function
David Bateman <dbateman@free.fr>
parents:
diff changeset
138
27742
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
139 ## Test input validation
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
140 %!error idivide (uint8 (1))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
141 %!error idivide (uint8 (1), 2, 3)
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
142 %!error <at least one input> idivide (1, 2)
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
143 %!error <at least one input> idivide ({1}, 2)
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
144 %!error <X \(int8\) must match.* Y \(uint8\)> idivide (int8 (1), uint8 (2))
d39c56824d01 idivid.m: error out if both inputs are floating point (bug #57278).
Rik <rik@octave.org>
parents: 26600
diff changeset
145 %!error <unrecognized rounding type "foo"> idivide (int8 (1), 2, "foo")