changeset 18291:9b163d6c1de7

cast.m: Improve documentation. * cast.m: Add two examples and more seealso links.
author Rik <rik@octave.org>
date Thu, 16 Jan 2014 10:21:02 -0800
parents 61dab64aa5a6
children 4718af222d9d
files scripts/miscellaneous/cast.m
diffstat 1 files changed, 26 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/cast.m	Wed Jan 15 17:21:53 2014 -0800
+++ b/scripts/miscellaneous/cast.m	Thu Jan 16 10:21:02 2014 -0800
@@ -19,27 +19,41 @@
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} cast (@var{val}, @var{type})
 ## Convert @var{val} to data type @var{type}.
-## @seealso{int8, uint8, int16, uint16, int32, uint32, int64, uint64, double}
+##
+## The value @var{val} may be modified to fit within the range of the new type.
+##
+## Examples:
+##
+## @example
+## @group
+## cast (-5, "uint8")
+##    @result{} 0
+## cast (300, "int8")
+##    @result{} 127
+## @end group
+## @end example
+##
+## @seealso{typecast, int8, uint8, int16, uint16, int32, uint32, int64, uint64, double, single, logical, char}
 ## @end deftypefn
 
 ## Author: jwe
 
 function retval = cast (val, typ)
 
-  if (nargin == 2)
-    if (ischar (typ))
-      if (any (strcmp (typ, {"int8"; "uint8"; "int16"; "uint16";
-                             "int32"; "uint32"; "int64"; "uint64";
-                             "double"; "single"; "logical"; "char"})))
-        retval = feval (typ, val);
-      else
-        error ("cast: type name '%s' is not a built-in type", typ);
-      endif
+  if (nargin != 2)
+    print_usage ();
+  endif
+
+  if (ischar (typ))
+    if (any (strcmp (typ, {"int8"; "uint8"; "int16"; "uint16";
+                           "int32"; "uint32"; "int64"; "uint64";
+                           "double"; "single"; "logical"; "char"})))
+      retval = feval (typ, val);
     else
-      error ("cast: expecting TYPE name as second argument");
+      error ("cast: type name '%s' is not a built-in type", typ);
     endif
   else
-    print_usage ();
+    error ("cast: expecting TYPE name as second argument");
   endif
 
 endfunction