annotate scripts/miscellaneous/cast.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 2007-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
5 ##
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
6 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
7 ## This file is part of Octave.
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
10 ## 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
11 ## 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
12 ## (at your option) any later version.
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
13 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
18 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6403
diff changeset
20 ## 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
21 ## <https://www.gnu.org/licenses/>.
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
22
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19697
diff changeset
24 ## @deftypefn {} {} cast (@var{val}, "@var{type}")
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
25 ## Convert @var{val} to data type @var{type}.
18291
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
26 ##
23134
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
27 ## 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
28 ## classes:
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19173
diff changeset
29 ##
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
30 ## @example
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
31 ## @group
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
32 ## "double"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
33 ## "single"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
34 ## "logical"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
35 ## "char"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
36 ## "int8"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
37 ## "int16"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
38 ## "int32"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
39 ## "int64"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
40 ## "uint8"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
41 ## "uint16"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
42 ## "uint32"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
43 ## "uint64"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
44 ## @end group
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
45 ## @end example
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
46 ##
18291
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
47 ## The value @var{val} may be modified to fit within the range of the new type.
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
48 ##
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
49 ## Examples:
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
50 ##
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
51 ## @example
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
52 ## @group
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
53 ## cast (-5, "uint8")
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
54 ## @result{} 0
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
55 ## cast (300, "int8")
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## @result{} 127
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
57 ## @end group
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
58 ## @end example
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
59 ##
23134
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
60 ## 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
61 ## 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
62 ## 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
63 ## 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
64 ## 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
65 ## 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
66 ## work
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
67 ##
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
68 ## @example
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
69 ## cast (cast (@var{user_defined_val}, "double"), "uint8")
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
70 ## @end example
01bf5c733de7 doc: Expand documentation for cast() (bug #50201).
Rik <rik@octave.org>
parents: 23083
diff changeset
71 ##
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
72 ## @seealso{typecast, int8, uint8, int16, uint16, int32, uint32, int64, uint64, double, single, logical, char, class, typeinfo}
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
73 ## @end deftypefn
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
74
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
75 function retval = cast (val, type)
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
76
18291
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 if (nargin != 2)
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
78 print_usage ();
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
79 endif
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
80
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
81 if (! ischar (type))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
82 error ("cast: TYPE must be a string");
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
83 elseif (! any (strcmp (type, {"int8"; "uint8"; "int16"; "uint16";
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
84 "int32"; "uint32"; "int64"; "uint64";
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
85 "double"; "single"; "logical"; "char"})))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
86 error ("cast: TYPE '%s' is not a built-in type", type);
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
87 endif
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19173
diff changeset
88
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
89 retval = feval (type, val);
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
90
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
91 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 15466
diff changeset
92
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
93
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
94 %!assert (cast (single (2.5), "double"), 2.5)
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
95 %!assert (cast (2.5, "single"), single (2.5))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
96 %!assert (cast ([5 0 -5], "logical"), [true false true])
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
97 %!assert (cast ([65 66 67], "char"), "ABC")
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
98 %!assert (cast ([-2.5 1.1 2.5], "int8"), int8 ([-3 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
99 %!assert (cast ([-2.5 1.1 2.5], "uint8"), uint8 ([0 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
100 %!assert (cast ([-2.5 1.1 2.5], "int16"), int16 ([-3 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
101 %!assert (cast ([-2.5 1.1 2.5], "uint16"), uint16 ([0 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
102 %!assert (cast ([-2.5 1.1 2.5], "int32"), int32 ([-3 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
103 %!assert (cast ([-2.5 1.1 2.5], "uint32"), uint32 ([0 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
104 %!assert (cast ([-2.5 1.1 2.5], "int64"), int64 ([-3 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
105 %!assert (cast ([-2.5 1.1 2.5], "uint64"), uint64 ([0 1 3]))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
106
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
107 ## Test input validation
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
108 %!error cast ()
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
109 %!error cast (1)
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
110 %!error cast (1,2,3)
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
111 %!error <TYPE 'foobar' is not a built-in type> cast (1, "foobar")
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
112 %!error <TYPE must be a string> cast (1, {"foobar"})