Mercurial > hg > octave-nkf
annotate scripts/general/idivide.m @ 10412:d2ac9433cd09
append AM_CPPFLAGS to libcruft_la_CPPFLAGS
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 15 Mar 2010 16:56:14 -0400 |
parents | 16f53d29049f |
children | 95c3e38098bf |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2008, 2009 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}) | |
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 | 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 | 24 ## behavior and @code{idivide} permits integer element-by-element |
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 | 27 ## a string with one of the values: |
28 ## | |
29 ## @table @asis | |
30 ## @item "fix" | |
31 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded | |
32 ## towards zero. | |
33 ## @item "round" | |
34 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded | |
35 ## towards the nearest integer. | |
36 ## @item "floor" | |
37 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded | |
38 ## downwards. | |
39 ## @item "ceil" | |
40 ## Calculate @code{@var{a} ./ @var{b}} with the fractional part rounded | |
41 ## upwards. | |
42 ## @end table | |
43 ## | |
44 ## @noindent | |
45 ## If @var{op} is not given it is assumed that it is @code{"fix"}. | |
46 ## An example demonstrating these rounding rules is | |
47 ## | |
48 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
49 ## @group |
7627 | 50 ## idivide (int8 ([-3, 3]), int8 (4), "fix") |
51 ## @result{} int8 ([0, 0]) | |
52 ## idivide (int8 ([-3, 3]), int8 (4), "round") | |
53 ## @result{} int8 ([-1, 1]) | |
54 ## idivide (int8 ([-3, 3]), int8 (4), "ceil") | |
55 ## @result{} int8 ([0, 1]) | |
56 ## idivide (int8 ([-3, 3]), int8 (4), "floor") | |
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 | 59 ## @end example |
60 ## | |
61 ## @seealso{ldivide, rdivide} | |
62 ## @end deftypefn | |
63 | |
64 function z = idivide (x, y, op) | |
65 if (nargin < 2 || nargin > 3) | |
66 print_usage (); | |
67 elseif (nargin == 2) | |
68 op = "fix"; | |
69 else | |
70 op = tolower (op); | |
71 endif | |
72 | |
73 if (strcmp (op, "round")) | |
74 z = x ./ y; | |
75 else | |
76 if (isfloat (x)) | |
77 typ = class (y); | |
78 elseif (isfloat (y)) | |
79 typ = class (x); | |
80 else | |
81 typ = class (x); | |
82 if (!strcmp (class (x), class (y))) | |
83 error ("idivide: incompatible types"); | |
84 endif | |
85 endif | |
86 | |
87 if (strcmp (op, "fix")) | |
88 z = cast (fix (double (x) ./ double (y)), typ); | |
89 elseif (strcmp (op, "floor")) | |
90 z = cast (floor (double (x) ./ double (y)), typ); | |
91 elseif (strcmp (op, "ceil")) | |
92 z = cast (ceil (double (x) ./ double (y)), typ); | |
93 else | |
94 error ("idivide: unrecognized rounding type"); | |
95 endif | |
96 endif | |
97 endfunction | |
98 | |
99 %!shared a, af, b, bf | |
100 %! a = int8(3); | |
101 %! af = 3; | |
102 %! b = int8([-4, 4]); | |
103 %! bf = [-4, 4]; | |
104 | |
105 %!assert (idivide (a, b), int8 ([0, 0])) | |
106 %!assert (idivide (a, b, "floor"), int8([-1, 0])) | |
107 %!assert (idivide (a, b, "ceil"), int8 ([0, 1])) | |
108 %!assert (idivide (a, b, "round"), int8 ([-1, 1])) | |
109 | |
110 %!assert (idivide (af, b), int8 ([0, 0])) | |
111 %!assert (idivide (af, b, "floor"), int8([-1, 0])) | |
112 %!assert (idivide (af, b, "ceil"), int8 ([0, 1])) | |
113 %!assert (idivide (af, b, "round"), int8 ([-1, 1])) | |
114 | |
115 %!assert (idivide (a, bf), int8 ([0, 0])) | |
116 %!assert (idivide (a, bf, "floor"), int8([-1, 0])) | |
117 %!assert (idivide (a, bf, "ceil"), int8 ([0, 1])) | |
118 %!assert (idivide (a, bf, "round"), int8 ([-1, 1])) | |
119 | |
120 %!error (idivide (uint8(1), int8(1))) |