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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
7 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
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
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
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
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
14 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
15 ## 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
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
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
19 ##
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
20 ## 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
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
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
25
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
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
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
28 ## Convert @var{val} to data type @var{type}.
18291
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
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
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
33 ## @example
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
34 ## @group
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
35 ## "double"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
36 ## "single"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
37 ## "logical"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
38 ## "char"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
39 ## "int8"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
40 ## "int16"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
41 ## "int32"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
42 ## "int64"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
43 ## "uint8"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
44 ## "uint16"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
45 ## "uint32"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
46 ## "uint64"
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
47 ## @end group
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
48 ## @end example
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
49 ##
18291
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
50 ## 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
51 ##
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
52 ## Examples:
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
53 ##
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
54 ## @example
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
55 ## @group
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## cast (-5, "uint8")
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
57 ## @result{} 0
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
58 ## cast (300, "int8")
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
59 ## @result{} 127
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
60 ## @end group
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
61 ## @end example
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
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
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
75 ## @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
76 ## @end deftypefn
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
77
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
78 function retval = cast (val, type)
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
79
18291
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
80 if (nargin != 2)
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 print_usage ();
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
82 endif
9b163d6c1de7 cast.m: Improve documentation.
Rik <rik@octave.org>
parents: 17744
diff changeset
83
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
84 if (! ischar (type))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
85 error ("cast: TYPE must be a string");
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
86 elseif (! any (strcmp (type, {"int8"; "uint8"; "int16"; "uint16";
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
87 "int32"; "uint32"; "int64"; "uint64";
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
88 "double"; "single"; "logical"; "char"})))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
89 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
90 endif
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19173
diff changeset
91
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
92 retval = feval (type, val);
6403
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
93
5011ac2fc23d [project @ 2007-03-13 14:45:51 by jwe]
jwe
parents:
diff changeset
94 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 15466
diff changeset
95
19173
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
96
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
97 %!assert (cast (single (2.5), "double"), 2.5)
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
98 %!assert (cast (2.5, "single"), single (2.5))
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
99 %!assert (cast ([5 0 -5], "logical"), [true false true])
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
100 %!assert (cast ([65 66 67], "char"), "ABC")
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
101 %!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
102 %!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
103 %!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
104 %!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
105 %!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
106 %!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
107 %!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
108 %!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
109
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
110 ## Test input validation
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
111 %!error cast ()
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
112 %!error cast (1)
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
113 %!error cast (1,2,3)
3d24778a8b12 cast.m: Overhaul function.
Rik <rik@octave.org>
parents: 18291
diff changeset
114 %!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
115 %!error <TYPE must be a string> cast (1, {"foobar"})