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