Mercurial > jwe > octave
annotate scripts/miscellaneous/cast.m @ 29363:0a5b15007766 stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 09:52:15 -0500 |
parents | bd51beb6205e |
children | 7854d5752dd2 |
rev | line source |
---|---|
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
2 ## |
29363
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27928
diff
changeset
|
3 ## Copyright (C) 2007-2021 The Octave Project Developers |
27923
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27903
diff
changeset
|
4 ## |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
6403 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
6403 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
6403 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
6403 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
24 ######################################################################## |
6403 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
27 ## @deftypefn {} {} cast (@var{val}, "@var{type}") |
6403 | 28 ## Convert @var{val} to data type @var{type}. |
18291 | 29 ## |
23134
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
30 ## Both @var{val} and @var{type} are typically one of the following built-in |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
31 ## classes: |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19173
diff
changeset
|
32 ## |
19173 | 33 ## @example |
34 ## @group | |
35 ## "double" | |
36 ## "single" | |
37 ## "logical" | |
38 ## "char" | |
39 ## "int8" | |
40 ## "int16" | |
41 ## "int32" | |
42 ## "int64" | |
43 ## "uint8" | |
44 ## "uint16" | |
45 ## "uint32" | |
46 ## "uint64" | |
47 ## @end group | |
48 ## @end example | |
49 ## | |
18291 | 50 ## The value @var{val} may be modified to fit within the range of the new type. |
51 ## | |
52 ## Examples: | |
53 ## | |
54 ## @example | |
55 ## @group | |
56 ## cast (-5, "uint8") | |
57 ## @result{} 0 | |
58 ## cast (300, "int8") | |
59 ## @result{} 127 | |
60 ## @end group | |
61 ## @end example | |
62 ## | |
23134
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
63 ## Programming Note: This function relies on the object @var{val} having a |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
64 ## conversion method named @var{type}. User-defined classes may implement only |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
65 ## a subset of the full list of types shown above. In that case, it may be |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
66 ## necessary to call cast twice in order to reach the desired type. |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
67 ## For example, the conversion to double is nearly always implemented, but |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
68 ## the conversion to uint8 might not be. In that case, the following code will |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
69 ## work |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
70 ## |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
71 ## @example |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
72 ## cast (cast (@var{user_defined_val}, "double"), "uint8") |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
73 ## @end example |
01bf5c733de7
doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents:
23083
diff
changeset
|
74 ## |
19173 | 75 ## @seealso{typecast, int8, uint8, int16, uint16, int32, uint32, int64, uint64, double, single, logical, char, class, typeinfo} |
6403 | 76 ## @end deftypefn |
77 | |
19173 | 78 function retval = cast (val, type) |
6403 | 79 |
18291 | 80 if (nargin != 2) |
81 print_usage (); | |
82 endif | |
83 | |
19173 | 84 if (! ischar (type)) |
85 error ("cast: TYPE must be a string"); | |
86 elseif (! any (strcmp (type, {"int8"; "uint8"; "int16"; "uint16"; | |
87 "int32"; "uint32"; "int64"; "uint64"; | |
88 "double"; "single"; "logical"; "char"}))) | |
89 error ("cast: TYPE '%s' is not a built-in type", type); | |
6403 | 90 endif |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19173
diff
changeset
|
91 |
19173 | 92 retval = feval (type, val); |
6403 | 93 |
94 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
15466
diff
changeset
|
95 |
19173 | 96 |
97 %!assert (cast (single (2.5), "double"), 2.5) | |
98 %!assert (cast (2.5, "single"), single (2.5)) | |
99 %!assert (cast ([5 0 -5], "logical"), [true false true]) | |
100 %!assert (cast ([65 66 67], "char"), "ABC") | |
101 %!assert (cast ([-2.5 1.1 2.5], "int8"), int8 ([-3 1 3])) | |
102 %!assert (cast ([-2.5 1.1 2.5], "uint8"), uint8 ([0 1 3])) | |
103 %!assert (cast ([-2.5 1.1 2.5], "int16"), int16 ([-3 1 3])) | |
104 %!assert (cast ([-2.5 1.1 2.5], "uint16"), uint16 ([0 1 3])) | |
105 %!assert (cast ([-2.5 1.1 2.5], "int32"), int32 ([-3 1 3])) | |
106 %!assert (cast ([-2.5 1.1 2.5], "uint32"), uint32 ([0 1 3])) | |
107 %!assert (cast ([-2.5 1.1 2.5], "int64"), int64 ([-3 1 3])) | |
108 %!assert (cast ([-2.5 1.1 2.5], "uint64"), uint64 ([0 1 3])) | |
109 | |
110 ## Test input validation | |
111 %!error cast () | |
112 %!error cast (1) | |
113 %!error cast (1,2,3) | |
114 %!error <TYPE 'foobar' is not a built-in type> cast (1, "foobar") | |
115 %!error <TYPE must be a string> cast (1, {"foobar"}) |