Mercurial > hg > octave-lyh
annotate scripts/general/idivide.m @ 17380:63b53ea33a8b
imread: fix returning multiple variables.
* imread.m: fix mistake when filling varargout from cset. Do not miss
the first cell.
* __magick_read__.cc (read_maps): change class of alpha map which is
only a column vector, not a Nx3 Matrix.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Wed, 04 Sep 2013 21:22:43 +0100 |
parents | bc924baa2c4e |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
1 ## Copyright (C) 2008-2012 David Bateman |
7627 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} 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
|
21 ## Integer division with different rounding rules. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
23 ## 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
|
24 ## 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
|
25 ## desired behavior and @code{idivide} permits integer element-by-element |
7627 | 26 ## 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
|
27 ## 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
|
28 ## a string with one of the values: |
7627 | 29 ## |
30 ## @table @asis | |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
31 ## @item @qcode{"fix"} |
7627 | 32 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded |
33 ## towards zero. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
34 ## |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
35 ## @item @qcode{"round"} |
7627 | 36 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded |
37 ## towards the nearest integer. | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
38 ## |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
39 ## @item @qcode{"floor"} |
7627 | 40 ## 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
|
41 ## towards negative infinity. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
42 ## |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
43 ## @item @qcode{"ceil"} |
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 positive infinity. |
7627 | 46 ## @end table |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
47 ## |
7627 | 48 ## @noindent |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
49 ## If @var{op} is not given it defaults to @qcode{"fix"}. |
7627 | 50 ## An example demonstrating these rounding rules is |
51 ## | |
52 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
53 ## @group |
7627 | 54 ## idivide (int8 ([-3, 3]), int8 (4), "fix") |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 ## @result{} int8 ([0, 0]) |
7627 | 56 ## idivide (int8 ([-3, 3]), int8 (4), "round") |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
57 ## @result{} int8 ([-1, 1]) |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
58 ## idivide (int8 ([-3, 3]), int8 (4), "floor") |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
59 ## @result{} int8 ([-1, 0]) |
7627 | 60 ## idivide (int8 ([-3, 3]), int8 (4), "ceil") |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
61 ## @result{} int8 ([0, 1]) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
62 ## @end group |
7627 | 63 ## @end example |
64 ## | |
65 ## @seealso{ldivide, rdivide} | |
66 ## @end deftypefn | |
67 | |
68 function z = idivide (x, y, op) | |
69 if (nargin < 2 || nargin > 3) | |
70 print_usage (); | |
71 elseif (nargin == 2) | |
72 op = "fix"; | |
73 else | |
74 op = tolower (op); | |
75 endif | |
76 | |
77 if (strcmp (op, "round")) | |
78 z = x ./ y; | |
79 else | |
80 if (isfloat (x)) | |
81 typ = class (y); | |
82 elseif (isfloat (y)) | |
83 typ = class (x); | |
84 else | |
85 typ = class (x); | |
86 if (!strcmp (class (x), class (y))) | |
10549 | 87 error ("idivide: incompatible types"); |
7627 | 88 endif |
89 endif | |
90 | |
91 if (strcmp (op, "fix")) | |
92 z = cast (fix (double (x) ./ double (y)), typ); | |
93 elseif (strcmp (op, "floor")) | |
94 z = cast (floor (double (x) ./ double (y)), typ); | |
95 elseif (strcmp (op, "ceil")) | |
96 z = cast (ceil (double (x) ./ double (y)), typ); | |
97 else | |
98 error ("idivide: unrecognized rounding type"); | |
99 endif | |
100 endif | |
101 endfunction | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
103 |
7627 | 104 %!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
|
105 %! a = int8 (3); |
7627 | 106 %! af = 3; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
107 %! b = int8 ([-4, 4]); |
7627 | 108 %! bf = [-4, 4]; |
109 | |
110 %!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
|
111 %!assert (idivide (a, b, "floor"), int8 ([-1, 0])) |
7627 | 112 %!assert (idivide (a, b, "ceil"), int8 ([0, 1])) |
113 %!assert (idivide (a, b, "round"), int8 ([-1, 1])) | |
114 | |
115 %!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
|
116 %!assert (idivide (af, b, "floor"), int8 ([-1, 0])) |
7627 | 117 %!assert (idivide (af, b, "ceil"), int8 ([0, 1])) |
118 %!assert (idivide (af, b, "round"), int8 ([-1, 1])) | |
119 | |
120 %!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
|
121 %!assert (idivide (a, bf, "floor"), int8 ([-1, 0])) |
7627 | 122 %!assert (idivide (a, bf, "ceil"), int8 ([0, 1])) |
123 %!assert (idivide (a, bf, "round"), int8 ([-1, 1])) | |
124 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
125 %!error (idivide (uint8 (1), int8 (1))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
126 |