Mercurial > octave
annotate doc/interpreter/numbers.txi @ 31069:43974344fe19 stable
doc: Expand documentation about type promotion and demotion (bug #62283)
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Sat, 04 Jun 2022 17:13:38 -0400 |
parents | 796f54d4ddbf |
children | 2dee06f4635c |
rev | line source |
---|---|
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29982
diff
changeset
|
1 @c Copyright (C) 1996-2022 The Octave Project Developers |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 @c Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
6 @c 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
|
7 @c 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
|
8 @c (at your option) any later version. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
9 @c |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
10 @c Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
11 @c WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 @c GNU General Public License for more details. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
14 @c |
7018 | 15 @c You should have received a copy of the GNU General Public License |
16 @c 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
|
17 @c <https://www.gnu.org/licenses/>. |
3294 | 18 |
4167 | 19 @node Numeric Data Types |
3294 | 20 @chapter Numeric Data Types |
21 @cindex numeric constant | |
22 @cindex numeric value | |
23 | |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
24 A @dfn{numeric constant} may be a scalar, a vector, or a matrix, and it may |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
25 contain complex values. |
3294 | 26 |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
27 The simplest form of a numeric constant, a scalar, is a single number. Note |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
28 that by default numeric constants are represented within Octave by IEEE 754 |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
29 double precision (binary64) floating-point format (complex constants are |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
30 stored as pairs of binary64 values). It is, however, possible to represent |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
31 real integers as described in @ref{Integer Data Types}. |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
32 |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
33 If the numeric constant is a real integer, it can be defined in decimal, |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
34 hexadecimal, or binary notation. Hexadecimal notation starts with @samp{0x} or |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
35 @samp{0X}, binary notation starts with @samp{0b} or @samp{0B}, otherwise |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
36 decimal notation is assumed. As a consequence, @samp{0b} is not a hexadecimal |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
37 number, in fact, it is not a valid number at all. |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
38 |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
39 For better readability, digits may be partitioned by the underscore separator |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
40 @samp{_}, which is ignored by the Octave interpreter. Here are some examples |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
41 of real-valued integer constants, which all represent the same value and are |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
42 internally stored as binary64: |
3294 | 43 |
44 @example | |
45 @group | |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
46 42 # decimal notation |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
47 0x2A # hexadecimal notation |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
48 0b101010 # binary notation |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
49 0b10_1010 # underscore notation |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
50 round (42.1) # also binary64 |
3294 | 51 @end group |
52 @end example | |
53 | |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
54 In decimal notation, the numeric constant may be denoted as decimal fraction |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
55 or even in scientific (exponential) notation. Note that this is not possible |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
56 for hexadecimal or binary notation. Again, in the following example all |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
57 numeric constants represent the same value: |
3294 | 58 |
59 @example | |
60 @group | |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
61 .105 |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
62 1.05e-1 |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
63 .00105e+2 |
3294 | 64 @end group |
65 @end example | |
66 | |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
67 Unlike most programming languages, complex numeric constants are denoted as |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
68 the sum of real and imaginary parts. The imaginary part is denoted by a |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
69 real-valued numeric constant followed immediately by a complex value indicator |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
70 (@samp{i}, @samp{j}, @samp{I}, or @samp{J} which represents |
3294 | 71 @tex |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
72 $\sqrt{-1}$). |
3294 | 73 @end tex |
6620 | 74 @ifnottex |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
75 @code{sqrt (-1)}). |
6620 | 76 @end ifnottex |
23217
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
77 No spaces are allowed between the numeric constant and the complex value |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
78 indicator. Some examples of complex numeric constants that all represent the |
893d99338cc8
doc: Reword manual section on numeric constants for clarity.
Rik <rik@octave.org>
parents:
23212
diff
changeset
|
79 same value: |
3294 | 80 |
81 @example | |
82 @group | |
23212
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
83 3 + 42i |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
84 3 + 42j |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
85 3 + 42I |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
86 3 + 42J |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
87 3.0 + 42.0i |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
88 3.0 + 0x2Ai |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
89 3.0 + 0b10_1010i |
0881e1671490
doc: Add documentation for hex and binary prefix and _ separator(bugs #50305, #50334)
Ernst Reissner <rei3ner@arcor.de>
parents:
22323
diff
changeset
|
90 0.3e1 + 420e-1i |
3294 | 91 @end group |
92 @end example | |
93 | |
6549 | 94 @DOCSTRING(double) |
95 | |
96 @DOCSTRING(complex) | |
97 | |
3294 | 98 @menu |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
99 * Matrices:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
100 * Ranges:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
101 * Single Precision Data Types:: |
6549 | 102 * Integer Data Types:: |
6781 | 103 * Bit Manipulations:: |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
104 * Logical Values:: |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
105 * Promotion and Demotion of Data Types:: |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
106 * Predicates for Numeric Objects:: |
3294 | 107 @end menu |
108 | |
4167 | 109 @node Matrices |
3294 | 110 @section Matrices |
111 @cindex matrices | |
112 | |
113 @opindex [ | |
114 @opindex ] | |
115 @opindex ; | |
116 @opindex , | |
117 | |
118 It is easy to define a matrix of values in Octave. The size of the | |
119 matrix is determined automatically, so it is not necessary to explicitly | |
120 state the dimensions. The expression | |
121 | |
122 @example | |
123 a = [1, 2; 3, 4] | |
124 @end example | |
125 | |
126 @noindent | |
127 results in the matrix | |
128 @tex | |
129 $$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ | |
130 @end tex | |
6620 | 131 @ifnottex |
3294 | 132 |
133 @example | |
134 @group | |
135 | |
136 / \ | |
137 | 1 2 | | |
138 a = | | | |
139 | 3 4 | | |
140 \ / | |
141 | |
142 @end group | |
143 @end example | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
144 |
6620 | 145 @end ifnottex |
3294 | 146 |
147 Elements of a matrix may be arbitrary expressions, provided that the | |
148 dimensions all make sense when combining the various pieces. For | |
149 example, given the above matrix, the expression | |
150 | |
151 @example | |
152 [ a, a ] | |
153 @end example | |
154 | |
155 @noindent | |
156 produces the matrix | |
157 | |
158 @example | |
159 @group | |
160 ans = | |
161 | |
162 1 2 1 2 | |
163 3 4 3 4 | |
164 @end group | |
165 @end example | |
166 | |
167 @noindent | |
168 but the expression | |
169 | |
170 @example | |
171 [ a, 1 ] | |
172 @end example | |
173 | |
174 @noindent | |
175 produces the error | |
176 | |
177 @example | |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
178 error: number of rows must match (1 != 2) near line 13, column 6 |
3294 | 179 @end example |
180 | |
181 @noindent | |
182 (assuming that this expression was entered as the first thing on line | |
183 13, of course). | |
184 | |
185 Inside the square brackets that delimit a matrix expression, Octave | |
186 looks at the surrounding context to determine whether spaces and newline | |
187 characters should be converted into element and row separators, or | |
4476 | 188 simply ignored, so an expression like |
3294 | 189 |
190 @example | |
191 @group | |
192 a = [ 1 2 | |
193 3 4 ] | |
194 @end group | |
195 @end example | |
196 | |
197 @noindent | |
198 will work. However, some possible sources of confusion remain. For | |
199 example, in the expression | |
200 | |
201 @example | |
202 [ 1 - 1 ] | |
203 @end example | |
204 | |
205 @noindent | |
206 the @samp{-} is treated as a binary operator and the result is the | |
207 scalar 0, but in the expression | |
208 | |
209 @example | |
210 [ 1 -1 ] | |
211 @end example | |
212 | |
213 @noindent | |
214 the @samp{-} is treated as a unary operator and the result is the | |
4476 | 215 vector @code{[ 1, -1 ]}. Similarly, the expression |
216 | |
217 @example | |
218 [ sin (pi) ] | |
219 @end example | |
220 | |
221 @noindent | |
222 will be parsed as | |
223 | |
224 @example | |
225 [ sin, (pi) ] | |
226 @end example | |
3294 | 227 |
4476 | 228 @noindent |
229 and will result in an error since the @code{sin} function will be | |
230 called with no arguments. To get around this, you must omit the space | |
231 between @code{sin} and the opening parenthesis, or enclose the | |
232 expression in a set of parentheses: | |
233 | |
234 @example | |
235 [ (sin (pi)) ] | |
236 @end example | |
237 | |
238 Whitespace surrounding the single quote character (@samp{'}, used as a | |
239 transpose operator and for delimiting character strings) can also cause | |
240 confusion. Given @code{a = 1}, the expression | |
3294 | 241 |
242 @example | |
243 [ 1 a' ] | |
244 @end example | |
245 | |
246 @noindent | |
4476 | 247 results in the single quote character being treated as a |
3294 | 248 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
249 expression | |
250 | |
251 @example | |
252 [ 1 a ' ] | |
253 @end example | |
254 | |
255 @noindent | |
256 produces the error message | |
257 | |
258 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
259 @group |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
260 parse error: |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
261 |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
262 syntax error |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
263 |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
264 >>> [ 1 a ' ] |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
265 ^ |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
266 @end group |
3294 | 267 @end example |
268 | |
269 @noindent | |
6620 | 270 because not doing so would cause trouble when parsing the valid expression |
3294 | 271 |
272 @example | |
273 [ a 'foo' ] | |
274 @end example | |
275 | |
276 For clarity, it is probably best to always use commas and semicolons to | |
4476 | 277 separate matrix elements and rows. |
3294 | 278 |
12190
66b2c377b38e
Add sizemax function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11573
diff
changeset
|
279 The maximum number of elements in a matrix is fixed when Octave is compiled. |
27880
31b95261c7d2
Backed out changeset 2890a931e647 (bug #47469)
Rik <rik@octave.org>
parents:
27627
diff
changeset
|
280 The allowable number can be queried with the function @code{sizemax}. Note |
31b95261c7d2
Backed out changeset 2890a931e647 (bug #47469)
Rik <rik@octave.org>
parents:
27627
diff
changeset
|
281 that other factors, such as the amount of memory available on your machine, |
31b95261c7d2
Backed out changeset 2890a931e647 (bug #47469)
Rik <rik@octave.org>
parents:
27627
diff
changeset
|
282 may limit the maximum size of matrices to something smaller. |
31b95261c7d2
Backed out changeset 2890a931e647 (bug #47469)
Rik <rik@octave.org>
parents:
27627
diff
changeset
|
283 |
31b95261c7d2
Backed out changeset 2890a931e647 (bug #47469)
Rik <rik@octave.org>
parents:
27627
diff
changeset
|
284 @DOCSTRING(sizemax) |
12190
66b2c377b38e
Add sizemax function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11573
diff
changeset
|
285 |
3294 | 286 When you type a matrix or the name of a variable whose value is a |
287 matrix, Octave responds by printing the matrix in with neatly aligned | |
288 rows and columns. If the rows of the matrix are too large to fit on the | |
289 screen, Octave splits the matrix and displays a header before each | |
290 section to indicate which columns are being displayed. You can use the | |
291 following variables to control the format of the output. | |
292 | |
3321 | 293 @DOCSTRING(output_precision) |
3294 | 294 |
295 It is possible to achieve a wide range of output styles by using | |
26301
99318daeaddd
Properly deprecate output_max_field_width from cset 4d945f2e5914.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25352
diff
changeset
|
296 different values of @code{output_precision}. Reasonable combinations can be |
99318daeaddd
Properly deprecate output_max_field_width from cset 4d945f2e5914.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25352
diff
changeset
|
297 set using the @code{format} function. @xref{Basic Input and Output}. |
3294 | 298 |
3321 | 299 @DOCSTRING(split_long_rows) |
3294 | 300 |
301 Octave automatically switches to scientific notation when values become | |
302 very large or very small. This guarantees that you will see several | |
303 significant figures for every value in a matrix. If you would prefer to | |
28652
bb0ca2753bc2
update docs for built-in variables (bug #58988)
John W. Eaton <jwe@octave.org>
parents:
27880
diff
changeset
|
304 see all values in a matrix printed in a fixed point format, you can use |
bb0ca2753bc2
update docs for built-in variables (bug #58988)
John W. Eaton <jwe@octave.org>
parents:
27880
diff
changeset
|
305 the function @code{fixed_point_format}. But doing so is not |
bb0ca2753bc2
update docs for built-in variables (bug #58988)
John W. Eaton <jwe@octave.org>
parents:
27880
diff
changeset
|
306 recommended, because it can produce output that can easily be |
bb0ca2753bc2
update docs for built-in variables (bug #58988)
John W. Eaton <jwe@octave.org>
parents:
27880
diff
changeset
|
307 misinterpreted. |
3294 | 308 |
3321 | 309 @DOCSTRING(fixed_point_format) |
3294 | 310 |
311 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
312 * Empty Matrices:: |
3294 | 313 @end menu |
314 | |
4167 | 315 @node Empty Matrices |
3294 | 316 @subsection Empty Matrices |
317 | |
318 A matrix may have one or both dimensions zero, and operations on empty | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18900
diff
changeset
|
319 matrices are handled as described by @nospell{Carl de Boor} in |
27585
757a7119e319
doc: Create en-dashes and em-dashes correctly in documentation.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
320 @cite{An Empty Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18900
diff
changeset
|
321 @nospell{C. N. Nett and W. M. Haddad}, in |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18900
diff
changeset
|
322 @cite{A System-Theoretic Appropriate Realization of the Empty Matrix Concept}, |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18900
diff
changeset
|
323 IEEE Transactions on Automatic Control, Volume 38, Number 5, May 1993. |
3294 | 324 @tex |
325 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, | |
326 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or | |
327 both dimensions equal to zero), the following are true: | |
328 $$ | |
329 \eqalign{% | |
330 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr | |
331 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
332 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr |
3294 | 333 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr |
334 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} | |
335 $$ | |
336 @end tex | |
6620 | 337 @ifnottex |
3294 | 338 Briefly, given a scalar @var{s}, an @var{m} by |
339 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix | |
340 @code{[](mxn)} (with either one or both dimensions equal to zero), the | |
341 following are true: | |
342 | |
343 @example | |
344 @group | |
345 s * [](mxn) = [](mxn) * s = [](mxn) | |
346 | |
347 [](mxn) + [](mxn) = [](mxn) | |
348 | |
349 [](0xm) * M(mxn) = [](0xn) | |
350 | |
351 M(mxn) * [](nx0) = [](mx0) | |
352 | |
353 [](mx0) * [](0xn) = 0(mxn) | |
354 @end group | |
355 @end example | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
356 |
6620 | 357 @end ifnottex |
3294 | 358 |
359 By default, dimensions of the empty matrix are printed along with the | |
360 empty matrix symbol, @samp{[]}. The built-in variable | |
361 @code{print_empty_dimensions} controls this behavior. | |
362 | |
3321 | 363 @DOCSTRING(print_empty_dimensions) |
3294 | 364 |
365 Empty matrices may also be used in assignment statements as a convenient | |
366 way to delete rows or columns of matrices. | |
17097
e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Rik <rik@octave.org>
parents:
16939
diff
changeset
|
367 @xref{Assignment Ops,,Assignment Expressions}. |
3294 | 368 |
369 When Octave parses a matrix expression, it examines the elements of the | |
370 list to determine whether they are all constants. If they are, it | |
371 replaces the list with a single matrix constant. | |
372 | |
4167 | 373 @node Ranges |
3294 | 374 @section Ranges |
375 @cindex range expressions | |
376 @cindex expression, range | |
377 | |
25352
3d5f953e2ef6
doc: Rewrite section on indexing for clarity (bug #53675).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
378 @opindex :, range expressions |
3294 | 379 |
380 A @dfn{range} is a convenient way to write a row vector with evenly | |
381 spaced elements. A range expression is defined by the value of the first | |
382 element in the range, an optional value for the increment between | |
383 elements, and a maximum value which the elements of the range will not | |
384 exceed. The base, increment, and limit are separated by colons (the | |
385 @samp{:} character) and may contain any arithmetic expressions and | |
386 function calls. If the increment is omitted, it is assumed to be 1. | |
387 For example, the range | |
388 | |
389 @example | |
390 1 : 5 | |
391 @end example | |
392 | |
393 @noindent | |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
394 defines the set of values @code{[ 1, 2, 3, 4, 5 ]}, and the range |
3294 | 395 |
396 @example | |
397 1 : 3 : 5 | |
398 @end example | |
399 | |
400 @noindent | |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
401 defines the set of values @code{[ 1, 4 ]}. |
3294 | 402 |
403 Although a range constant specifies a row vector, Octave does @emph{not} | |
19394
b39cbe9f3bb0
allow ranges to be disabled
John W. Eaton <jwe@octave.org>
parents:
19040
diff
changeset
|
404 normally convert range constants to vectors unless it is necessary to do so. |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
405 This allows you to write a constant like @code{1 : 10000} without using |
3294 | 406 80,000 bytes of storage on a typical 32-bit workstation. |
407 | |
11179
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
408 A common example of when it does become necessary to convert ranges into |
11573
6f8ffe2c6f76
Grammarcheck txi files for 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
409 vectors occurs when they appear within a vector (i.e., inside square |
6f8ffe2c6f76
Grammarcheck txi files for 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
410 brackets). For instance, whereas |
11179
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
411 |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
412 @example |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
413 x = 0 : 0.1 : 1; |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
414 @end example |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
415 |
11573
6f8ffe2c6f76
Grammarcheck txi files for 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
416 @noindent |
11179
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
417 defines @var{x} to be a variable of type @code{range} and occupies 24 |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
418 bytes of memory, the expression |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
419 |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
420 @example |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
421 y = [ 0 : 0.1 : 1]; |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
422 @end example |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
423 |
11573
6f8ffe2c6f76
Grammarcheck txi files for 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
424 @noindent |
11179
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
425 defines @var{y} to be of type @code{matrix} and occupies 88 bytes of |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
426 memory. |
6ead75935ebf
Explain that ranges in vectors do get expanded
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
10846
diff
changeset
|
427 |
19394
b39cbe9f3bb0
allow ranges to be disabled
John W. Eaton <jwe@octave.org>
parents:
19040
diff
changeset
|
428 This space saving optimization may be disabled using the function |
29982
605275522c37
Deprecate disable_range, disable_diagonal_matrix, disable_permutation_matrix.
Rik <rik@octave.org>
parents:
28652
diff
changeset
|
429 @dfn{optimize_range}. |
19394
b39cbe9f3bb0
allow ranges to be disabled
John W. Eaton <jwe@octave.org>
parents:
19040
diff
changeset
|
430 |
29982
605275522c37
Deprecate disable_range, disable_diagonal_matrix, disable_permutation_matrix.
Rik <rik@octave.org>
parents:
28652
diff
changeset
|
431 @DOCSTRING(optimize_range) |
19394
b39cbe9f3bb0
allow ranges to be disabled
John W. Eaton <jwe@octave.org>
parents:
19040
diff
changeset
|
432 |
3294 | 433 Note that the upper (or lower, if the increment is negative) bound on |
434 the range is not always included in the set of values, and that ranges | |
435 defined by floating point values can produce surprising results because | |
436 Octave uses floating point arithmetic to compute the values in the | |
437 range. If it is important to include the endpoints of a range and the | |
438 number of elements is known, you should use the @code{linspace} function | |
439 instead (@pxref{Special Utility Matrices}). | |
440 | |
9158 | 441 When adding a scalar to a range, subtracting a scalar from it (or subtracting a |
442 range from a scalar) and multiplying by scalar, Octave will attempt to avoid | |
443 unpacking the range and keep the result as a range, too, if it can determine | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
444 that it is safe to do so. For instance, doing |
9158 | 445 |
446 @example | |
447 a = 2*(1:1e7) - 1; | |
448 @end example | |
449 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
450 @noindent |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
451 will produce the same result as @code{1:2:2e7-1}, but without ever forming a |
9158 | 452 vector with ten million elements. |
453 | |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
454 Using zero as an increment in the colon notation, as @code{1:0:1} is not |
9158 | 455 allowed, because a division by zero would occur in determining the number of |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
456 range elements. However, ranges with zero increment (i.e., all elements equal) |
9158 | 457 are useful, especially in indexing, and Octave allows them to be constructed |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
458 using the built-in function @code{ones}. Note that because a range must be a |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
459 row vector, @code{ones (1, 10)} produces a range, while @code{ones (10, 1)} |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
20716
diff
changeset
|
460 does not. |
9158 | 461 |
3294 | 462 When Octave parses a range expression, it examines the elements of the |
463 expression to determine whether they are all constants. If they are, it | |
464 replaces the range expression with a single range constant. | |
465 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
466 @node Single Precision Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
467 @section Single Precision Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
468 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
469 Octave includes support for single precision data types, and most of the |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
470 functions in Octave accept single precision values and return single |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
471 precision answers. A single precision variable is created with the |
8534
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
472 @code{single} function. |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
473 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
474 @DOCSTRING(single) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
475 |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
476 for example: |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
477 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
478 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
479 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
480 sngl = single (rand (2, 2)) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
481 @result{} sngl = |
8534
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
482 0.37569 0.92982 |
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
483 0.11962 0.50876 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
484 class (sngl) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
485 @result{} single |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
486 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
487 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
488 |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
489 Many functions can also return single precision values directly. For |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
490 example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
491 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
492 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
493 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
494 ones (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
495 zeros (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
496 eye (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
497 rand (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
498 NaN (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
499 NA (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
500 Inf (2, 2, "single") |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
501 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
502 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
503 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
504 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
505 will all return single precision matrices. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
506 |
6549 | 507 @node Integer Data Types |
508 @section Integer Data Types | |
509 | |
6620 | 510 Octave supports integer matrices as an alternative to using double |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
511 precision. It is possible to use both signed and unsigned integers |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
512 represented by 8, 16, 32, or 64 bits. It should be noted that most |
6620 | 513 computations require floating point data, meaning that integers will |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
514 often change type when involved in numeric computations. For this |
6620 | 515 reason integers are most often used to store data, and not for |
516 calculations. | |
517 | |
518 In general most integer matrices are created by casting | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
519 existing matrices to integers. The following example shows how to cast |
6620 | 520 a matrix into 32 bit integers. |
521 | |
522 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
523 @group |
6620 | 524 float = rand (2, 2) |
525 @result{} float = 0.37569 0.92982 | |
526 0.11962 0.50876 | |
527 integer = int32 (float) | |
528 @result{} integer = 0 1 | |
529 0 1 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
530 @end group |
6620 | 531 @end example |
532 | |
533 @noindent | |
534 As can be seen, floating point values are rounded to the nearest integer | |
535 when converted. | |
536 | |
6549 | 537 @DOCSTRING(isinteger) |
538 | |
539 @DOCSTRING(int8) | |
540 | |
541 @DOCSTRING(uint8) | |
542 | |
543 @DOCSTRING(int16) | |
544 | |
545 @DOCSTRING(uint16) | |
546 | |
547 @DOCSTRING(int32) | |
548 | |
549 @DOCSTRING(uint32) | |
550 | |
551 @DOCSTRING(int64) | |
552 | |
553 @DOCSTRING(uint64) | |
554 | |
555 @DOCSTRING(intmax) | |
556 | |
557 @DOCSTRING(intmin) | |
558 | |
17390
4bcd301754ce
Add Matlab-compatible flintmax function
Mike Miller <mtmiller@ieee.org>
parents:
17281
diff
changeset
|
559 @DOCSTRING(flintmax) |
4bcd301754ce
Add Matlab-compatible flintmax function
Mike Miller <mtmiller@ieee.org>
parents:
17281
diff
changeset
|
560 |
6624 | 561 @menu |
562 * Integer Arithmetic:: | |
563 @end menu | |
564 | |
6620 | 565 @node Integer Arithmetic |
566 @subsection Integer Arithmetic | |
567 | |
568 While many numerical computations can't be carried out in integers, | |
569 Octave does support basic operations like addition and multiplication | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
570 on integers. The operators @code{+}, @code{-}, @code{.*}, and @code{./} |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
571 work on integers of the same type. So, it is possible to add two 32 bit |
6620 | 572 integers, but not to add a 32 bit integer and a 16 bit integer. |
573 | |
574 When doing integer arithmetic one should consider the possibility of | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
575 underflow and overflow. This happens when the result of the computation |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
576 can't be represented using the chosen integer type. As an example it is |
6620 | 577 not possible to represent the result of @math{10 - 20} when using |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
578 unsigned integers. Octave makes sure that the result of integer |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
579 computations is the integer that is closest to the true result. So, the |
6620 | 580 result of @math{10 - 20} when using unsigned integers is zero. |
581 | |
582 When doing integer division Octave will round the result to the nearest | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
583 integer. This is different from most programming languages, where the |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
584 result is often floored to the nearest integer. So, the result of |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14402
diff
changeset
|
585 @code{int32 (5) ./ int32 (8)} is @code{1}. |
6620 | 586 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
587 @DOCSTRING(idivide) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
588 |
6781 | 589 @node Bit Manipulations |
590 @section Bit Manipulations | |
591 | |
592 Octave provides a number of functions for the manipulation of numeric | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
593 values on a bit by bit basis. The basic functions to set and obtain the |
6781 | 594 values of individual bits are @code{bitset} and @code{bitget}. |
595 | |
596 @DOCSTRING(bitset) | |
597 | |
598 @DOCSTRING(bitget) | |
599 | |
600 The arguments to all of Octave's bitwise operations can be scalar or | |
601 arrays, except for @code{bitcmp}, whose @var{k} argument must a | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
602 scalar. In the case where more than one argument is an array, then all |
6781 | 603 arguments must have the same shape, and the bitwise operator is applied |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
604 to each of the elements of the argument individually. If at least one |
6781 | 605 argument is a scalar and one an array, then the scalar argument is |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
606 duplicated. Therefore |
6781 | 607 |
608 @example | |
609 bitget (100, 8:-1:1) | |
610 @end example | |
611 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
612 @noindent |
6781 | 613 is the same as |
614 | |
615 @example | |
616 bitget (100 * ones (1, 8), 8:-1:1) | |
617 @end example | |
618 | |
619 It should be noted that all values passed to the bit manipulation | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
620 functions of Octave are treated as integers. Therefore, even though the |
6781 | 621 example for @code{bitset} above passes the floating point value |
622 @code{10}, it is treated as the bits @code{[1, 0, 1, 0]} rather than the | |
623 bits of the native floating point format representation of @code{10}. | |
624 | |
8828 | 625 As the maximum value that can be represented by a number is important |
6781 | 626 for bit manipulation, particularly when forming masks, Octave supplies |
20716
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20486
diff
changeset
|
627 two utility functions: @code{flintmax} for floating point integers, and |
1ecee53513d7
doc: Peridodic grammar check of documentation.
Rik <rik@octave.org>
parents:
20486
diff
changeset
|
628 @code{intmax} for integer objects (@code{uint8}, @code{int64}, etc.). |
6781 | 629 |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
630 Octave also includes the basic bitwise 'and', 'or', and 'exclusive or' |
6781 | 631 operators. |
632 | |
633 @DOCSTRING(bitand) | |
634 | |
635 @DOCSTRING(bitor) | |
636 | |
637 @DOCSTRING(bitxor) | |
638 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
639 The bitwise 'not' operator is a unary operator that performs a logical |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
640 negation of each of the bits of the value. For this to make sense, the |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
641 mask against which the value is negated must be defined. Octave's |
6781 | 642 bitwise 'not' operator is @code{bitcmp}. |
643 | |
644 @DOCSTRING(bitcmp) | |
645 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
646 Octave also includes the ability to left-shift and right-shift values bitwise. |
6781 | 647 |
648 @DOCSTRING(bitshift) | |
649 | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
650 Bits that are shifted out of either end of the value are lost. Octave |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
651 also uses arithmetic shifts, where the sign bit of the value is kept |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
652 during a right shift. For example: |
6781 | 653 |
654 @example | |
655 @group | |
656 bitshift (-10, -1) | |
657 @result{} -5 | |
658 bitshift (int8 (-1), -1) | |
659 @result{} -1 | |
660 @end group | |
661 @end example | |
662 | |
663 Note that @code{bitshift (int8 (-1), -1)} is @code{-1} since the bit | |
664 representation of @code{-1} in the @code{int8} data type is @code{[1, 1, | |
665 1, 1, 1, 1, 1, 1]}. | |
666 | |
4167 | 667 @node Logical Values |
3428 | 668 @section Logical Values |
669 | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
670 Octave has built-in support for logical values, i.e., variables that |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
671 are either @code{true} or @code{false}. When comparing two variables, |
6620 | 672 the result will be a logical value whose value depends on whether or |
673 not the comparison is true. | |
674 | |
675 The basic logical operations are @code{&}, @code{|}, and @code{!}, | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
676 which correspond to ``Logical And'', ``Logical Or'', and ``Logical |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
677 Negation''. These operations all follow the usual rules of logic. |
6620 | 678 |
679 It is also possible to use logical values as part of standard numerical | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
680 calculations. In this case @code{true} is converted to @code{1}, and |
6620 | 681 @code{false} to 0, both represented using double precision floating |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
682 point numbers. So, the result of @code{true*22 - false/6} is @code{22}. |
6620 | 683 |
684 Logical values can also be used to index matrices and cell arrays. | |
685 When indexing with a logical array the result will be a vector containing | |
686 the values corresponding to @code{true} parts of the logical array. | |
687 The following example illustrates this. | |
688 | |
689 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
690 @group |
6620 | 691 data = [ 1, 2; 3, 4 ]; |
692 idx = (data <= 2); | |
693 data(idx) | |
9136
537a73861cb7
Fix small mistake in example.
Ansgar Burchard <ansgar@43-1.org>
parents:
9036
diff
changeset
|
694 @result{} ans = [ 1; 2 ] |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
695 @end group |
6620 | 696 @end example |
697 | |
698 @noindent | |
699 Instead of creating the @code{idx} array it is possible to replace | |
11376
567ca09a97aa
Remove obsolete documentation about 64-bit integer arithmetic.
Rik <octave@nomad.inbox5.com>
parents:
11179
diff
changeset
|
700 @code{data(idx)} with @w{@code{data( data <= 2 )}} in the above code. |
6620 | 701 |
8828 | 702 Logical values can also be constructed by |
6620 | 703 casting numeric objects to logical values, or by using the @code{true} |
704 or @code{false} functions. | |
705 | |
6549 | 706 @DOCSTRING(logical) |
707 | |
3428 | 708 @DOCSTRING(true) |
709 | |
710 @DOCSTRING(false) | |
711 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
712 @node Promotion and Demotion of Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
713 @section Promotion and Demotion of Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
714 |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
715 Many operators and functions can work with mixed data types. For example, |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
716 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
717 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
718 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
719 uint8 (1) + 1 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
720 @result{} 2 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
721 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
722 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
723 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
724 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
725 where the above operator works with an 8-bit integer and a double precision |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
726 value and returns an 8-bit integer value. Note that the type is demoted |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
727 to an 8-bit integer, rather than promoted to a double precision value as |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
728 might be expected. The reason is that if Octave promoted values in |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
729 expressions like the above with all numerical constants would need to be |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
730 explicitly cast to the appropriate data type like |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
731 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
732 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
733 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
734 uint8 (1) + uint8 (1) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
735 @result{} 2 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
736 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
737 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
738 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
739 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
740 which becomes difficult for the user to apply uniformly and might allow |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
741 hard to find bugs to be introduced. The same applies to single precision |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
742 values where a mixed operation such as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
743 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
744 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
745 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
746 single (1) + 1 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
747 @result{} 2 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
748 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
749 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
750 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
751 @noindent |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
752 returns a single precision value. The mixed operations that are valid |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
753 and their returned data types are |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
754 |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
755 @multitable @columnfractions .2 .3 .3 .2 |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
756 @headitem @tab Mixed Operation @tab Result @tab |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
757 @item @tab double OP single @tab single @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
758 @item @tab double OP integer @tab integer @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
759 @item @tab double OP char @tab double @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
760 @item @tab double OP logical @tab double @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
761 @item @tab single OP integer @tab integer @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
762 @item @tab single OP char @tab single @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
763 @item @tab single OP logical @tab single @tab |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
764 @end multitable |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
765 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
766 The same logic applies to functions with mixed arguments such as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
767 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
768 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
769 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
770 min (single (1), 0) |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
771 @result{} 0 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
772 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
773 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
774 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
775 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
776 where the returned value is single precision. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
777 |
31069
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
778 Many functions and operators will also promote integer or logical types to |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
779 double, or single to double, especially if they take only one argument. |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
780 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
781 @example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
782 @group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
783 a = det (int8 ([1 2; 3 4])) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
784 @result{} a = -2 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
785 class (a) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
786 @result{} double |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
787 @end group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
788 @end example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
789 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
790 But there are also exceptions for promoting to double, especially if the |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
791 function or operator in question can take multiple arguments. |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
792 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
793 @example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
794 @group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
795 a = eig (int8 ([1 2; 3 4])) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
796 @result{} error: eig: wrong type argument 'int8 matrix' |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
797 @end group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
798 @end example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
799 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
800 When the two operands are both integers but of different widths, then the |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
801 behavior depends on the operator or the function in question. For some |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
802 operators and functions, narrow-bitwidth operands are promoted to a wider |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
803 bitwidth: |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
804 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
805 @example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
806 @group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
807 a = min (int8 (100), int16 (200)) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
808 @result{} 100 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
809 class (a) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
810 @result{} int16 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
811 @end group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
812 @end example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
813 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
814 However, not all functions or operators will accept integer operands of |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
815 differing types: |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
816 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
817 @example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
818 @group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
819 int8 (100) + int16 (200) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
820 @result{} error: binary operator '+' not implemented |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
821 for 'int8 scalar' by 'int16 scalar' operations |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
822 @end group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
823 @end example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
824 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
825 Further, in most cases, both operands need to be signed or both need to be |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
826 unsigned. Mixing signed and unsigned usually causes an error, even if they |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
827 are of the same bitwidth. |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
828 |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
829 @example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
830 @group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
831 min (int8 (100), uint16 (200)) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
832 @result{} error: min: cannot compute min (int8 scalar, uint16 scalar) |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
833 @end group |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
834 @end example |
43974344fe19
doc: Expand documentation about type promotion and demotion (bug #62283)
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
835 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
836 In the case of mixed type indexed assignments, the type is not |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
837 changed. For example, |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
838 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
839 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
840 @group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
841 x = ones (2, 2); |
14402
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
842 x(1, 1) = single (2) |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
843 @result{} x = 2 1 |
cbcaf5602469
doc: Make spacing nicer for Strings chapter of manual.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
844 1 1 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
845 @end group |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
846 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
847 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
848 @noindent |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
849 where @code{x} remains of the double precision type. |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
850 |
4167 | 851 @node Predicates for Numeric Objects |
3294 | 852 @section Predicates for Numeric Objects |
853 | |
6620 | 854 Since the type of a variable may change during the execution of a |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
855 program, it can be necessary to do type checking at run-time. Doing this |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
856 also allows you to change the behavior of a function depending on the |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
857 type of the input. As an example, this naive implementation of @code{abs} |
7001 | 858 returns the absolute value of the input if it is a real number, and the |
6620 | 859 length of the input if it is a complex number. |
860 | |
861 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
862 @group |
6620 | 863 function a = abs (x) |
864 if (isreal (x)) | |
865 a = sign (x) .* x; | |
866 elseif (iscomplex (x)) | |
867 a = sqrt (real(x).^2 + imag(x).^2); | |
868 endif | |
869 endfunction | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9158
diff
changeset
|
870 @end group |
6620 | 871 @end example |
872 | |
873 The following functions are available for determining the type of a | |
874 variable. | |
875 | |
3428 | 876 @DOCSTRING(isnumeric) |
877 | |
18900
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
878 @DOCSTRING(islogical) |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
879 |
12581
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12488
diff
changeset
|
880 @DOCSTRING(isfloat) |
3428 | 881 |
12581
c8be316f9543
Group commonly used isXXX functions together in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12488
diff
changeset
|
882 @DOCSTRING(isreal) |
7638
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7018
diff
changeset
|
883 |
4029 | 884 @DOCSTRING(iscomplex) |
3428 | 885 |
4029 | 886 @DOCSTRING(ismatrix) |
3294 | 887 |
4029 | 888 @DOCSTRING(isvector) |
3294 | 889 |
12488
bea828c03969
Add new isrow, iscolumn functions.
Rik <octave@nomad.inbox5.com>
parents:
12190
diff
changeset
|
890 @DOCSTRING(isrow) |
bea828c03969
Add new isrow, iscolumn functions.
Rik <octave@nomad.inbox5.com>
parents:
12190
diff
changeset
|
891 |
bea828c03969
Add new isrow, iscolumn functions.
Rik <octave@nomad.inbox5.com>
parents:
12190
diff
changeset
|
892 @DOCSTRING(iscolumn) |
bea828c03969
Add new isrow, iscolumn functions.
Rik <octave@nomad.inbox5.com>
parents:
12190
diff
changeset
|
893 |
4029 | 894 @DOCSTRING(isscalar) |
3294 | 895 |
4029 | 896 @DOCSTRING(issquare) |
3294 | 897 |
4029 | 898 @DOCSTRING(issymmetric) |
3428 | 899 |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11403
diff
changeset
|
900 @DOCSTRING(ishermitian) |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11403
diff
changeset
|
901 |
6550 | 902 @DOCSTRING(isdefinite) |
903 | |
18900
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
904 @DOCSTRING(isbanded) |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
905 |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
906 @DOCSTRING(isdiag) |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
907 |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
908 @DOCSTRING(istril) |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
909 |
49961d67e4b9
Add new matrix functions (isdiag, isbanded, istril, istriu) (bug #42688).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
17744
diff
changeset
|
910 @DOCSTRING(istriu) |
6550 | 911 |
912 @DOCSTRING(isprime) | |
13017
aaf6fbb2512f
doc: Add cross references to "status of variables" in sections for predicates
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12581
diff
changeset
|
913 |
aaf6fbb2512f
doc: Add cross references to "status of variables" in sections for predicates
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12581
diff
changeset
|
914 If instead of knowing properties of variables, you wish to know which |
aaf6fbb2512f
doc: Add cross references to "status of variables" in sections for predicates
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12581
diff
changeset
|
915 variables are defined and to gather other information about the |
17097
e7a059a9a644
doc: Use XREF as anchor prefix in documentation for clearer results in Info viewer.
Rik <rik@octave.org>
parents:
16939
diff
changeset
|
916 workspace itself, @pxref{Status of Variables}. |