Mercurial > octave
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 |
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 | 6 ## |
7 ## This file is part of Octave. | |
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 | 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 | 13 ## |
14 ## Octave is distributed in the hope that it will be useful, but | |
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 | 18 ## |
19 ## You should have received a copy of the GNU General Public License | |
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 | 22 |
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 | 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 | 33 ## |
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 | 36 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded |
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 | 40 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded |
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 | 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 | 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 | 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 | 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 | 54 ## An example demonstrating these rounding rules is |
55 ## | |
56 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
57 ## @group |
7627 | 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 | 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 | 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 | 67 ## @end example |
68 ## | |
69 ## @seealso{ldivide, rdivide} | |
70 ## @end deftypefn | |
71 | |
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 | 74 if (nargin < 2 || nargin > 3) |
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 | 79 op = "fix"; |
80 else | |
81 op = tolower (op); | |
82 endif | |
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 | 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 | 92 typ = class (x); |
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 | 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 | 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 | 113 endif |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
114 |
7627 | 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 | 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 | 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 | 122 %! bf = [-4, 4]; |
123 | |
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 | 126 %!assert (idivide (a, b, "ceil"), int8 ([0, 1])) |
127 %!assert (idivide (a, b, "round"), int8 ([-1, 1])) | |
128 | |
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 | 131 %!assert (idivide (af, b, "ceil"), int8 ([0, 1])) |
132 %!assert (idivide (af, b, "round"), int8 ([-1, 1])) | |
133 | |
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 | 136 %!assert (idivide (a, bf, "ceil"), int8 ([0, 1])) |
137 %!assert (idivide (a, bf, "round"), int8 ([-1, 1])) | |
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") |