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

    function PhysValues = hw2phys(Prec, HWValues)
%HW2PHYS Convert hardware values to physical values.
%
%   PhysValues = HW2PHYS(FLOATPREC, HWValues) converts floating point
%   hardware values HWValues to physical values PhysValues using the
%   achievable resolution specified by the cgprecfloat object FLOATPREC.

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


% Error check on HWValues structure
HWValues = i_check(Prec, HWValues, 'HWValues');

% Error check on HWValues.mantissa
HWValues.mantissa = i_check(Prec, HWValues.mantissa, 'HWValues.mantissa');

% Error check on HWValues.exponent
HWValues.exponent = i_check(Prec, HWValues.exponent, 'HWValues.exponent');

% Error check on HWValues.sign
HWValues.sign = i_check(Prec, HWValues.sign, 'HWValues.sign');

% Initialise PhysValues array
PhysValues = zeros(size(HWValues.mantissa));

% Calculate nonzero physical values
% Use the equation n = (1+f)*2^e where n is the physical value, f is the
% mantissa (0<f<1) and e is the exponent.  Reference: "Floating Points - IEEE
% Standard Unifies Arithmetic Model", available online at
% http://www.mathworks.co.uk/company/newsletter/pdf/Fall96Cleve.pdf
PhysValues(~isinf(HWValues.exponent)) = ...
    ((1+HWValues.mantissa(~isinf(HWValues.exponent))).* ...  % mantissa term
    (2.^HWValues.exponent(~isinf(HWValues.exponent)))).* ... % exponent term
    HWValues.sign(~isinf(HWValues.exponent));                % sign

% Insert zero physical values as required
PhysValues(isinf(HWValues.exponent)) = 0;


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

% Check input variables
switch VarName
    case {'HWValues'}
        if ~isstruct(in)
            % HWValues is not a structure
            error(message('mbc:cgprecfloat:InvalidState'));
        elseif ~isfield(in, 'mantissa')
            % HWValues does not have a field mantissa
            error('mbc:cgprecfloat:InvalidState', ...
                'HWValues must be a structure with a ''mantissa'' field.');
        elseif ~isfield(in, 'exponent')
            % HWValues does not have a field exponent
            error(message('mbc:cgprecfloat:InvalidState1'));
        elseif ~isfield(in, 'sign')
            % HWValues does not have a field sign
            error(message('mbc:cgprecfloat:InvalidState2'));
        else
            out = in;
        end
    case {'HWValues.mantissa','HWValues.exponent'}
        if pCheckNumeric(Prec, in, true, VarName)
            % Value is valid
            out = in;
        end
    case {'HWValues.sign'}
        if pCheckNumeric(Prec, in, true, VarName)
            % Value is valid
            out = sign(in);
        end
end