Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fixed point

Subject: fixed point

From: Amish Rughoonundon

Date: 4 Oct, 2010 21:00:22

Message: 1 of 4

Hi,
I am getting confused with the way matlab computes fixed point.
I tried to get the fixed point representation of -1.5 two ways
I understand that for the first one is uses the MSB as the sign bit and then the computation is
[-1*(2^0)]+[0*(1/2^1) ] = -1

I am totally confused how it's calculating the second one. I would expect it to throw an error since it cannot fit the sign bit on top of two fractional bit in a word length of 2.
Is the word length meaningless. Thanks for any insight.

>> finum=fi(-1.5,1,2,1)
finum = -1
          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 2
        FractionLength: 1
>> finum.bin
ans =10

>> finum=fi(-1.5,1,2,2)
finum = -0.5000
          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 2
        FractionLength: 2
>> finum.bin
ans =10

Subject: fixed point

From: Roger Stafford

Date: 5 Oct, 2010 03:27:05

Message: 2 of 4

"Amish Rughoonundon" <axr0284@yahoo.com> wrote in message <i8df95$c8j$1@fred.mathworks.com>...
> Hi,
> I am getting confused with the way matlab computes fixed point.
> I tried to get the fixed point representation of -1.5 two ways
> I understand that for the first one is uses the MSB as the sign bit and then the computation is
> [-1*(2^0)]+[0*(1/2^1) ] = -1
>
> I am totally confused how it's calculating the second one. I would expect it to throw an error since it cannot fit the sign bit on top of two fractional bit in a word length of 2.
> Is the word length meaningless. Thanks for any insight.
>
> >> finum=fi(-1.5,1,2,1)
> finum = -1
> DataTypeMode: Fixed-point: binary point scaling
> Signedness: Signed
> WordLength: 2
> FractionLength: 1
> >> finum.bin
> ans =10
>
> >> finum=fi(-1.5,1,2,2)
> finum = -0.5000
> DataTypeMode: Fixed-point: binary point scaling
> Signedness: Signed
> WordLength: 2
> FractionLength: 2
> >> finum.bin
> ans =10
- - - - - - - - - - -
  You can't accurately represent -1.5 with fixed point format less than word length 3 bits. With fraction length 1, it would be (in twos complement):

 -1*2^1 + 0*2^0 + 1*2^(-1) = -1.5 (bits = 1 0.1)

With fraction length 2, you would need word length of at least 4:

 -1*2^1 + 0*2^0 + 1*2^(-1) + 0*2^(-2) = -1.5 (bits = 1 0.1 0)

  Read the Wikipedia article at:

 http://en.wikipedia.org/wiki/Two's_complement

Roger Stafford

Subject: fixed point

From: Amish Rughoonundon

Date: 5 Oct, 2010 12:49:04

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i8e5u9$pmg$1@fred.mathworks.com>...
> "Amish Rughoonundon" <axr0284@yahoo.com> wrote in message <i8df95$c8j$1@fred.mathworks.com>...
> You can't accurately represent -1.5 with fixed point format less than word length 3 bits. With fraction length 1, it would be (in twos complement):
>
> -1*2^1 + 0*2^0 + 1*2^(-1) = -1.5 (bits = 1 0.1)
>
> With fraction length 2, you would need word length of at least 4:
>
> -1*2^1 + 0*2^0 + 1*2^(-1) + 0*2^(-2) = -1.5 (bits = 1 0.1 0)
>
> Read the Wikipedia article at:
>
> http://en.wikipedia.org/wiki/Two's_complement
>
> Roger Stafford

Any reason matlab doesn't throw any warning or error message when my word length is greater or equal to my fractional length for a signed number

Subject: fixed point

From: Roger Stafford

Date: 5 Oct, 2010 18:34:05

Message: 4 of 4

"Amish Rughoonundon" <axr0284@yahoo.com> wrote in message <i8f6s0$alm$1@fred.mathworks.com>...
> Any reason matlab doesn't throw any warning or error message when my word length is greater or equal to my fractional length for a signed number
- - - - - - - -
  When a "word length is greater or equal to my fractional length" that wouldn't imply that a warning is called for. The larger the word length, the less likely the need for a warning. It actually depends on the size of the number that is to be fitted into the fixed point format. Overflow or underflow happens if the number involved is either too large in the positive direction or in the negative direction, respectively, to fit into that format. If either of these happens, the result will depend on whether a "saturate" or "wrap" option has been selected earlier. Whether a warning is issued depends on whether the "logging mode" property has been set.

  By all means study thoroughly the documentation for the Fixed Point Toolbox. That should answer all your questions about fixed point format.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us