matlab中cast,Cast fi Objects

Ways to Cast with MATLAB Software

You may find it useful to cast data into another type—for example, when you

are casting data from an accumulator to memory. There are several ways to cast data

in MATLAB. The following sections provide examples of four different

methods:

Casting by Subscripted Assignment

Casting by Conversion Function

Casting with the Fixed-Point Designer™

reinterpretcast

function

Casting with the cast FunctionCasting by Subscripted Assignment

The following subscripted assignment statement retains the type of

A and saturates the value of B to an

int8:

A = int8(0);

B = int16(32767);

A(:) = B

A =

int8

127

class(A)

ans =

'int8'

The same is true for fi objects:

fipref('NumericTypeDisplay', 'short');

A = fi(0, 1, 8, 0);

B = fi(32767, 1, 16, 0);

A(:) = B

A =

127

numerictype(1,8,0)

Note

For more information on subscripted assignments, see the subsasgn function.Casting by Conversion Function

You can convert from one data type to another by using a conversion function.

In this example, A does not have to be predefined because it

is overwritten.

B = int16(32767);

A = int8(B)

A =

int8

127

class(A)

ans =

'int8'

The same is true for fi objects:

B = fi(32767,1,16,0)

A = fi(B,1,8,0)

B =

32767

numerictype(1,16,0)

A =

127

numerictype(1,8,0)Using a numerictype Object in the fi Conversion Function

Often a specific numerictype is used in many places, and it

is convenient to predefine numerictype objects for use in the

conversion functions. Predefining these objects is a good practice because it

also puts the data type specification in one place.

T8 = numerictype(1,8,0)

T16 = numerictype(1,16,0)

T8 =

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 8

FractionLength: 0

T16 =

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 16

FractionLength: 0

B = fi(32767,T16)

A = fi(B,T8)

B =

32767

numerictype(1,16,0)

A =

127

numerictype(1,8,0)Casting with the reinterpretcast Function

You can convert fixed-point and built-in data types without changing the

underlying data. The Fixed-Point Designer

reinterpretcast function

performs this type of conversion.

In the following example, B is an unsigned

fi object with a word length of 8 bits and a fraction

length of 5 bits. The reinterpretcast function converts

B into a signed fi object

A with a word length of 8 bits and a fraction length of 1

bit. The real-world values of A and B

differ, but their binary representations are the same.

B = fi([pi/4 1 pi/2 4],0,8,5)

T = numerictype(1,8,1);

A = reinterpretcast(B,T)

B =

0.7813 1.0000 1.5625 4.0000

DataTypeMode: Fixed-point: binary point scaling

Signedness: Unsigned

WordLength: 8

FractionLength: 5

A =

12.5000 16.0000 25.0000 -64.0000

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 8

FractionLength: 1

To verify that the underlying data has not changed, compare the binary

representations of A and B:

binary_B = bin(B)

binary_A = bin(A)

binary_B =

'00011001 00100000 00110010 10000000'

binary_A =

'00011001 00100000 00110010 10000000'Casting with the cast Function

Using the cast function, you can

convert the value of a variable to the same numerictype,

complexity, and fimath as another variable.

In the following example, a is cast to the data type of

b. The output, c, has the same

numerictype and fimath properties as

b, and the value of a.

a = pi;

b = fi([],1,16,13,'RoundingMethod','Floor');

c = cast(a,'like',b)

c =

3.1415

DataTypeMode: Fixed-point: binary point scaling

Signedness: Signed

WordLength: 16

FractionLength: 13

RoundingMethod: Floor

OverflowAction: Saturate

ProductMode: FullPrecision

SumMode: FullPrecision

Using this syntax allows you to specify data types separately from your

algorithmic code as described in Manual Fixed-Point Conversion Best Practices.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值