www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgprecfloat/cgprecfloat.m

    function FloatPrec = cgprecfloat(mbits, ebits, PhysRange, varargin)
%CGPRECFLOAT Constructor for the cgprecfloat class.
%
%   FLOATPREC = CGPRECFLOAT(mbits, ebits, PhysRange, PREC) constructs a
%   cgprecfloat (signed floating point) object FLOATPREC with mbits bits
%   in the mantissa, ebits bits in the exponent, a range PhysRange of
%   admissible physical values and a parent cgprec object PREC.
%
%   FLOATPREC = CGPRECFLOAT constructs a cgprecfloat object with
%   default properties of mbits = 52, ebits = 11, PhysRange = [-Inf Inf]
%   and a default parent cgprec object (IEEE double precision)
%
%   FLOATPREC = CGPRECFLOAT(str) where str is either 'double' or 'single'
%   creates an IEEE double or single floating point cgprecfloat object.
%
%   FLOATPREC = CGPRECFLOAT(mbits, ebits) constructs a cgprecfloat
%   object with mbits bits in the mantissa, ebits bits in the exponent,
%   default properties of PhysRange = [-Inf Inf] and a default parent
%   cgprec object.
%
%   FLOATPREC = CGPRECFLOAT(mbits, ebits, PhysRange) constructs a
%   cgprecfloat object with mbits bits in the mantissa, ebits bits in the
%   exponent, a range PhysRange of admissible physical values and a default
%   parent cgprec object.
%
%   FLOATPREC = CGPRECFLOAT(mbits, ebits, PhysRange, Comment) first
%   constructs a cgprec object cgprec(Comment), and then constructs a
%   child cgprecfloat object of this cgprec object with mbits bits in
%   the mantissa, ebits bits in the exponent and a range PhysRange of
%   admissible physical values.
%
%   See also  CGPRECFIX, CGPREC

%   Copyright 2000-2006 The MathWorks, Inc. and Ford Global Technologies, Inc.


% Error check on mbits, ebits
if nargin==0
    % mbits, ebits not specified, use the default values
    mbits = 52;
    ebits = 11;
elseif nargin==1
    if ischar(mbits)
        switch mbits
            case 'double'
                mbits = 52;
                ebits = 11;
            case 'single'
                mbits = 23;
                ebits = 8;
        end
    else
        error(message('mbc:cgprecfloat:InvalidArgument'));
    end
elseif nargin>1
    % try to use mbits, ebits if valid
    mbits = i_check(mbits, 'mbits');
    ebits = i_check(ebits, 'ebits');
end

% Error check on PhysRange
if nargin<3
    % PhysRange not specified, use the default value
    PhysRange = i_check([], 'PhysRange');
else
    % Try to use PhysRange if valid
    PhysRange = i_check(PhysRange, 'PhysRange');
end

% Error check on varargin (cgprec object or comment)
if nargin<4
    % Information about cgprec object not specified, use the default value
    Prec = cgprec;
else
    if isa(varargin{1}, 'cgprec')
        % Extra input argument is a cgprec object
        Prec = varargin{1};
    else
        % Try to use extra input as a comment with which to construct a
        % cgprec object if valid
        Prec = cgprec(varargin{1});
    end
end

% Assemble object structure
FloatPrecStruct = struct('mbits', mbits, ...
    'ebits', ebits, ...
    'PhysRange', PhysRange);

% Create object class
FloatPrec = class(FloatPrecStruct,'cgprecfloat',Prec);


% -------------------------------------------------------------------------
function out = i_check(in, VarName)

% Check input variables
switch VarName
    case {'mbits','ebits'}
        if isnumeric(in) && isreal(in) && isscalar(in)
            out = max(round(in),1);
        else
            % mbits, ebits is not a real scalar, use default value
            out = 1;
        end
    case {'PhysRange'}
        if isnumeric(in) && isreal(in) && isequal(size(in), [1 2])
            % PhysRange is a real 1x2 array, sort in ascending order
            out = sort(in);
        else
            % PhysRange is not a real 1x2 array, use default value
            out = [-Inf Inf];
        end
end