您的位置:首页 > 编程语言 > MATLAB

matlab中的数据类型和显示精度

2011-04-13 21:19 225 查看
;==========================================

>> help class
CLASS Return class name of object.
S = CLASS(OBJ) returns the name of the class of object OBJ.

Possibilities are:
double -- Double precision floating point number array
(this is the traditional MATLAB matrix or array)
single -- Single precision floating point number array
logical -- Logical array
char -- Character array
cell -- Cell array
struct -- Structure array
function_handle -- Function Handle
int8 -- 8-bit signed integer array
uint8 -- 8-bit unsigned integer array
int16 -- 16-bit signed integer array
uint16 -- 16-bit unsigned integer array
int32 -- 32-bit signed integer array
uint32 -- 32-bit unsigned integer array
int64 -- 64-bit signed integer array
uint64 -- 64-bit unsigned integer array
<class_name> -- MATLAB class name for MATLAB objects
<java_class> -- Java class name for java objects

%Example 1: Obtain the name of the class of value PI
name = class(PI);

%Example 2: Obtain the full name of a package-based java class
import java.lang.*;
obj = String('mystring');
class(obj)

For classes created without a CLASSDEF statement (pre-MATLAB version
7.6 syntax), CLASS invoked within a constructor method creates an
object of type 'class_name'. Constructor methods are functions saved
in a file named <class_name>.m and placed in a directory named
@<class_name>. Note that 'class_name' must be the second argument to
CLASS. Uses of CLASS for this purpose are shown below.

O = CLASS(S,'class_name') creates an object of class 'class_name'
from the structure S.

O = CLASS(S,'class_name',PARENT1,PARENT2,...) also inherits the
methods and fields of the parent objects PARENT1, PARENT2, ...

O = CLASS(struct([]),'class_name',PARENT1,PARENT2,...), specifying
an empty structure S, creates an object that inherits the methods and
fields from one or more parent classes, but does not have any
additional fields beyond those inherited from the parents.

See also isa, superiorto, inferiorto, classdef, struct.

;=====================================

>> help format
FORMAT Set output format.
FORMAT with no inputs sets the output format to the default appropriate
for the class of the variable. For float variables, the default is
FORMAT SHORT.

FORMAT does not affect how MATLAB computations are done. Computations
on float variables, namely single or double, are done in appropriate
floating point precision, no matter how those variables are displayed.
Computations on integer variables are done natively in integer. Integer
variables are always displayed to the appropriate number of digits for
the class, for example, 3 digits to display the INT8 range -128:127.
FORMAT SHORT and LONG do not affect the display of integer variables.

FORMAT may be used to switch between different output display formats
of all float variables as follows:
FORMAT SHORT Scaled fixed point format with 5 digits.
FORMAT LONG Scaled fixed point format with 15 digits for double
and 7 digits for single.
FORMAT SHORTE Floating point format with 5 digits.
FORMAT LONGE Floating point format with 15 digits for double and
7 digits for single.
FORMAT SHORTG Best of fixed or floating point format with 5
digits.
FORMAT LONGG Best of fixed or floating point format with 15
digits for double and 7 digits for single.
FORMAT SHORTENG Engineering format that has at least 5 digits
and a power that is a multiple of three
FORMAT LONGENG Engineering format that has exactly 16 significant
digits and a power that is a multiple of three.

FORMAT may be used to switch between different output display formats
of all numeric variables as follows:
FORMAT HEX Hexadecimal format.
FORMAT + The symbols +, - and blank are printed
for positive, negative and zero elements.
Imaginary parts are ignored.
FORMAT BANK Fixed format for dollars and cents.
FORMAT RAT Approximation by ratio of small integers. Numbers
with a large numerator or large denominator are
replaced by *.

FORMAT may be used to affect the spacing in the display of all
variables as follows:
FORMAT COMPACT Suppresses extra line-feeds.
FORMAT LOOSE Puts the extra line-feeds back in.

Example:
format short, pi, single(pi)
displays both double and single pi with 5 digits as 3.1416 while
format long, pi, single(pi)
displays pi as 3.141592653589793 and single(pi) as 3.1415927.

format, intmax('uint64'), realmax
shows these values as 18446744073709551615 and 1.7977e+308 while
format hex, intmax('uint64'), realmax
shows them as ffffffffffffffff and 7fefffffffffffff respectively.
The HEX display corresponds to the internal representation of the value
and is not the same as the hexadecimal notation in the C programming
language.

See also disp, display, isnumeric, isfloat, isinteger.
;=========================================

>> help vpa
VPA Variable precision arithmetic.
R = VPA(S) numerically evaluates each element of the double matrix
S using variable precision floating point arithmetic with D decimal
digit accuracy, where D is the current setting of DIGITS.
The resulting R is a SYM.

