Mercurial > hg > octave-lyh
annotate doc/interpreter/numbers.txi @ 8534:0eb83938c8bc
Trivial fixes in numbers.txi
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Sun, 18 Jan 2009 22:01:36 +0100 |
parents | fa78cb8d8a5c |
children | 8463d1a2e544 |
rev | line source |
---|---|
6778 | 1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
5 @c Octave is free software; you can redistribute it and/or modify it | |
6 @c under the terms of the GNU General Public License as published by the | |
7 @c Free Software Foundation; either version 3 of the License, or (at | |
8 @c your option) any later version. | |
9 @c | |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 @c for more details. | |
14 @c | |
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 | |
17 @c <http://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 | |
24 A @dfn{numeric constant} may be a scalar, a vector, or a matrix, and it | |
25 may contain complex values. | |
26 | |
27 The simplest form of a numeric constant, a scalar, is a single number | |
28 that can be an integer, a decimal fraction, a number in scientific | |
6620 | 29 (exponential) notation, or a complex number. Note that by default numeric |
3294 | 30 constants are represented within Octave in double-precision floating |
31 point format (complex constants are stored as pairs of double-precision | |
6620 | 32 floating point values). It is however possible to represent real |
8534
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
33 integers as described in @ref{Integer Data Types}. Here are some |
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
34 examples of real-valued numeric constants, which all have the same |
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
35 value: |
3294 | 36 |
37 @example | |
38 @group | |
39 105 | |
40 1.05e+2 | |
41 1050e-1 | |
42 @end group | |
43 @end example | |
44 | |
45 To specify complex constants, you can write an expression of the form | |
46 | |
47 @example | |
48 @group | |
49 3 + 4i | |
50 3.0 + 4.0i | |
51 0.3e1 + 40e-1i | |
52 @end group | |
53 @end example | |
54 | |
6620 | 55 @noindent |
3294 | 56 all of which are equivalent. The letter @samp{i} in the previous example |
57 stands for the pure imaginary constant, defined as | |
58 @iftex | |
59 @tex | |
60 $\sqrt{-1}$. | |
61 @end tex | |
62 @end iftex | |
6620 | 63 @ifnottex |
3294 | 64 @code{sqrt (-1)}. |
6620 | 65 @end ifnottex |
3294 | 66 |
67 For Octave to recognize a value as the imaginary part of a complex | |
68 constant, a space must not appear between the number and the @samp{i}. | |
69 If it does, Octave will print an error message, like this: | |
70 | |
71 @example | |
72 @group | |
73 octave:13> 3 + 4 i | |
74 | |
75 parse error: | |
76 | |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
77 syntax error |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
78 |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
79 >>> 3 + 4 i |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
80 ^ |
3294 | 81 @end group |
82 @end example | |
83 | |
6620 | 84 @noindent |
3294 | 85 You may also use @samp{j}, @samp{I}, or @samp{J} in place of the |
86 @samp{i} above. All four forms are equivalent. | |
87 | |
6549 | 88 @DOCSTRING(double) |
89 | |
90 @DOCSTRING(complex) | |
91 | |
3294 | 92 @menu |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
93 * Matrices:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
94 * Ranges:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
95 * Single Precision Data Types:: |
6549 | 96 * Integer Data Types:: |
6781 | 97 * Bit Manipulations:: |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
98 * Logical Values:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
99 * Promotion and Demotion of Data Types:: |
3294 | 100 * Predicates for Numeric Objects:: |
101 @end menu | |
102 | |
4167 | 103 @node Matrices |
3294 | 104 @section Matrices |
105 @cindex matrices | |
106 | |
107 @opindex [ | |
108 @opindex ] | |
109 @opindex ; | |
110 @opindex , | |
111 | |
112 It is easy to define a matrix of values in Octave. The size of the | |
113 matrix is determined automatically, so it is not necessary to explicitly | |
114 state the dimensions. The expression | |
115 | |
116 @example | |
117 a = [1, 2; 3, 4] | |
118 @end example | |
119 | |
120 @noindent | |
121 results in the matrix | |
122 @iftex | |
123 @tex | |
124 $$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ | |
125 @end tex | |
126 @end iftex | |
6620 | 127 @ifnottex |
3294 | 128 |
129 @example | |
130 @group | |
131 | |
132 / \ | |
133 | 1 2 | | |
134 a = | | | |
135 | 3 4 | | |
136 \ / | |
137 | |
138 @end group | |
139 @end example | |
6620 | 140 @end ifnottex |
3294 | 141 |
142 Elements of a matrix may be arbitrary expressions, provided that the | |
143 dimensions all make sense when combining the various pieces. For | |
144 example, given the above matrix, the expression | |
145 | |
146 @example | |
147 [ a, a ] | |
148 @end example | |
149 | |
150 @noindent | |
151 produces the matrix | |
152 | |
153 @example | |
154 @group | |
155 ans = | |
156 | |
157 1 2 1 2 | |
158 3 4 3 4 | |
159 @end group | |
160 @end example | |
161 | |
162 @noindent | |
163 but the expression | |
164 | |
165 @example | |
166 [ a, 1 ] | |
167 @end example | |
168 | |
169 @noindent | |
170 produces the error | |
171 | |
172 @example | |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
173 error: number of rows must match (1 != 2) near line 13, column 6 |
3294 | 174 @end example |
175 | |
176 @noindent | |
177 (assuming that this expression was entered as the first thing on line | |
178 13, of course). | |
179 | |
180 Inside the square brackets that delimit a matrix expression, Octave | |
181 looks at the surrounding context to determine whether spaces and newline | |
182 characters should be converted into element and row separators, or | |
4476 | 183 simply ignored, so an expression like |
3294 | 184 |
185 @example | |
186 @group | |
187 a = [ 1 2 | |
188 3 4 ] | |
189 @end group | |
190 @end example | |
191 | |
192 @noindent | |
193 will work. However, some possible sources of confusion remain. For | |
194 example, in the expression | |
195 | |
196 @example | |
197 [ 1 - 1 ] | |
198 @end example | |
199 | |
200 @noindent | |
201 the @samp{-} is treated as a binary operator and the result is the | |
202 scalar 0, but in the expression | |
203 | |
204 @example | |
205 [ 1 -1 ] | |
206 @end example | |
207 | |
208 @noindent | |
209 the @samp{-} is treated as a unary operator and the result is the | |
4476 | 210 vector @code{[ 1, -1 ]}. Similarly, the expression |
211 | |
212 @example | |
213 [ sin (pi) ] | |
214 @end example | |
215 | |
216 @noindent | |
217 will be parsed as | |
218 | |
219 @example | |
220 [ sin, (pi) ] | |
221 @end example | |
3294 | 222 |
4476 | 223 @noindent |
224 and will result in an error since the @code{sin} function will be | |
225 called with no arguments. To get around this, you must omit the space | |
226 between @code{sin} and the opening parenthesis, or enclose the | |
227 expression in a set of parentheses: | |
228 | |
229 @example | |
230 [ (sin (pi)) ] | |
231 @end example | |
232 | |
233 Whitespace surrounding the single quote character (@samp{'}, used as a | |
234 transpose operator and for delimiting character strings) can also cause | |
235 confusion. Given @code{a = 1}, the expression | |
3294 | 236 |
237 @example | |
238 [ 1 a' ] | |
239 @end example | |
240 | |
241 @noindent | |
4476 | 242 results in the single quote character being treated as a |
3294 | 243 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
244 expression | |
245 | |
246 @example | |
247 [ 1 a ' ] | |
248 @end example | |
249 | |
250 @noindent | |
251 produces the error message | |
252 | |
253 @example | |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
254 parse error: |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
255 |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
256 syntax error |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
257 |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
258 >>> [ 1 a ' ] |
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7989
diff
changeset
|
259 ^ |
3294 | 260 @end example |
261 | |
262 @noindent | |
6620 | 263 because not doing so would cause trouble when parsing the valid expression |
3294 | 264 |
265 @example | |
266 [ a 'foo' ] | |
267 @end example | |
268 | |
269 For clarity, it is probably best to always use commas and semicolons to | |
4476 | 270 separate matrix elements and rows. |
3294 | 271 |
272 When you type a matrix or the name of a variable whose value is a | |
273 matrix, Octave responds by printing the matrix in with neatly aligned | |
274 rows and columns. If the rows of the matrix are too large to fit on the | |
275 screen, Octave splits the matrix and displays a header before each | |
276 section to indicate which columns are being displayed. You can use the | |
277 following variables to control the format of the output. | |
278 | |
3321 | 279 @DOCSTRING(output_max_field_width) |
3294 | 280 |
3321 | 281 @DOCSTRING(output_precision) |
3294 | 282 |
283 It is possible to achieve a wide range of output styles by using | |
284 different values of @code{output_precision} and | |
285 @code{output_max_field_width}. Reasonable combinations can be set using | |
286 the @code{format} function. @xref{Basic Input and Output}. | |
287 | |
3321 | 288 @DOCSTRING(split_long_rows) |
3294 | 289 |
290 Octave automatically switches to scientific notation when values become | |
291 very large or very small. This guarantees that you will see several | |
292 significant figures for every value in a matrix. If you would prefer to | |
293 see all values in a matrix printed in a fixed point format, you can set | |
294 the built-in variable @code{fixed_point_format} to a nonzero value. But | |
295 doing so is not recommended, because it can produce output that can | |
296 easily be misinterpreted. | |
297 | |
3321 | 298 @DOCSTRING(fixed_point_format) |
3294 | 299 |
300 @menu | |
301 * Empty Matrices:: | |
302 @end menu | |
303 | |
4167 | 304 @node Empty Matrices |
3294 | 305 @subsection Empty Matrices |
306 | |
307 A matrix may have one or both dimensions zero, and operations on empty | |
308 matrices are handled as described by Carl de Boor in @cite{An Empty | |
309 Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. | |
310 Haddad, in @cite{A System-Theoretic Appropriate Realization of the Empty | |
311 Matrix Concept}, IEEE Transactions on Automatic Control, Volume 38, | |
312 Number 5, May 1993. | |
313 @iftex | |
314 @tex | |
315 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, | |
316 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or | |
317 both dimensions equal to zero), the following are true: | |
318 $$ | |
319 \eqalign{% | |
320 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr | |
321 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr | |
322 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr | |
323 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr | |
324 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} | |
325 $$ | |
326 @end tex | |
327 @end iftex | |
6620 | 328 @ifnottex |
3294 | 329 Briefly, given a scalar @var{s}, an @var{m} by |
330 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix | |
331 @code{[](mxn)} (with either one or both dimensions equal to zero), the | |
332 following are true: | |
333 | |
334 @example | |
335 @group | |
336 s * [](mxn) = [](mxn) * s = [](mxn) | |
337 | |
338 [](mxn) + [](mxn) = [](mxn) | |
339 | |
340 [](0xm) * M(mxn) = [](0xn) | |
341 | |
342 M(mxn) * [](nx0) = [](mx0) | |
343 | |
344 [](mx0) * [](0xn) = 0(mxn) | |
345 @end group | |
346 @end example | |
6620 | 347 @end ifnottex |
3294 | 348 |
349 By default, dimensions of the empty matrix are printed along with the | |
350 empty matrix symbol, @samp{[]}. The built-in variable | |
351 @code{print_empty_dimensions} controls this behavior. | |
352 | |
3321 | 353 @DOCSTRING(print_empty_dimensions) |
3294 | 354 |
355 Empty matrices may also be used in assignment statements as a convenient | |
356 way to delete rows or columns of matrices. | |
357 @xref{Assignment Ops, ,Assignment Expressions}. | |
358 | |
359 When Octave parses a matrix expression, it examines the elements of the | |
360 list to determine whether they are all constants. If they are, it | |
361 replaces the list with a single matrix constant. | |
362 | |
4167 | 363 @node Ranges |
3294 | 364 @section Ranges |
365 @cindex range expressions | |
366 @cindex expression, range | |
367 | |
3920 | 368 @opindex colon |
3294 | 369 |
370 A @dfn{range} is a convenient way to write a row vector with evenly | |
371 spaced elements. A range expression is defined by the value of the first | |
372 element in the range, an optional value for the increment between | |
373 elements, and a maximum value which the elements of the range will not | |
374 exceed. The base, increment, and limit are separated by colons (the | |
375 @samp{:} character) and may contain any arithmetic expressions and | |
376 function calls. If the increment is omitted, it is assumed to be 1. | |
377 For example, the range | |
378 | |
379 @example | |
380 1 : 5 | |
381 @end example | |
382 | |
383 @noindent | |
384 defines the set of values @samp{[ 1, 2, 3, 4, 5 ]}, and the range | |
385 | |
386 @example | |
387 1 : 3 : 5 | |
388 @end example | |
389 | |
390 @noindent | |
391 defines the set of values @samp{[ 1, 4 ]}. | |
392 | |
393 Although a range constant specifies a row vector, Octave does @emph{not} | |
394 convert range constants to vectors unless it is necessary to do so. | |
395 This allows you to write a constant like @samp{1 : 10000} without using | |
396 80,000 bytes of storage on a typical 32-bit workstation. | |
397 | |
398 Note that the upper (or lower, if the increment is negative) bound on | |
399 the range is not always included in the set of values, and that ranges | |
400 defined by floating point values can produce surprising results because | |
401 Octave uses floating point arithmetic to compute the values in the | |
402 range. If it is important to include the endpoints of a range and the | |
403 number of elements is known, you should use the @code{linspace} function | |
404 instead (@pxref{Special Utility Matrices}). | |
405 | |
406 When Octave parses a range expression, it examines the elements of the | |
407 expression to determine whether they are all constants. If they are, it | |
408 replaces the range expression with a single range constant. | |
409 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
410 @node Single Precision Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
411 @section Single Precision Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
412 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
413 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
|
414 functions in Octave accept single precision values and return single |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
415 precion 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
|
416 @code{single} function. |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
417 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
418 @DOCSTRING(single) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
419 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
420 for example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
421 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
422 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
423 sngl = single (rand (2, 2)) |
8534
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
424 @result{} sngl = |
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
425 0.37569 0.92982 |
0eb83938c8bc
Trivial fixes in numbers.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8347
diff
changeset
|
426 0.11962 0.50876 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
427 class (sngl) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
428 @result{} single |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
429 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
430 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
431 Many functions can also return single precision values directly. For |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
432 example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
433 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
434 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
435 ones (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
436 zeros (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
437 eye (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
438 rand (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
439 NaN (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
440 NA (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
441 Inf (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
442 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
443 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
444 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
445 will all return single precision matrices. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
446 |
6549 | 447 @node Integer Data Types |
448 @section Integer Data Types | |
449 | |
6620 | 450 Octave supports integer matrices as an alternative to using double |
451 precision. It is possible to use both signed and unsigned integers | |
452 represented by 8, 16, 32, or 64 bits. It should be noted that most | |
453 computations require floating point data, meaning that integers will | |
454 often change type when involved in numeric computations. For this | |
455 reason integers are most often used to store data, and not for | |
456 calculations. | |
457 | |
458 In general most integer matrices are created by casting | |
459 existing matrices to integers. The following example shows how to cast | |
460 a matrix into 32 bit integers. | |
461 | |
462 @example | |
463 float = rand (2, 2) | |
464 @result{} float = 0.37569 0.92982 | |
465 0.11962 0.50876 | |
466 integer = int32 (float) | |
467 @result{} integer = 0 1 | |
468 0 1 | |
469 @end example | |
470 | |
471 @noindent | |
472 As can be seen, floating point values are rounded to the nearest integer | |
473 when converted. | |
474 | |
6549 | 475 @DOCSTRING(isinteger) |
476 | |
477 @DOCSTRING(int8) | |
478 | |
479 @DOCSTRING(uint8) | |
480 | |
481 @DOCSTRING(int16) | |
482 | |
483 @DOCSTRING(uint16) | |
484 | |
485 @DOCSTRING(int32) | |
486 | |
487 @DOCSTRING(uint32) | |
488 | |
489 @DOCSTRING(int64) | |
490 | |
491 @DOCSTRING(uint64) | |
492 | |
493 @DOCSTRING(intmax) | |
494 | |
495 @DOCSTRING(intmin) | |
496 | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
8015
diff
changeset
|
497 @DOCSTRING(intwarning) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
8015
diff
changeset
|
498 |
6624 | 499 @menu |
500 * Integer Arithmetic:: | |
501 @end menu | |
502 | |
6620 | 503 @node Integer Arithmetic |
504 @subsection Integer Arithmetic | |
505 | |
506 While many numerical computations can't be carried out in integers, | |
507 Octave does support basic operations like addition and multiplication | |
508 on integers. The operators @code{+}, @code{-}, @code{.*}, and @code{./} | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
509 work on integers of the same type. So, it is possible to add two 32 bit |
6620 | 510 integers, but not to add a 32 bit integer and a 16 bit integer. |
511 | |
6715 | 512 The arithmetic operations on integers are performed by casting the |
513 integer values to double precision values, performing the operation, and | |
514 then re-casting the values back to the original integer type. As the | |
515 double precision type of Octave is only capable of representing integers | |
516 with up to 53 bits of precision, it is not possible to perform | |
517 arithmetic of the 64 bit integer types. | |
518 | |
6620 | 519 When doing integer arithmetic one should consider the possibility of |
520 underflow and overflow. This happens when the result of the computation | |
521 can't be represented using the chosen integer type. As an example it is | |
522 not possible to represent the result of @math{10 - 20} when using | |
523 unsigned integers. Octave makes sure that the result of integer | |
524 computations is the integer that is closest to the true result. So, the | |
525 result of @math{10 - 20} when using unsigned integers is zero. | |
526 | |
527 When doing integer division Octave will round the result to the nearest | |
528 integer. This is different from most programming languages, where the | |
529 result is often floored to the nearest integer. So, the result of | |
530 @code{int32(5)./int32(8)} is @code{1}. | |
531 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
532 @DOCSTRING(idivide) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
533 |
6781 | 534 @node Bit Manipulations |
535 @section Bit Manipulations | |
536 | |
537 Octave provides a number of functions for the manipulation of numeric | |
538 values on a bit by bit basis. The basic functions to set and obtain the | |
539 values of individual bits are @code{bitset} and @code{bitget}. | |
540 | |
541 @DOCSTRING(bitset) | |
542 | |
543 @DOCSTRING(bitget) | |
544 | |
545 The arguments to all of Octave's bitwise operations can be scalar or | |
546 arrays, except for @code{bitcmp}, whose @var{k} argument must a | |
547 scalar. In the case where more than one argument is an array, then all | |
548 arguments must have the same shape, and the bitwise operator is applied | |
549 to each of the elements of the argument individually. If at least one | |
550 argument is a scalar and one an array, then the scalar argument is | |
551 duplicated. Therefore | |
552 | |
553 @example | |
554 bitget (100, 8:-1:1) | |
555 @end example | |
556 | |
557 is the same as | |
558 | |
559 @example | |
560 bitget (100 * ones (1, 8), 8:-1:1) | |
561 @end example | |
562 | |
563 It should be noted that all values passed to the bit manipulation | |
564 functions of Octave are treated as integers. Therefore, even though the | |
565 example for @code{bitset} above passes the floating point value | |
566 @code{10}, it is treated as the bits @code{[1, 0, 1, 0]} rather than the | |
567 bits of the native floating point format representation of @code{10}. | |
568 | |
569 As the maximum number that can be represented by a number is important | |
570 for bit manipulation, particularly when forming masks, Octave supplies | |
571 the function @code{bitmax}. | |
572 | |
573 @DOCSTRING(bitmax) | |
574 | |
575 This is the double precision version of the functions @code{intmax}, | |
576 previously discussed. | |
577 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
578 Octave also includes the basic bitwise 'and', 'or' and 'exclusive or' |
6781 | 579 operators. |
580 | |
581 @DOCSTRING(bitand) | |
582 | |
583 @DOCSTRING(bitor) | |
584 | |
585 @DOCSTRING(bitxor) | |
586 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
587 The bitwise 'not' operator is a unary operator that performs a logical |
6781 | 588 negation of each of the bits of the value. For this to make sense, the |
589 mask against which the value is negated must be defined. Octave's | |
590 bitwise 'not' operator is @code{bitcmp}. | |
591 | |
592 @DOCSTRING(bitcmp) | |
593 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
594 Octave also includes the ability to left-shift and right-shift values bitwise. |
6781 | 595 |
596 @DOCSTRING(bitshift) | |
597 | |
598 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
|
599 also uses arithmetic shifts, where the sign bit of the value is kept |
6781 | 600 during a right shift. For example |
601 | |
602 @example | |
603 @group | |
604 bitshift (-10, -1) | |
605 @result{} -5 | |
606 bitshift (int8 (-1), -1) | |
607 @result{} -1 | |
608 @end group | |
609 @end example | |
610 | |
611 Note that @code{bitshift (int8 (-1), -1)} is @code{-1} since the bit | |
612 representation of @code{-1} in the @code{int8} data type is @code{[1, 1, | |
613 1, 1, 1, 1, 1, 1]}. | |
614 | |
4167 | 615 @node Logical Values |
3428 | 616 @section Logical Values |
617 | |
6620 | 618 Octave has built-in support for logical values, i.e. variables that |
619 are either @code{true} or @code{false}. When comparing two variables, | |
620 the result will be a logical value whose value depends on whether or | |
621 not the comparison is true. | |
622 | |
623 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
|
624 which correspond to ``Logical And'', ``Logical Or'', and ``Logical |
6620 | 625 Negation''. These operations all follow the rules of logic. |
626 | |
627 It is also possible to use logical values as part of standard numerical | |
628 calculations. In this case @code{true} is converted to @code{1}, and | |
629 @code{false} to 0, both represented using double precision floating | |
630 point numbers. So, the result of @code{true*22 - false/6} is @code{22}. | |
631 | |
632 Logical values can also be used to index matrices and cell arrays. | |
633 When indexing with a logical array the result will be a vector containing | |
634 the values corresponding to @code{true} parts of the logical array. | |
635 The following example illustrates this. | |
636 | |
637 @example | |
638 data = [ 1, 2; 3, 4 ]; | |
639 idx = (data <= 2); | |
640 data(idx) | |
641 @result{} ans = [ 1; 4 ] | |
642 @end example | |
643 | |
644 @noindent | |
645 Instead of creating the @code{idx} array it is possible to replace | |
646 @code{data(idx)} with @code{data( data <= 2 )} in the above code. | |
647 | |
648 Besides when doing comparisons, logical values can be constructed by | |
649 casting numeric objects to logical values, or by using the @code{true} | |
650 or @code{false} functions. | |
651 | |
6549 | 652 @DOCSTRING(logical) |
653 | |
3428 | 654 @DOCSTRING(true) |
655 | |
656 @DOCSTRING(false) | |
657 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
658 @node Promotion and Demotion of Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
659 @section Promotion and Demotion of Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
660 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
661 Many operators and functions can work with mixed data types. For example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
662 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
663 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
664 uint8 (1) + 1 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
665 @result{} 2 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
666 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
667 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
668 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
669 where the above operator works with an 8-bit integer and a double precision |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
670 value and returns an 8-bit integer value. Note that the type is demoted |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
671 to an 8-bit integer, rather than promoted to a double precision value as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
672 might be expected. The reason is that if Octave promoted values in |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
673 expressions like the above with all numerical constants would need to be |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
674 explicitly cast to the appropriate data type type like |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
675 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
676 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
677 uint8 (1) + uint8 (1) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
678 @result{} 2 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
679 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
680 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
681 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
682 which becomes difficult for the user to apply uniformly and might allow |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
683 hard to find bugs to be introduced. The same applies to single precision |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
684 values where a mixed operation such as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
685 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
686 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
687 single (1) + 1 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
688 @result{} 2 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
689 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
690 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
691 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
692 returns a single precision value. The mixed operations that are valid |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
693 and their returned data types are |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
694 |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
695 @multitable @columnfractions .2 .3 .3 .2 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
696 @item @tab Mixed Operation @tab Result @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
697 @item @tab double OP single @tab single @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
698 @item @tab double OP integer @tab integer @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
699 @item @tab double OP char @tab double @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
700 @item @tab double OP logical @tab double @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
701 @item @tab single OP integer @tab integer @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
702 @item @tab single OP char @tab single @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
703 @item @tab single OP logical @tab single @tab |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
704 @end multitable |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
705 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
706 The same logic applies to functions with mixed arguments such as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
707 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
708 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
709 min (single (1), 0) |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
710 @result{} 0 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
711 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
712 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
713 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
714 where the returned value is single precision. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
715 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
716 In the case of mixed type indexed assignments, the type is not |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
717 changed. For example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
718 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
719 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
720 x = ones (2, 2); |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
721 x (1, 1) = single (2) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
722 @result{} x = 2 1 |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
723 1 1 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
724 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
725 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
726 @noindent |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
727 where @code{x} remains of the double precision type. |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
728 |
4167 | 729 @node Predicates for Numeric Objects |
3294 | 730 @section Predicates for Numeric Objects |
731 | |
6620 | 732 Since the type of a variable may change during the execution of a |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8325
diff
changeset
|
733 program, it can be necessary to do type checking at run-time. Doing this |
6620 | 734 also allows you to change the behaviour of a function depending on the |
735 type of the input. As an example, this naive implementation of @code{abs} | |
7001 | 736 returns the absolute value of the input if it is a real number, and the |
6620 | 737 length of the input if it is a complex number. |
738 | |
739 @example | |
740 function a = abs (x) | |
741 if (isreal (x)) | |
742 a = sign (x) .* x; | |
743 elseif (iscomplex (x)) | |
744 a = sqrt (real(x).^2 + imag(x).^2); | |
745 endif | |
746 endfunction | |
747 @end example | |
748 | |
749 The following functions are available for determining the type of a | |
750 variable. | |
751 | |
3428 | 752 @DOCSTRING(isnumeric) |
753 | |
754 @DOCSTRING(isreal) | |
755 | |
7638
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7018
diff
changeset
|
756 @DOCSTRING(isfloat) |
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7018
diff
changeset
|
757 |
4029 | 758 @DOCSTRING(iscomplex) |
3428 | 759 |
4029 | 760 @DOCSTRING(ismatrix) |
3294 | 761 |
4029 | 762 @DOCSTRING(isvector) |
3294 | 763 |
4029 | 764 @DOCSTRING(isscalar) |
3294 | 765 |
4029 | 766 @DOCSTRING(issquare) |
3294 | 767 |
4029 | 768 @DOCSTRING(issymmetric) |
3428 | 769 |
6550 | 770 @DOCSTRING(isdefinite) |
771 | |
6688 | 772 @DOCSTRING(islogical) |
6550 | 773 |
774 @DOCSTRING(isprime) |