how to convert negative numbers in fixed point

Status
Not open for further replies.
My bad, I thought it could be directly copied.

can you explain me something that i don't understand:

if i get 2 numbers that can be positive or negative (not a integer->number 16 bit fraction 16 bit fixed point- i dont know if the input is positive or negative )for example:


situation 1:

Q_in=266.7244=> 010AB972(hex)
W_in=0.7071 =>B505(hex)

out= Q_in+W_in=010B6E77(hex) =>17526391(dec) /2^16 = 267.4315 (thats a correct answer)

situation 2:

Q_in=266.7244=> 010AB972(hex)
W_in=(-0.7071) =>4AFC(hex)

out= Q_in+W_in=010B046E(hex) =>17499246(dec) /2^16 = 267.0173 (thats a correct answer)

now the problem begin :-? :

situation 3:

Q_in=(-266.7244)=> FEF5468E(hex)
W_in=0.7071 =>B505(hex)

out= Q_in+W_in=FEF5FB93(hex) =>4277533587(dec) /2^16 = 65269.9287 (thats a wrong answer....i suppose to get (-266.0173) )


situation 4:

Q_in=(-266.7244)=> FEF5468E(hex)
W_in=(-0.7071) =>4AFC(hex)

out= Q_in+W_in=FEF5DC86(hex) =>4277525638(dec) /2^16 = 65269.8614 (thats a wrong answer....i suppose to get (-267.4315) )


what i'm doing wrong it the calculation?

thanks
 

situation 3:

Q_in=(-266.7244)=> FEF5468E(hex)
W_in=0.7071 =>B505(hex)

out= Q_in+W_in=FEF5FB93(hex) =>4277533587(dec) /2^16 = 65269.9287 (thats a wrong answer....i suppose to get (-266.0173) )

what i'm doing wrong it the calculation?

thanks

FEF5FB93 is negative value: to get the abs: FEF5FB92 --> flip --> 10A046D (17433709) / 2^16 = 266.0173 (your abs), attach the sign back, -266.0173
 

FEF5FB93 is negative value: to get the abs: FEF5FB92 --> flip --> 10A046D (17433709) / 2^16 = 266.0173 your abs), attach the sign back, -266.0173

But if I don't know if my input W_in or Q_in is positive or negative how can I know if I need to flip the resulte and attch the sigh if I don't know if my input is positive or negative?
For example in situation 1 and 2 I don't need to flip the results (in situation 2 W_in is negative and Q_in positive and still i don't need to flip the results)

I don't understed the principle..
 

If the most significant bit of the W_in or Q_in is a 1 means the value is negative. In 2's complement the MSB's weighting is -2^(n-1) (for n-bits of data).
 


In your algorithm design you must specify fixed-point data type for all your signals and variables. This is the principle. You can keep using std_logic_vector(N downto 0). But whenever you need to convert it to real. You will have to define the fixed-type.

If you deal heavily with fixed-type, consider use Matlab fixed-point tool, or fixed-point package as recommended above.

For good code you will consider adding one more bit to check overflow and underflow but that's more complication added; and you can skip it with discretion.
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…