Negative number rounding confusion

Discussion in 'VHDL' started by ShortCircuit, Apr 12, 2010.

  1. ShortCircuit

    ShortCircuit

    Joined:
    Apr 12, 2010
    Messages:
    6
    Lately I have been doing some fractional binary calculations with signed binary numbers in the 2's complement format. I have a reasonable understanding of how to add, sub, multiply with these numbers etc. However I continue to get caught out by a 2's complement misunderstanding or by possible rounding confusion.

    I am writing VHDL model to match a C algorithm, so I have benchmark of what results I should be getting from this C code. My VHDL model matches the expected C results except for when I get a negative result, which is always -1 off from where I expect it to be.

    I am trying to implement the following equation:
    X=(A-B)*C;
    where A & B are integers represented in binary,
    and C is a fractional binary number

    Examples:
    ( 4 - ( -121 ) )*20.0928 = 2511.6
    ---> In modelSim I get a result of 2511 (Decimal), which is what I expected

    ( -1 - 68 )*5.9936 = -413.558
    --> In VHDL I get -414, when I expected -413.

    In VHDL I perform this calculation then shift right and resize the result inorder to get it into the correct width and with the correct binary point location.

    Is this normal for the negative numbers to round down? I assumed the fractional part would just be disregarded. Has anyone else experienced this issue?

    Thanks
    ShortCircuit, Apr 12, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem_eda
    Replies:
    5
    Views:
    7,854
    Pieter Hulshoff
    Oct 11, 2004
  2. Thor W Hammer

    Formatting a number without rounding

    Thor W Hammer, Nov 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    504
    Karl Seguin
    Nov 22, 2005
  3. david.karr@wamu.net

    Small confusion about negative lookbehind

    david.karr@wamu.net, May 30, 2005, in forum: Java
    Replies:
    2
    Views:
    554
  4. EK
    Replies:
    0
    Views:
    318
  5. MRAB
    Replies:
    2
    Views:
    308
    John Machin
    Jan 31, 2009
Loading...

Share This Page