[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: casting
From: |
Søren Hauberg |
Subject: |
Re: casting |
Date: |
Tue, 13 Mar 2007 09:40:48 +0100 |
User-agent: |
Thunderbird 1.5.0.10 (X11/20070307) |
John W. Eaton skrev:
On 12-Mar-2007, David Grohmann wrote:
|
| Thank you. I will give that a shot.
|
| Søren Hauberg wrote:
| > How about this function:
| >
| > function B = cast(A, newclass)
| > B = feval(newclass, A);
| > endfunction
To avoid the feval and to also be able to detect invalid class names,
I used the following:
function retval = cast (val, typ)
if (nargin == 2)
if (ischar (typ))
switch (typ)
case "int8"
retval = int8 (val);
case "uint8"
retval = uint8 (val);
case "int16"
retval = int16 (val);
case "uint16"
retval = uint16 (val);
case "int32"
retval = int32 (val);
case "uint32"
retval = uint32 (val);
case "int64"
retval = int64 (val);
case "uint64"
retval = uint64 (val);
case "double"
retval = double (val);
case "single"
retval = single (val);
otherwise
error ("cast: invalid type name `%s'", typ);
endswitch
else
error ("cast: expecting type name as second argument");
endif
else
print_usage ();
endif
endfunction
Just a few stupid questions :-)
Is feval really so slow that you switch-case is faster? If I had to do
input checking in my code I would just have added something like
if (!any(strcp(typ, {"int8", "int16", "uint8", (you get the idea...)})))
error("cast: second argument must be the name of a builtin type");
endif
followed be the call to feval. I would just guess that it's more easy to
maintain because it's less code.
Also, I that you handle the "single" type in your code. Does octave
support "single" now?
Is the "cast" function in cvs now?
Søren
- casting, David Grohmann, 2007/03/09