VPA(S,D) uses D digits, instead of the current setting of DIGITS.
D is an integer or the SYM representation of a number.

It is important to avoid the evaluation of an expression using double
precision floating point arithmetic before it is passed to VPA.
For example,
phi = vpa((1+sqrt(5))/2)
first computes a 16-digit approximation to the golden ratio, then
converts that approximation to one with d digits, where d is the current
setting of DIGITS. To get full precision, use unevaluated string or
symbolic arguments,
phi = vpa('(1+sqrt(5))/2')
or
s = sym('sqrt(5)')
phi = vpa((1+s)/2);

Additional examples:
vpa(pi,780) shows six consecutive 9's near digit 770 in the
decimal expansion of pi.

vpa(hilb(2),5) returns

[ 1., .50000]
[.50000, .33333]

See also double, digits.

Overloaded methods:
sym/vpa

Reference page in Help browser
doc vpa

VPA Variable precision arithmetic.
R = VPA(S) numerically evaluates each element of S using variable
precision floating point arithmetic with D decimal digit accuracy,
where D is the current setting of DIGITS. R is a SYM.

VPA(S,D) uses D digits, instead of the current setting of DIGITS.
D is an integer or the SYM representation of a number.

Examples:
phi = sym('(1+sqrt(5))/2'); % phi is the "golden ratio".
vpa(phi,75) is a string containing 75 digits of phi.

vpa(sym(pi),1919) is a screen full of pi.
vpa(sym('exp(pi*sqrt(163))'),36) shows an "almost integer".

vpa(sym(hilb(2)),5) returns
[ 1., .50000]
[.50000, .33333]

See also double, digits.

;===================================

matlab能够表示的最小实数为2^(-1074),任何绝对值小于2^(-1074)的实数,matlab都将其视为0。

>>x=2^(-1074)

x=4.9407e-324

>>x=2^(-1075)

x=0

>>x==0

ans=1

matlab中默认的数据类型为双精度类型。

>> str='/n/t%g to %g /n/t %g to %g';
>> sprintf(str,-realmax,-realmin,realmin,realmax)

ans =

-1.79769e+308 to -2.22507e-308 %-2^1024 ~-2^(-1022)
2.22507e-308 to 1.79769e+308 %2^(-1022)~2^1024

matlab中有一个内置常量eps,其值为2.2204*10^(-16) ,它可以作为实数是否等于0的一个比较对象。

>> help realmax
REALMAX Largest positive floating point number.
x = realmax is the largest double precision floating point number
representable on this computer. Anything larger overflows.

REALMAX('double') is the same as REALMAX with no arguments.

REALMAX('single') is the largest single precision floating point number
representable on this computer. Anything larger overflows to single(Inf).

See also eps, realmin, intmax.

>> help eps
EPS Spacing of floating point numbers.
D = EPS(X), is the positive distance from ABS(X) to the next larger in
magnitude floating point number of the same precision as X.
X may be either double precision or single precision.
For all X, EPS(X) is equal to EPS(ABS(X)).

EPS, with no arguments, is the distance from 1.0 to the next larger double
precision number, that is EPS with no arguments returns 2^(-52).

EPS('double') is the same as EPS, or EPS(1.0).
EPS('single') is the same as EPS(single(1.0)), or single(2^-23).

Except for numbers whose absolute value is smaller than REALMIN,
if 2^E <= ABS(X) < 2^(E+1), then
EPS(X) returns 2^(E-23) if ISA(X,'single')
EPS(X) returns 2^(E-52) if ISA(X,'double')

For all X of class double such that ABS(X) <= REALMIN, EPS(X)
returns 2^(-1074). Similarly, for all X of class single such that
ABS(X) <= REALMIN('single'), EPS(X) returns 2^(-149).

Replace expressions of the form
if Y < EPS * ABS(X)
with
if Y < EPS(X)

Example return values from calling EPS with various inputs are
presented in the table below:

Expression Return Value
===========================================
eps(1/2) 2^(-53)
eps(1) 2^(-52)
eps(2) 2^(-51)
eps(realmax) 2^971
eps(0) 2^(-1074)
eps(realmin/2) 2^(-1074)
eps(realmin/16) 2^(-1074)
eps(Inf) NaN
eps(NaN) NaN
-------------------------------------------
eps(single(1/2)) 2^(-24)
eps(single(1)) 2^(-23)
eps(single(2)) 2^(-22)
eps(realmax('single')) 2^104
eps(single(0)) 2^(-149)
eps(realmin('single')/2) 2^(-149)
eps(realmin('single')/16) 2^(-149)
eps(single(Inf)) single(NaN)
eps(single(NaN)) single(NaN)

See also realmax, realmin.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: